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;

/* renamed from: ᵻᵟˑᵛ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C5207 {

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public static final Charset f19430 = Charset.forName("UTF-8");

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final ʿˁ f19431;

    public C5207(ʿˁ r1) {
        this.f19431 = r1;
    }

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public static String m10767(List list) {
        HashMap map = new HashMap();
        JSONArray jSONArray = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            try {
                jSONArray.put(new JSONObject(AbstractC2668.f10355.m8348(list.get(i))));
            } catch (JSONException unused) {
            }
        }
        map.put("rolloutsState", jSONArray);
        return new JSONObject(map).toString();
    }

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public static void m10768(File file) {
        if (file.exists() && file.delete()) {
            file.getAbsolutePath();
        }
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public static HashMap m10769(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;
    }

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public static ArrayList m10770(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 {
                C3718 c3718 = AbstractC2668.f10355;
                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 C0796(j, string2, string3, string4, string5));
            } catch (Exception unused) {
            }
        }
        return arrayList;
    }

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public final void m10771(String str, Map map, boolean z) {
        String string;
        BufferedWriter bufferedWriter;
        ʿˁ r3 = this.f19431;
        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), f19430));
            } catch (Exception unused) {
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            bufferedWriter.write(string);
            bufferedWriter.flush();
            ᵕˏᵛ.ˇʼʤˎ(bufferedWriter);
        } catch (Exception unused2) {
            bufferedWriter2 = bufferedWriter;
            m10768(file);
            ᵕˏᵛ.ˇʼʤˎ(bufferedWriter2);
        } catch (Throwable th2) {
            th = th2;
            bufferedWriter2 = bufferedWriter;
            ᵕˏᵛ.ˇʼʤˎ(bufferedWriter2);
            throw th;
        }
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final Map m10772(String str, boolean z) throws Throwable {
        FileInputStream fileInputStream;
        ʿˁ r2 = this.f19431;
        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 mapM10769 = m10769(ᵕˏᵛ.ᵷᵱ(fileInputStream));
            ᵕˏᵛ.ˇʼʤˎ(fileInputStream);
            return mapM10769;
        } catch (Exception unused2) {
            fileInputStream2 = fileInputStream;
            m10768(file);
            ᵕˏᵛ.ˇʼʤˎ(fileInputStream2);
            return Collections.EMPTY_MAP;
        } catch (Throwable th2) {
            th = th2;
            fileInputStream2 = fileInputStream;
            ᵕˏᵛ.ˇʼʤˎ(fileInputStream2);
            throw th;
        }
    }

    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public final void m10773(String str, List list) {
        String strM10767;
        BufferedWriter bufferedWriter;
        File file = this.f19431.ᵻᵢﹳᵰ(str, "rollouts-state");
        if (list.isEmpty()) {
            if (file.exists() && file.delete()) {
                file.getAbsolutePath();
                return;
            }
            return;
        }
        BufferedWriter bufferedWriter2 = null;
        try {
            try {
                strM10767 = m10767(list);
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), f19430));
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception unused) {
        }
        try {
            bufferedWriter.write(strM10767);
            bufferedWriter.flush();
            ᵕˏᵛ.ˇʼʤˎ(bufferedWriter);
        } catch (Exception unused2) {
            bufferedWriter2 = bufferedWriter;
            m10768(file);
            ᵕˏᵛ.ˇʼʤˎ(bufferedWriter2);
        } catch (Throwable th2) {
            th = th2;
            bufferedWriter2 = bufferedWriter;
            ᵕˏᵛ.ˇʼʤˎ(bufferedWriter2);
            throw th;
        }
    }

    /* JADX WARN: Type inference failed for: r8v2, types: [int] */
    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public final String m10774(String str) throws Throwable {
        FileInputStream fileInputStream;
        File file = this.f19431.ᵻᵢﹳᵰ(str, "user-data");
        Closeable closeable = null;
        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) {
                            m10768(file);
                            ᵕˏᵛ.ˇʼʤˎ(fileInputStream);
                            return null;
                        }
                    } catch (Exception unused2) {
                        fileInputStream = null;
                    } catch (Throwable th) {
                        th = th;
                        ᵕˏᵛ.ˇʼʤˎ(closeable);
                        throw th;
                    }
                }
            } catch (Throwable th2) {
                th = th2;
                closeable = r8;
            }
        }
        Log.isLoggable("FirebaseCrashlytics", 3);
        m10768(file);
        return null;
    }
}
