package m4;

import android.util.Log;
import d5.AbstractC0844a;
import java.io.BufferedWriter;
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 l6.C1139a;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import q4.C1463c;

/* JADX INFO: renamed from: m4.h, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1172h {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Charset f14622b = Charset.forName("UTF-8");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final C1463c f14623a;

    public C1172h(C1463c c1463c) {
        this.f14623a = c1463c;
    }

    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++) {
            String string = jSONArray.getString(i);
            try {
                C1139a c1139a = AbstractC1178n.f14640a;
                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 j8 = jSONObject.getLong("templateVersion");
                if (string4.length() > 256) {
                    string4 = string4.substring(0, 256);
                }
                arrayList.add(new C1166b(string2, string3, string4, string5, j8));
            } catch (Exception e8) {
                Log.w("FirebaseCrashlytics", "Failed de-serializing rollouts state. " + string, e8);
            }
        }
        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(AbstractC1178n.f14640a.o(list.get(i))));
            } catch (JSONException e8) {
                Log.w("FirebaseCrashlytics", "Exception parsing rollout assignment!", e8);
            }
        }
        map.put("rolloutsState", jSONArray);
        return new JSONObject(map).toString();
    }

    public static void f(File file) {
        if (file.exists() && file.delete()) {
            Log.i("FirebaseCrashlytics", "Deleted corrupt file: " + file.getAbsolutePath(), null);
        }
    }

    public static void g(File file, String str) {
        if (file.exists() && file.delete()) {
            Log.i("FirebaseCrashlytics", "Deleted corrupt file: " + file.getAbsolutePath() + "\nReason: " + str, null);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r1v4, types: [int] */
    /* JADX WARN: Type inference failed for: r1v5 */
    /* JADX WARN: Type inference failed for: r1v8, types: [java.io.Closeable] */
    public final Map c(String str, boolean z8) throws Throwable {
        Throwable th;
        FileInputStream fileInputStream;
        Exception e8;
        C1463c c1463c = this.f14623a;
        File fileF = z8 ? c1463c.f(str, "internal-keys") : c1463c.f(str, "keys");
        if (!fileF.exists() || fileF.length() == 0) {
            g(fileF, "The file has a length of zero for session: " + str);
            return Collections.EMPTY_MAP;
        }
        try {
            try {
                fileInputStream = new FileInputStream(fileF);
                try {
                    HashMap mapA = a(k4.g.i(fileInputStream));
                    k4.g.b(fileInputStream, "Failed to close user metadata file.");
                    return mapA;
                } catch (Exception e9) {
                    e8 = e9;
                    Log.w("FirebaseCrashlytics", "Error deserializing user metadata.", e8);
                    f(fileF);
                    k4.g.b(fileInputStream, "Failed to close user metadata file.");
                    return Collections.EMPTY_MAP;
                }
            } catch (Throwable th2) {
                th = th2;
                k4.g.b(, "Failed to close user metadata file.");
                throw th;
            }
        } catch (Exception e10) {
            fileInputStream = null;
            e8 = e10;
        } catch (Throwable th3) {
            ?? r12 = 0;
            th = th3;
            k4.g.b(r12, "Failed to close user metadata file.");
            throw th;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r3v2, types: [int] */
    /* JADX WARN: Type inference failed for: r4v0 */
    /* JADX WARN: Type inference failed for: r4v1 */
    /* JADX WARN: Type inference failed for: r4v2, types: [java.io.Closeable] */
    public final String d(String str) throws Throwable {
        FileInputStream fileInputStream;
        File fileF = this.f14623a.f(str, "user-data");
        ?? r42 = 0;
        if (fileF.exists()) {
            ?? r32 = (fileF.length() > 0L ? 1 : (fileF.length() == 0L ? 0 : -1));
            try {
                if (r32 != 0) {
                    try {
                        fileInputStream = new FileInputStream(fileF);
                        try {
                            JSONObject jSONObject = new JSONObject(k4.g.i(fileInputStream));
                            String strOptString = !jSONObject.isNull("userId") ? jSONObject.optString("userId", null) : null;
                            String str2 = "Loaded userId " + strOptString + " for session " + str;
                            if (Log.isLoggable("FirebaseCrashlytics", 3)) {
                                Log.d("FirebaseCrashlytics", str2, null);
                            }
                            k4.g.b(fileInputStream, "Failed to close user metadata file.");
                            return strOptString;
                        } catch (Exception e8) {
                            e = e8;
                            Log.w("FirebaseCrashlytics", "Error deserializing user metadata.", e);
                            f(fileF);
                            k4.g.b(fileInputStream, "Failed to close user metadata file.");
                            return null;
                        }
                    } catch (Exception e9) {
                        e = e9;
                        fileInputStream = null;
                    } catch (Throwable th) {
                        th = th;
                        k4.g.b(r42, "Failed to close user metadata file.");
                        throw th;
                    }
                }
            } catch (Throwable th2) {
                th = th2;
                r42 = r32;
            }
        }
        String strX = AbstractC0844a.x("No userId set for session ", str);
        if (Log.isLoggable("FirebaseCrashlytics", 3)) {
            Log.d("FirebaseCrashlytics", strX, null);
        }
        f(fileF);
        return null;
    }

    public final void h(String str, Map map, boolean z8) {
        String string;
        BufferedWriter bufferedWriter;
        C1463c c1463c = this.f14623a;
        File fileF = z8 ? c1463c.f(str, "internal-keys") : c1463c.f(str, "keys");
        BufferedWriter bufferedWriter2 = null;
        try {
            try {
                string = new JSONObject(map).toString();
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileF), f14622b));
            } catch (Exception e8) {
                e = e8;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            bufferedWriter.write(string);
            bufferedWriter.flush();
            k4.g.b(bufferedWriter, "Failed to close key/value metadata file.");
        } catch (Exception e9) {
            e = e9;
            bufferedWriter2 = bufferedWriter;
            Log.w("FirebaseCrashlytics", "Error serializing key/value metadata.", e);
            f(fileF);
            k4.g.b(bufferedWriter2, "Failed to close key/value metadata file.");
        } catch (Throwable th2) {
            th = th2;
            bufferedWriter2 = bufferedWriter;
            k4.g.b(bufferedWriter2, "Failed to close key/value metadata file.");
            throw th;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r2v1, types: [boolean] */
    /* JADX WARN: Type inference failed for: r2v2 */
    /* JADX WARN: Type inference failed for: r2v4, types: [java.io.Closeable] */
    public final void i(String str, List list) {
        Throwable th;
        BufferedWriter bufferedWriter;
        Exception e8;
        File fileF = this.f14623a.f(str, "rollouts-state");
        ?? IsEmpty = list.isEmpty();
        if (IsEmpty != 0) {
            g(fileF, "Rollout state is empty for session: " + str);
            return;
        }
        try {
            try {
                String strE = e(list);
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileF), f14622b));
                try {
                    bufferedWriter.write(strE);
                    bufferedWriter.flush();
                    k4.g.b(bufferedWriter, "Failed to close rollouts state file.");
                } catch (Exception e9) {
                    e8 = e9;
                    Log.w("FirebaseCrashlytics", "Error serializing rollouts state.", e8);
                    f(fileF);
                    k4.g.b(bufferedWriter, "Failed to close rollouts state file.");
                }
            } catch (Throwable th2) {
                th = th2;
                k4.g.b(IsEmpty, "Failed to close rollouts state file.");
                throw th;
            }
        } catch (Exception e10) {
            bufferedWriter = null;
            e8 = e10;
        } catch (Throwable th3) {
            IsEmpty = 0;
            th = th3;
            k4.g.b(IsEmpty, "Failed to close rollouts state file.");
            throw th;
        }
    }
}
