package defpackage;

import android.net.TrafficStats;
import android.text.TextUtils;
import com.google.firebase.installations.FirebaseInstallationsException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONObject;

/* renamed from: ːⁱ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C2005 implements InterfaceC0466 {

    /* renamed from: ᵯٴᵦ, reason: contains not printable characters */
    public static final Object f8045 = new Object();

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public final C4446 f8046;

    /* renamed from: ˌᵜᵹᵩ, reason: contains not printable characters */
    public final ArrayList f8047;

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public final Object f8048;

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public final C2839 f8049;

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final ᵶˉᵹʣ f8050;

    /* renamed from: ᵝᵵᵺ, reason: contains not printable characters */
    public final ExecutorC5212 f8051;

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final C2288 f8052;

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final C3769 f8053;

    /* renamed from: ᵶᵷᵕ, reason: contains not printable characters */
    public String f8054;

    /* renamed from: ᵸˑ, reason: contains not printable characters */
    public final HashSet f8055;

    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public final ExecutorService f8056;

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public final C4240 f8057;

    static {
        new AtomicInteger(1);
    }

    public C2005(C2288 c2288, InterfaceC3290 interfaceC3290, ExecutorService executorService, ExecutorC5212 executorC5212) {
        c2288.m5993();
        C3769 c3769 = new C3769(c2288.f8947, interfaceC3290);
        ᵶˉᵹʣ r7 = new ᵶˉᵹʣ(8, c2288);
        if (ˊᵿʗ.ᵢˇᵭ == null) {
            ˊᵿʗ.ᵢˇᵭ = new ˊᵿʗ(10);
        }
        ˊᵿʗ r1 = ˊᵿʗ.ᵢˇᵭ;
        if (C4240.f15883 == null) {
            C4240.f15883 = new C4240(r1);
        }
        C4240 c4240 = C4240.f15883;
        C4446 c4446 = new C4446(new C3845(2, c2288));
        C2839 c2839 = new C2839();
        this.f8048 = new Object();
        this.f8055 = new HashSet();
        this.f8047 = new ArrayList();
        this.f8052 = c2288;
        this.f8053 = c3769;
        this.f8050 = r7;
        this.f8057 = c4240;
        this.f8046 = c4446;
        this.f8049 = c2839;
        this.f8056 = executorService;
        this.f8051 = executorC5212;
    }

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public final C3921 m5524() {
        m5526();
        C1353 c1353 = new C1353();
        m5530(new C3090(this.f8057, c1353));
        this.f8056.execute(new RunnableC3608(this, 1));
        return c1353.f5582;
    }

    /* renamed from: ˌᵜᵹᵩ, reason: contains not printable characters */
    public final synchronized void m5525(String str) {
        this.f8054 = str;
    }

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public final void m5526() {
        C2288 c2288 = this.f8052;
        c2288.m5993();
        ᵩʛʽ.ˎˏʜʾ(c2288.f8945.f15904, "Please set your Application ID. A valid Firebase App ID is required to communicate with Firebase server APIs: It identifies your application with Firebase.Please refer to https://firebase.google.com/support/privacy/init-options.");
        c2288.m5993();
        ᵩʛʽ.ˎˏʜʾ(c2288.f8945.f15900, "Please set your Project ID. A valid Firebase Project ID is required to communicate with Firebase server APIs: It identifies your application with Firebase.Please refer to https://firebase.google.com/support/privacy/init-options.");
        c2288.m5993();
        ᵩʛʽ.ˎˏʜʾ(c2288.f8945.f15903, "Please set a valid API key. A Firebase API key is required to communicate with Firebase server APIs: It authenticates your project with Google.Please refer to https://firebase.google.com/support/privacy/init-options.");
        c2288.m5993();
        String str = c2288.f8945.f15904;
        Pattern pattern = C4240.f15884;
        ᵩʛʽ.ˏᵙ("Please set your Application ID. A valid Firebase App ID is required to communicate with Firebase server APIs: It identifies your application with Firebase.Please refer to https://firebase.google.com/support/privacy/init-options.", str.contains(":"));
        c2288.m5993();
        ᵩʛʽ.ˏᵙ("Please set a valid API key. A Firebase API key is required to communicate with Firebase server APIs: It authenticates your project with Google.Please refer to https://firebase.google.com/support/privacy/init-options.", C4240.f15884.matcher(c2288.f8945.f15903).matches());
    }

    /* JADX WARN: Finally extract failed */
    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public final void m5527(C3686 c3686) {
        synchronized (f8045) {
            try {
                C2288 c2288 = this.f8052;
                c2288.m5993();
                ᵪᵨ r1 = ᵪᵨ.ᵴᵳ(c2288.f8947);
                try {
                    this.f8050.ᵡⁱᵥ(c3686);
                    if (r1 != null) {
                        r1.ᵡˎ();
                    }
                } catch (Throwable th) {
                    if (r1 != null) {
                        r1.ᵡˎ();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final C3686 m5528(C3686 c3686) throws FirebaseInstallationsException {
        int responseCode;
        C4948 c4948M8423;
        C2288 c2288 = this.f8052;
        c2288.m5993();
        String str = c2288.f8945.f15903;
        String str2 = c3686.f13722;
        c2288.m5993();
        String str3 = c2288.f8945.f15900;
        String str4 = c3686.f13724;
        C3769 c3769 = this.f8053;
        C4395 c4395 = c3769.f14087;
        if (!c4395.m9428()) {
            throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlM8425 = C3769.m8425("projects/" + str3 + "/installations/" + str2 + "/authTokens:generate");
        for (int i = 0; i <= 1; i++) {
            TrafficStats.setThreadStatsTag(32771);
            HttpURLConnection httpURLConnectionM8429 = c3769.m8429(urlM8425, str);
            try {
                try {
                    httpURLConnectionM8429.setRequestMethod("POST");
                    httpURLConnectionM8429.addRequestProperty("Authorization", "FIS_v2 " + str4);
                    httpURLConnectionM8429.setDoOutput(true);
                    C3769.m8427(httpURLConnectionM8429);
                    responseCode = httpURLConnectionM8429.getResponseCode();
                    c4395.m9427(responseCode);
                } finally {
                    httpURLConnectionM8429.disconnect();
                    TrafficStats.clearThreadStatsTag();
                }
            } catch (IOException | AssertionError unused) {
            }
            if (responseCode >= 200 && responseCode < 300) {
                c4948M8423 = C3769.m8423(httpURLConnectionM8429);
            } else {
                C3769.m8426(httpURLConnectionM8429, null);
                if (responseCode == 401 || responseCode == 404) {
                    C4782 c4782M10401 = C4948.m10401();
                    c4782M10401.f17765 = 3;
                    c4948M8423 = c4782M10401.m10102();
                } else {
                    if (responseCode == 429) {
                        throw new FirebaseInstallationsException("Firebase servers have received too many requests from this client in a short period of time. Please try again later.");
                    }
                    if (responseCode < 500 || responseCode >= 600) {
                        C4782 c4782M104012 = C4948.m10401();
                        c4782M104012.f17765 = 2;
                        c4948M8423 = c4782M104012.m10102();
                    }
                }
            }
            int iM4088 = AbstractC1327.m4088(c4948M8423.f18411);
            if (iM4088 == 0) {
                String str5 = c4948M8423.f18412;
                long j = c4948M8423.f18413;
                this.f8057.f15885.getClass();
                long jCurrentTimeMillis = System.currentTimeMillis() / 1000;
                C4032 c4032M8278 = c3686.m8278();
                c4032M8278.f15136 = str5;
                c4032M8278.f15133 = Long.valueOf(j);
                c4032M8278.f15135 = Long.valueOf(jCurrentTimeMillis);
                return c4032M8278.m8877();
            }
            if (iM4088 == 1) {
                C4032 c4032M82782 = c3686.m8278();
                c4032M82782.f15134 = "BAD CONFIG";
                c4032M82782.f15138 = 5;
                return c4032M82782.m8877();
            }
            if (iM4088 != 2) {
                throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
            }
            m5525(null);
            C4032 c4032M82783 = c3686.m8278();
            c4032M82783.f15138 = 2;
            return c4032M82783.m8877();
        }
        throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
    }

    /* renamed from: ᵝᵵᵺ, reason: contains not printable characters */
    public final C3686 m5529(C3686 c3686) throws FirebaseInstallationsException {
        int responseCode;
        C2637 c2637M8421;
        String str = c3686.f13722;
        String string = null;
        if (str != null && str.length() == 11) {
            C4252 c4252 = (C4252) this.f8046.get();
            synchronized (c4252.f15922) {
                try {
                    String[] strArr = C4252.f15921;
                    int i = 0;
                    while (true) {
                        if (i < 4) {
                            String str2 = strArr[i];
                            String string2 = c4252.f15922.getString("|T|" + c4252.f15923 + "|" + str2, null);
                            if (string2 == null || string2.isEmpty()) {
                                i++;
                            } else if (string2.startsWith("{")) {
                                try {
                                    string = new JSONObject(string2).getString("token");
                                } catch (JSONException unused) {
                                }
                            } else {
                                string = string2;
                            }
                        }
                    }
                } finally {
                }
            }
        }
        C3769 c3769 = this.f8053;
        C2288 c2288 = this.f8052;
        c2288.m5993();
        String str3 = c2288.f8945.f15903;
        String str4 = c3686.f13722;
        C2288 c22882 = this.f8052;
        c22882.m5993();
        String str5 = c22882.f8945.f15900;
        C2288 c22883 = this.f8052;
        c22883.m5993();
        String str6 = c22883.f8945.f15904;
        C4395 c4395 = c3769.f14087;
        if (!c4395.m9428()) {
            throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlM8425 = C3769.m8425("projects/" + str5 + "/installations");
        for (int i2 = 0; i2 <= 1; i2++) {
            TrafficStats.setThreadStatsTag(32769);
            HttpURLConnection httpURLConnectionM8429 = c3769.m8429(urlM8425, str3);
            try {
                try {
                    httpURLConnectionM8429.setRequestMethod("POST");
                    httpURLConnectionM8429.setDoOutput(true);
                    if (string != null) {
                        httpURLConnectionM8429.addRequestProperty("x-goog-fis-android-iid-migration-auth", string);
                    }
                    C3769.m8422(httpURLConnectionM8429, str4, str6);
                    responseCode = httpURLConnectionM8429.getResponseCode();
                    c4395.m9427(responseCode);
                } finally {
                    httpURLConnectionM8429.disconnect();
                    TrafficStats.clearThreadStatsTag();
                }
            } catch (IOException | AssertionError unused2) {
            }
            if (responseCode >= 200 && responseCode < 300) {
                c2637M8421 = C3769.m8421(httpURLConnectionM8429);
                httpURLConnectionM8429.disconnect();
                TrafficStats.clearThreadStatsTag();
            } else {
                C3769.m8426(httpURLConnectionM8429, str6);
                if (responseCode == 429) {
                    throw new FirebaseInstallationsException("Firebase servers have received too many requests from this client in a short period of time. Please try again later.");
                }
                if (responseCode < 500 || responseCode >= 600) {
                    C2637 c2637 = new C2637(null, null, null, null, 2);
                    httpURLConnectionM8429.disconnect();
                    TrafficStats.clearThreadStatsTag();
                    c2637M8421 = c2637;
                }
                httpURLConnectionM8429.disconnect();
                TrafficStats.clearThreadStatsTag();
            }
            int iM4088 = AbstractC1327.m4088(c2637M8421.f10221);
            if (iM4088 != 0) {
                if (iM4088 != 1) {
                    throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
                }
                C4032 c4032M8278 = c3686.m8278();
                c4032M8278.f15134 = "BAD CONFIG";
                c4032M8278.f15138 = 5;
                return c4032M8278.m8877();
            }
            String str7 = c2637M8421.f10224;
            String str8 = c2637M8421.f10222;
            this.f8057.f15885.getClass();
            long jCurrentTimeMillis = System.currentTimeMillis() / 1000;
            C4948 c4948 = c2637M8421.f10225;
            String str9 = c4948.f18412;
            long j = c4948.f18413;
            C4032 c4032M82782 = c3686.m8278();
            c4032M82782.f15137 = str7;
            c4032M82782.f15138 = 4;
            c4032M82782.f15136 = str9;
            c4032M82782.f15139 = str8;
            c4032M82782.f15133 = Long.valueOf(j);
            c4032M82782.f15135 = Long.valueOf(jCurrentTimeMillis);
            return c4032M82782.m8877();
        }
        throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final void m5530(InterfaceC2913 interfaceC2913) {
        synchronized (this.f8048) {
            this.f8047.add(interfaceC2913);
        }
    }

    /* renamed from: ᵯٴᵦ, reason: contains not printable characters */
    public final synchronized void m5531(C3686 c3686, C3686 c36862) {
        try {
            if (this.f8055.size() != 0 && !TextUtils.equals(c3686.f13722, c36862.f13722)) {
                Iterator it = this.f8055.iterator();
                if (it.hasNext()) {
                    if (it.next() != null) {
                        throw new ClassCastException();
                    }
                    throw null;
                }
            }
        } finally {
        }
    }

    /* JADX WARN: Finally extract failed */
    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final void m5532() {
        C3686 c3686M8877;
        int i;
        synchronized (f8045) {
            try {
                C2288 c2288 = this.f8052;
                c2288.m5993();
                ᵪᵨ r1 = ᵪᵨ.ᵴᵳ(c2288.f8947);
                try {
                    c3686M8877 = this.f8050.ᐧﾞ();
                    int i2 = c3686M8877.f13723;
                    i = 2;
                    boolean z = true;
                    if (i2 != 2 && i2 != 1) {
                        z = false;
                    }
                    if (z) {
                        String strM5535 = m5535(c3686M8877);
                        ᵶˉᵹʣ r5 = this.f8050;
                        C4032 c4032M8278 = c3686M8877.m8278();
                        c4032M8278.f15137 = strM5535;
                        c4032M8278.f15138 = 3;
                        c3686M8877 = c4032M8278.m8877();
                        r5.ᵡⁱᵥ(c3686M8877);
                    }
                    if (r1 != null) {
                        r1.ᵡˎ();
                    }
                } catch (Throwable th) {
                    if (r1 != null) {
                        r1.ᵡˎ();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
        m5534(c3686M8877);
        this.f8051.execute(new RunnableC3608(this, i));
    }

    /* renamed from: ᵶᵷᵕ, reason: contains not printable characters */
    public final void m5533(Exception exc) {
        synchronized (this.f8048) {
            try {
                Iterator it = this.f8047.iterator();
                while (it.hasNext()) {
                    if (((InterfaceC2913) it.next()).mo4556(exc)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* renamed from: ᵸˑ, reason: contains not printable characters */
    public final void m5534(C3686 c3686) {
        synchronized (this.f8048) {
            try {
                Iterator it = this.f8047.iterator();
                while (it.hasNext()) {
                    if (((InterfaceC2913) it.next()).mo4557(c3686)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:6:0x001e  */
    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final java.lang.String m5535(defpackage.C3686 r3) {
        /*
            r2 = this;
            ᵕˎˊ r0 = r2.f8052
            r0.m5993()
            java.lang.String r0 = r0.f8948
            java.lang.String r1 = "CHIME_ANDROID_SDK"
            boolean r0 = r0.equals(r1)
            if (r0 != 0) goto L1e
            ᵕˎˊ r0 = r2.f8052
            java.lang.String r1 = "[DEFAULT]"
            r0.m5993()
            java.lang.String r0 = r0.f8948
            boolean r0 = r1.equals(r0)
            if (r0 == 0) goto L50
        L1e:
            int r3 = r3.f13723
            r0 = 1
            if (r3 != r0) goto L50
            ᵱᵽˎᵟ r3 = r2.f8046
            java.lang.Object r3 = r3.get()
            ᵯʠᵲ r3 = (defpackage.C4252) r3
            android.content.SharedPreferences r0 = r3.f15922
            monitor-enter(r0)
            java.lang.String r1 = r3.m9197()     // Catch: java.lang.Throwable -> L36
            if (r1 == 0) goto L38
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L36
            goto L3d
        L36:
            r2 = move-exception
            goto L4e
        L38:
            java.lang.String r1 = r3.m9198()     // Catch: java.lang.Throwable -> L36
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L36
        L3d:
            boolean r3 = android.text.TextUtils.isEmpty(r1)
            if (r3 == 0) goto L4d
            ᵜᵞᵱˊ r2 = r2.f8049
            r2.getClass()
            java.lang.String r2 = defpackage.C2839.m6943()
            return r2
        L4d:
            return r1
        L4e:
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L36
            throw r2
        L50:
            ᵜᵞᵱˊ r2 = r2.f8049
            r2.getClass()
            java.lang.String r2 = defpackage.C2839.m6943()
            return r2
        */
        throw new UnsupportedOperationException("Method not decompiled: defpackage.C2005.m5535(ᵧᵺ):java.lang.String");
    }

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public final C3921 m5536() {
        String str;
        m5526();
        synchronized (this) {
            str = this.f8054;
        }
        if (str != null) {
            return ʼٴ.ˑᵥˁ(str);
        }
        C1353 c1353 = new C1353();
        m5530(new C1587(c1353));
        C3921 c3921 = c1353.f5582;
        this.f8056.execute(new RunnableC3608(this, 0));
        return c3921;
    }
}
