package com.bytedance.sdk.component;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import com.facebook.react.uimanager.events.VMXo.nBTxWTaH;
import com.revenuecat.purchases.common.Constants;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
/* loaded from: classes2.dex */
public class Sg {
    private static YFl GA;
    private static boolean YFl;
    private static ArrayMap<File, Sg> YoT;
    private static ArrayMap<String, File> eT;
    private Properties AlY;
    private long DSW;
    private final File NjR;
    private final Object Sg;
    private final File nc;
    private long qsH;
    private final Object tN;
    private int vc;
    private volatile boolean wN;

    /* loaded from: classes2.dex */
    public interface YFl {
        ExecutorService getExecutorService();
    }

    public static void YFl(YFl yFl) {
        GA = yFl;
    }

    static /* synthetic */ int NjR(Sg sg) {
        int i = sg.vc;
        sg.vc = i - 1;
        return i;
    }

    static /* synthetic */ long vc(Sg sg) {
        long j = sg.DSW;
        sg.DSW = 1 + j;
        return j;
    }

    static /* synthetic */ int wN(Sg sg) {
        int i = sg.vc;
        sg.vc = i + 1;
        return i;
    }

    public static Sg YFl(Context context, String str) {
        if (TextUtils.isEmpty(str)) {
            str = "tt_prop";
        }
        synchronized (Sg.class) {
            if (eT == null) {
                eT = new ArrayMap<>();
            }
            File file = eT.get(str);
            if (file == null) {
                file = new File(context.getFilesDir(), str);
                eT.put(str, file);
            }
            if (YoT == null) {
                YoT = new ArrayMap<>();
            }
            Sg sg = YoT.get(file);
            if (sg == null) {
                Sg sg2 = new Sg(file);
                YoT.put(file, sg2);
                return sg2;
            }
            return sg;
        }
    }

    /* JADX WARN: Type inference failed for: r3v4, types: [com.bytedance.sdk.component.Sg$1] */
    private Sg(File file) {
        Object obj = new Object();
        this.Sg = obj;
        this.tN = new Object();
        this.AlY = new Properties();
        this.wN = false;
        this.vc = 0;
        this.NjR = file;
        this.nc = YFl(file);
        synchronized (obj) {
            this.wN = false;
        }
        YFl yFl = GA;
        if (yFl == null || yFl.getExecutorService() == null) {
            new Thread("TTPropHelper") { // from class: com.bytedance.sdk.component.Sg.1
                @Override // java.lang.Thread, java.lang.Runnable
                public void run() {
                    Sg.this.YFl();
                }
            }.start();
        } else {
            GA.getExecutorService().execute(new Runnable() { // from class: com.bytedance.sdk.component.Sg.2
                @Override // java.lang.Runnable
                public void run() {
                    Sg.this.YFl();
                }
            });
        }
    }

    static File YFl(File file) {
        return new File(file.getPath() + ".bak");
    }

    /* JADX WARN: Removed duplicated region for block: B:47:0x00d3  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:36:0x00b3 -> B:62:0x00bc). Please submit an issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void YFl() {
        /*
            Method dump skipped, instructions count: 237
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.sdk.component.Sg.YFl():void");
    }

    private void AlY() {
        while (!this.wN) {
            try {
                this.Sg.wait();
            } catch (InterruptedException unused) {
            }
        }
    }

    public String YFl(String str, String str2) {
        String property;
        if (TextUtils.isEmpty(str)) {
            return str2;
        }
        synchronized (this.Sg) {
            AlY();
            property = this.AlY.getProperty(str, str2);
        }
        return property;
    }

    public int YFl(String str, int i) {
        int parseInt;
        if (TextUtils.isEmpty(str)) {
            return i;
        }
        synchronized (this.Sg) {
            try {
                try {
                    AlY();
                    parseInt = Integer.parseInt(this.AlY.getProperty(str, String.valueOf(i)));
                } catch (NumberFormatException e) {
                    Log.e("TTPropHelper", e.getMessage());
                    return i;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return parseInt;
    }

    public long YFl(String str, long j) {
        long parseLong;
        if (TextUtils.isEmpty(str)) {
            return j;
        }
        synchronized (this.Sg) {
            try {
                try {
                    AlY();
                    parseLong = Long.parseLong(this.AlY.getProperty(str, String.valueOf(j)));
                } catch (NumberFormatException e) {
                    Log.e("TTPropHelper", e.getMessage());
                    return j;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return parseLong;
    }

    public float YFl(String str, float f) {
        float parseFloat;
        if (TextUtils.isEmpty(str)) {
            return f;
        }
        synchronized (this.Sg) {
            try {
                try {
                    AlY();
                    parseFloat = Float.parseFloat(this.AlY.getProperty(str, String.valueOf(f)));
                } catch (NumberFormatException e) {
                    Log.e("TTPropHelper", e.getMessage());
                    return f;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return parseFloat;
    }

    public boolean YFl(String str, boolean z) {
        boolean parseBoolean;
        if (TextUtils.isEmpty(str)) {
            return z;
        }
        synchronized (this.Sg) {
            try {
                try {
                    AlY();
                    parseBoolean = Boolean.parseBoolean(this.AlY.getProperty(str, String.valueOf(z)));
                } catch (NumberFormatException e) {
                    Log.e("TTPropHelper", e.getMessage());
                    return z;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return parseBoolean;
    }

    public boolean YFl(String str) {
        boolean containsKey;
        synchronized (this.Sg) {
            try {
                try {
                    AlY();
                    containsKey = this.AlY.containsKey(str);
                } catch (NumberFormatException e) {
                    Log.e("TTPropHelper", e.getMessage());
                    return false;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return containsKey;
    }

    public tN Sg() {
        return new tN();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* renamed from: com.bytedance.sdk.component.Sg$Sg  reason: collision with other inner class name */
    /* loaded from: classes2.dex */
    public static class C0105Sg {
        volatile boolean AlY;
        final Properties Sg;
        final long YFl;
        final CountDownLatch tN;
        boolean wN;

        private C0105Sg(long j, Properties properties) {
            this.tN = new CountDownLatch(1);
            this.AlY = false;
            this.wN = false;
            this.YFl = j;
            this.Sg = properties;
        }

        void YFl(boolean z, boolean z2) {
            this.wN = z;
            this.AlY = z2;
            this.tN.countDown();
        }
    }

    /* loaded from: classes7.dex */
    public class tN implements SharedPreferences.Editor {
        private final Object Sg = new Object();
        private final Map<String, Object> tN = new HashMap();
        private boolean AlY = false;

        public tN() {
        }

        @Override // android.content.SharedPreferences.Editor
        /* renamed from: YFl */
        public tN putStringSet(String str, Set<String> set) {
            synchronized (this.Sg) {
                this.tN.put(str, set == null ? null : new HashSet(set));
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* renamed from: YFl */
        public tN putInt(String str, int i) {
            synchronized (this.Sg) {
                this.tN.put(str, Integer.valueOf(i));
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* renamed from: YFl */
        public tN putLong(String str, long j) {
            synchronized (this.Sg) {
                this.tN.put(str, Long.valueOf(j));
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* renamed from: YFl */
        public tN putFloat(String str, float f) {
            synchronized (this.Sg) {
                this.tN.put(str, Float.valueOf(f));
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* renamed from: YFl */
        public tN putString(String str, String str2) {
            synchronized (this.Sg) {
                this.tN.put(str, str2);
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* renamed from: YFl */
        public tN putBoolean(String str, boolean z) {
            synchronized (this.Sg) {
                this.tN.put(str, Boolean.valueOf(z));
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* renamed from: YFl */
        public tN remove(String str) {
            synchronized (this.Sg) {
                this.tN.put(str, this);
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        /* renamed from: YFl */
        public tN clear() {
            synchronized (this.Sg) {
                this.AlY = true;
            }
            return this;
        }

        @Override // android.content.SharedPreferences.Editor
        public boolean commit() {
            String str = nBTxWTaH.tqMXfQDtitteA;
            long currentTimeMillis = Sg.YFl ? System.currentTimeMillis() : 0L;
            C0105Sg Sg = Sg();
            Sg.this.YFl(Sg, true);
            try {
                Sg.tN.await();
                if (Sg.YFl) {
                    Log.d("TTPropHelper", Sg.this.NjR.getName() + Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR + Sg.YFl + str + (System.currentTimeMillis() - currentTimeMillis) + " ms");
                }
                return Sg.AlY;
            } catch (InterruptedException unused) {
                if (Sg.YFl) {
                    Log.d("TTPropHelper", Sg.this.NjR.getName() + Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR + Sg.YFl + str + (System.currentTimeMillis() - currentTimeMillis) + " ms");
                    return false;
                }
                return false;
            } catch (Throwable th) {
                if (Sg.YFl) {
                    Log.d("TTPropHelper", Sg.this.NjR.getName() + Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR + Sg.YFl + str + (System.currentTimeMillis() - currentTimeMillis) + " ms");
                }
                throw th;
            }
        }

        @Override // android.content.SharedPreferences.Editor
        public void apply() {
            Sg.this.YFl(Sg(), false);
        }

        private C0105Sg Sg() {
            Properties properties;
            long j;
            Object obj;
            boolean z;
            synchronized (Sg.this.Sg) {
                if (Sg.this.vc > 0) {
                    Properties properties2 = new Properties();
                    properties2.putAll(Sg.this.AlY);
                    Sg.this.AlY = properties2;
                }
                properties = Sg.this.AlY;
                Sg.wN(Sg.this);
                synchronized (this.Sg) {
                    boolean z2 = false;
                    if (this.AlY) {
                        if (properties.isEmpty()) {
                            z = false;
                        } else {
                            properties.clear();
                            z = true;
                        }
                        this.AlY = false;
                        z2 = z;
                    }
                    for (Map.Entry<String, Object> entry : this.tN.entrySet()) {
                        String key = entry.getKey();
                        Object value = entry.getValue();
                        if (value != this && value != null) {
                            if (!properties.containsKey(key) || (obj = properties.get(key)) == null || !obj.equals(String.valueOf(value))) {
                                properties.put(key, String.valueOf(value));
                                z2 = true;
                            }
                        }
                        if (properties.containsKey(key)) {
                            properties.remove(key);
                            z2 = true;
                        }
                    }
                    this.tN.clear();
                    if (z2) {
                        Sg.vc(Sg.this);
                    }
                    j = Sg.this.DSW;
                }
            }
            return new C0105Sg(j, properties);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void YFl(final C0105Sg c0105Sg, final boolean z) {
        boolean z2;
        Runnable runnable = new Runnable() { // from class: com.bytedance.sdk.component.Sg.3
            @Override // java.lang.Runnable
            public void run() {
                synchronized (Sg.this.tN) {
                    try {
                        Sg.this.Sg(c0105Sg, z);
                    } catch (OutOfMemoryError unused) {
                    }
                }
                synchronized (Sg.this.Sg) {
                    Sg.NjR(Sg.this);
                }
            }
        };
        if (z) {
            synchronized (this.Sg) {
                z2 = this.vc == 1;
            }
            if (z2) {
                runnable.run();
                return;
            }
        }
        com.bytedance.sdk.component.tN.YFl(runnable, true ^ z);
    }

    /*  JADX ERROR: JadxRuntimeException in pass: BlockProcessor
        jadx.core.utils.exceptions.JadxRuntimeException: Unreachable block: B:55:0x00f3
        	at jadx.core.dex.visitors.blocks.BlockProcessor.checkForUnreachableBlocks(BlockProcessor.java:81)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.processBlocksTree(BlockProcessor.java:47)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.visit(BlockProcessor.java:39)
        */
    /* JADX INFO: Access modifiers changed from: private */
    public void Sg(com.bytedance.sdk.component.Sg.C0105Sg r18, boolean r19) {
        /*
            Method dump skipped, instructions count: 471
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.sdk.component.Sg.Sg(com.bytedance.sdk.component.Sg$Sg, boolean):void");
    }
}
