package defpackage;

import android.util.Log;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: renamed from: ᵚ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C2692 {

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public static final Charset f10364 = Charset.forName("UTF-8");

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final ˁᵦ f10365;

    public C2692(ˁᵦ r1) {
        this.f10365 = r1;
    }

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public static ArrayList m6491(String str) throws JSONException {
        JSONArray jSONArray = new JSONObject(str).getJSONArray("rolloutsState");
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < jSONArray.length(); i++) {
            String string = jSONArray.getString(i);
            try {
                ᵢʒʡᵟ r4 = AbstractC0962.f4238;
                JSONObject jSONObject = new JSONObject(string);
                String string2 = jSONObject.getString("rolloutId");
                String string3 = jSONObject.getString("parameterKey");
                String string4 = jSONObject.getString("parameterValue");
                String string5 = jSONObject.getString("variantId");
                long j = jSONObject.getLong("templateVersion");
                if (string4.length() > 256) {
                    string4 = string4.substring(0, 256);
                }
                arrayList.add(new C2599(j, string2, string3, string4, string5));
            } catch (Exception unused) {
            }
        }
        return arrayList;
    }

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public static String m6492(List list) {
        HashMap map = new HashMap();
        JSONArray jSONArray = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            try {
                jSONArray.put(new JSONObject(AbstractC0962.f4238.ˊᵡᵗʨ(list.get(i))));
            } catch (JSONException unused) {
            }
        }
        map.put("rolloutsState", jSONArray);
        return new JSONObject(map).toString();
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public static HashMap m6493(String str) {
        JSONObject jSONObject = new JSONObject(str);
        HashMap map = new HashMap();
        Iterator<String> itKeys = jSONObject.keys();
        while (itKeys.hasNext()) {
            String next = itKeys.next();
            String strOptString = null;
            if (!jSONObject.isNull(next)) {
                strOptString = jSONObject.optString(next, null);
            }
            map.put(next, strOptString);
        }
        return map;
    }

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public static void m6494(File file) {
        if (file.exists() && file.delete()) {
            file.getAbsolutePath();
        }
    }

    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    public final void m6495(String str, List list) {
        String strM6492;
        BufferedWriter bufferedWriter;
        File file = this.f10365.ᵷᵺʾᵖ(str, "rollouts-state");
        if (list.isEmpty()) {
            if (file.exists() && file.delete()) {
                file.getAbsolutePath();
                return;
            }
            return;
        }
        BufferedWriter bufferedWriter2 = null;
        try {
            try {
                strM6492 = m6492(list);
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), f10364));
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception unused) {
        }
        try {
            bufferedWriter.write(strM6492);
            bufferedWriter.flush();
            ᵥˇᵛˎ.ᵻˈᵦʼ(bufferedWriter);
        } catch (Exception unused2) {
            bufferedWriter2 = bufferedWriter;
            m6494(file);
            ᵥˇᵛˎ.ᵻˈᵦʼ(bufferedWriter2);
        } catch (Throwable th2) {
            th = th2;
            bufferedWriter2 = bufferedWriter;
            ᵥˇᵛˎ.ᵻˈᵦʼ(bufferedWriter2);
            throw th;
        }
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final Map m6496(String str, boolean z) throws Throwable {
        FileInputStream fileInputStream;
        ˁᵦ r2 = this.f10365;
        File file = z ? r2.ᵷᵺʾᵖ(str, "internal-keys") : r2.ᵷᵺʾᵖ(str, "keys");
        if (!file.exists() || file.length() == 0) {
            if (file.exists() && file.delete()) {
                file.getAbsolutePath();
            }
            return Collections.EMPTY_MAP;
        }
        FileInputStream fileInputStream2 = null;
        try {
            try {
                fileInputStream = new FileInputStream(file);
            } catch (Exception unused) {
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            HashMap mapM6493 = m6493(ᵥˇᵛˎ.ˆˀᵯ(fileInputStream));
            ᵥˇᵛˎ.ᵻˈᵦʼ(fileInputStream);
            return mapM6493;
        } catch (Exception unused2) {
            fileInputStream2 = fileInputStream;
            m6494(file);
            ᵥˇᵛˎ.ᵻˈᵦʼ(fileInputStream2);
            return Collections.EMPTY_MAP;
        } catch (Throwable th2) {
            th = th2;
            fileInputStream2 = fileInputStream;
            ᵥˇᵛˎ.ᵻˈᵦʼ(fileInputStream2);
            throw th;
        }
    }

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public final void m6497(String str, Map map, boolean z) {
        String string;
        BufferedWriter bufferedWriter;
        ˁᵦ r3 = this.f10365;
        File file = z ? r3.ᵷᵺʾᵖ(str, "internal-keys") : r3.ᵷᵺʾᵖ(str, "keys");
        BufferedWriter bufferedWriter2 = null;
        try {
            try {
                string = new JSONObject(map).toString();
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), f10364));
            } catch (Exception unused) {
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            bufferedWriter.write(string);
            bufferedWriter.flush();
            ᵥˇᵛˎ.ᵻˈᵦʼ(bufferedWriter);
        } catch (Exception unused2) {
            bufferedWriter2 = bufferedWriter;
            m6494(file);
            ᵥˇᵛˎ.ᵻˈᵦʼ(bufferedWriter2);
        } catch (Throwable th2) {
            th = th2;
            bufferedWriter2 = bufferedWriter;
            ᵥˇᵛˎ.ᵻˈᵦʼ(bufferedWriter2);
            throw th;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r2v0 */
    /* JADX WARN: Type inference failed for: r2v1 */
    /* JADX WARN: Type inference failed for: r2v2, types: [java.io.Closeable] */
    /* JADX WARN: Type inference failed for: r8v2, types: [int] */
    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final String m6498(String str) {
        FileInputStream fileInputStream;
        File file = this.f10365.ᵷᵺʾᵖ(str, "user-data");
        ?? r2 = 0;
        if (file.exists()) {
            ?? r8 = (file.length() > 0L ? 1 : (file.length() == 0L ? 0 : -1));
            try {
                if (r8 != 0) {
                    try {
                        fileInputStream = new FileInputStream(file);
                        try {
                            JSONObject jSONObject = new JSONObject(ᵥˇᵛˎ.ˆˀᵯ(fileInputStream));
                            String strOptString = !jSONObject.isNull("userId") ? jSONObject.optString("userId", null) : null;
                            Log.isLoggable("FirebaseCrashlytics", 3);
                            ᵥˇᵛˎ.ᵻˈᵦʼ(fileInputStream);
                            return strOptString;
                        } catch (Exception unused) {
                            m6494(file);
                            ᵥˇᵛˎ.ᵻˈᵦʼ(fileInputStream);
                            return null;
                        }
                    } catch (Exception unused2) {
                        fileInputStream = null;
                    } catch (Throwable th) {
                        th = th;
                        ᵥˇᵛˎ.ᵻˈᵦʼ((Closeable) r2);
                        throw th;
                    }
                }
            } catch (Throwable th2) {
                th = th2;
                r2 = r8;
            }
        }
        Log.isLoggable("FirebaseCrashlytics", 3);
        m6494(file);
        return null;
    }
}
