package o;

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;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.vP, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C2710vP {
    public static final Charset b = Charset.forName("UTF-8");
    public final C1533iy a;

    public C2710vP(C1533iy c1533iy) {
        this.a = c1533iy;
    }

    public static HashMap a(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;
    }

    public static ArrayList b(String str) throws JSONException {
        JSONArray jSONArray = new JSONObject(str).getJSONArray("rolloutsState");
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < jSONArray.length(); i++) {
            try {
                arrayList.add(AbstractC1161f30.a(jSONArray.getString(i)));
            } catch (Exception unused) {
            }
        }
        return arrayList;
    }

    public static String e(List list) {
        HashMap map = new HashMap();
        JSONArray jSONArray = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            try {
                jSONArray.put(new JSONObject(AbstractC1161f30.a.c(list.get(i))));
            } catch (JSONException unused) {
            }
        }
        map.put("rolloutsState", jSONArray);
        return new JSONObject(map).toString();
    }

    public static void f(File file) {
        if (file.exists() && file.delete()) {
            file.getAbsolutePath();
        }
    }

    public final Map c(String str, boolean z) throws Throwable {
        FileInputStream fileInputStream;
        C1533iy c1533iy = this.a;
        File fileB = z ? c1533iy.b(str, "internal-keys") : c1533iy.b(str, "keys");
        if (!fileB.exists() || fileB.length() == 0) {
            if (fileB.exists() && fileB.delete()) {
                fileB.getAbsolutePath();
            }
            return Collections.EMPTY_MAP;
        }
        FileInputStream fileInputStream2 = null;
        try {
            try {
                fileInputStream = new FileInputStream(fileB);
            } catch (Exception unused) {
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            HashMap mapA = a(C1884mi0.L(fileInputStream));
            C1884mi0.p(fileInputStream);
            return mapA;
        } catch (Exception unused2) {
            fileInputStream2 = fileInputStream;
            f(fileB);
            C1884mi0.p(fileInputStream2);
            return Collections.EMPTY_MAP;
        } catch (Throwable th2) {
            th = th2;
            fileInputStream2 = fileInputStream;
            C1884mi0.p(fileInputStream2);
            throw th;
        }
    }

    /* JADX WARN: Type inference failed for: r0v2, types: [int] */
    public final String d(String str) {
        FileInputStream fileInputStream;
        File fileB = this.a.b(str, "user-data");
        Closeable closeable = null;
        if (fileB.exists()) {
            ?? r0 = (fileB.length() > 0L ? 1 : (fileB.length() == 0L ? 0 : -1));
            try {
                if (r0 != 0) {
                    try {
                        fileInputStream = new FileInputStream(fileB);
                        try {
                            JSONObject jSONObject = new JSONObject(C1884mi0.L(fileInputStream));
                            String strOptString = !jSONObject.isNull("userId") ? jSONObject.optString("userId", null) : null;
                            Log.isLoggable("FirebaseCrashlytics", 3);
                            C1884mi0.p(fileInputStream);
                            return strOptString;
                        } catch (Exception unused) {
                            f(fileB);
                            C1884mi0.p(fileInputStream);
                            return null;
                        }
                    } catch (Exception unused2) {
                        fileInputStream = null;
                    } catch (Throwable th) {
                        th = th;
                        C1884mi0.p(closeable);
                        throw th;
                    }
                }
            } catch (Throwable th2) {
                th = th2;
                closeable = r0;
            }
        }
        Log.isLoggable("FirebaseCrashlytics", 3);
        f(fileB);
        return null;
    }

    public final void g(String str, Map map, boolean z) throws Throwable {
        String string;
        BufferedWriter bufferedWriter;
        C1533iy c1533iy = this.a;
        File fileB = z ? c1533iy.b(str, "internal-keys") : c1533iy.b(str, "keys");
        BufferedWriter bufferedWriter2 = null;
        try {
            try {
                string = new JSONObject(map).toString();
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileB), b));
            } catch (Exception unused) {
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            bufferedWriter.write(string);
            bufferedWriter.flush();
            C1884mi0.p(bufferedWriter);
        } catch (Exception unused2) {
            bufferedWriter2 = bufferedWriter;
            f(fileB);
            C1884mi0.p(bufferedWriter2);
        } catch (Throwable th2) {
            th = th2;
            bufferedWriter2 = bufferedWriter;
            C1884mi0.p(bufferedWriter2);
            throw th;
        }
    }
}
