package defpackage;

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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
class kv5 {
    public static final Charset b = Charset.forName("UTF-8");
    public final oy2 a;

    public kv5(oy2 oy2Var) {
        this.a = oy2Var;
    }

    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 {
                arrayList.add(c58.a(string));
            } catch (Exception e) {
                hh5.a.b("Failed de-serializing rollouts state. " + string, e);
            }
        }
        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(c58.a.b(list.get(i))));
            } catch (JSONException e) {
                hh5.a.b("Exception parsing rollout assignment!", e);
            }
        }
        map.put("rolloutsState", jSONArray);
        return new JSONObject(map).toString();
    }

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

    public static void g(File file, String str) {
        if (file.exists() && file.delete()) {
            file.getAbsolutePath();
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r1v3, types: [int] */
    /* JADX WARN: Type inference failed for: r1v4 */
    /* JADX WARN: Type inference failed for: r1v7, types: [java.io.Closeable] */
    public final Map c(String str, boolean z) throws Throwable {
        Throwable th;
        FileInputStream fileInputStream;
        Exception e;
        oy2 oy2Var = this.a;
        File fileB = z ? oy2Var.b(str, "internal-keys") : oy2Var.b(str, "keys");
        if (!fileB.exists() || fileB.length() == 0) {
            g(fileB, n41.n("The file has a length of zero for session: ", str));
            return Collections.EMPTY_MAP;
        }
        try {
            try {
                fileInputStream = new FileInputStream(fileB);
                try {
                    HashMap mapA = a(c71.i(fileInputStream));
                    c71.b(fileInputStream, "Failed to close user metadata file.");
                    return mapA;
                } catch (Exception e2) {
                    e = e2;
                    hh5.a.b("Error deserializing user metadata.", e);
                    f(fileB);
                    c71.b(fileInputStream, "Failed to close user metadata file.");
                    return Collections.EMPTY_MAP;
                }
            } catch (Throwable th2) {
                th = th2;
                c71.b(, "Failed to close user metadata file.");
                throw th;
            }
        } catch (Exception e3) {
            fileInputStream = null;
            e = e3;
        } catch (Throwable th3) {
            ?? r1 = 0;
            th = th3;
            c71.b(r1, "Failed to close user metadata file.");
            throw th;
        }
    }

    public final String d(String str) {
        FileInputStream fileInputStream;
        File fileB = this.a.b(str, "user-data");
        FileInputStream fileInputStream2 = null;
        if (!fileB.exists() || fileB.length() == 0) {
            f(fileB);
            return null;
        }
        try {
            fileInputStream = new FileInputStream(fileB);
            try {
                try {
                    JSONObject jSONObject = new JSONObject(c71.i(fileInputStream));
                    String strOptString = jSONObject.isNull("userId") ? null : jSONObject.optString("userId", null);
                    c71.b(fileInputStream, "Failed to close user metadata file.");
                    return strOptString;
                } catch (Exception e) {
                    e = e;
                    hh5.a.b("Error deserializing user metadata.", e);
                    f(fileB);
                    c71.b(fileInputStream, "Failed to close user metadata file.");
                    return null;
                }
            } catch (Throwable th) {
                th = th;
                fileInputStream2 = fileInputStream;
                c71.b(fileInputStream2, "Failed to close user metadata file.");
                throw th;
            }
        } catch (Exception e2) {
            e = e2;
            fileInputStream = null;
        } catch (Throwable th2) {
            th = th2;
            c71.b(fileInputStream2, "Failed to close user metadata file.");
            throw th;
        }
    }

    public final void h(String str, Map map, boolean z) {
        String string;
        BufferedWriter bufferedWriter;
        oy2 oy2Var = this.a;
        File fileB = z ? oy2Var.b(str, "internal-keys") : oy2Var.b(str, "keys");
        BufferedWriter bufferedWriter2 = null;
        try {
            try {
                string = new JSONObject(map).toString();
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileB), b));
            } catch (Exception e) {
                e = e;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            bufferedWriter.write(string);
            bufferedWriter.flush();
            c71.b(bufferedWriter, "Failed to close key/value metadata file.");
        } catch (Exception e2) {
            e = e2;
            bufferedWriter2 = bufferedWriter;
            hh5.a.b("Error serializing key/value metadata.", e);
            f(fileB);
            c71.b(bufferedWriter2, "Failed to close key/value metadata file.");
        } catch (Throwable th2) {
            th = th2;
            bufferedWriter2 = bufferedWriter;
            c71.b(bufferedWriter2, "Failed to close key/value metadata file.");
            throw th;
        }
    }
}
