package a3;

import L1.A;
import N2.o;
import O2.k;
import android.net.TrafficStats;
import android.util.Log;
import b3.C0330a;
import b3.C0331b;
import c3.C0339a;
import c3.C0340b;
import c3.C0341c;
import com.google.android.gms.internal.measurement.C1;
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.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONObject;
import s1.z;

/* JADX INFO: renamed from: a3.c, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0231c implements InterfaceC0232d {

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static final Object f3611m = new Object();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final I2.g f3612a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final C0341c f3613b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final io.sentry.internal.debugmeta.c f3614c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final j f3615d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final o f3616e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final h f3617f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final Object f3618g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final ExecutorService f3619h;
    public final k i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public String f3620j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final HashSet f3621k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final ArrayList f3622l;

    static {
        new AtomicInteger(1);
    }

    public C0231c(I2.g gVar, Z2.a aVar, ExecutorService executorService, k kVar) {
        gVar.a();
        C0341c c0341c = new C0341c(gVar.f932a, aVar);
        io.sentry.internal.debugmeta.c cVar = new io.sentry.internal.debugmeta.c(10, gVar);
        if (A.f1275B == null) {
            A.f1275B = new A(24);
        }
        A a5 = A.f1275B;
        if (j.f3630d == null) {
            j.f3630d = new j(a5);
        }
        j jVar = j.f3630d;
        o oVar = new o(new N2.e(2, gVar));
        h hVar = new h();
        this.f3618g = new Object();
        this.f3621k = new HashSet();
        this.f3622l = new ArrayList();
        this.f3612a = gVar;
        this.f3613b = c0341c;
        this.f3614c = cVar;
        this.f3615d = jVar;
        this.f3616e = oVar;
        this.f3617f = hVar;
        this.f3619h = executorService;
        this.i = kVar;
    }

    /* JADX WARN: Finally extract failed */
    public final void a() {
        C0331b c0331bD;
        synchronized (f3611m) {
            try {
                I2.g gVar = this.f3612a;
                gVar.a();
                io.sentry.internal.debugmeta.c cVarD = io.sentry.internal.debugmeta.c.d(gVar.f932a);
                try {
                    c0331bD = this.f3614c.D();
                    int i = c0331bD.f4699b;
                    boolean z5 = true;
                    if (i != 2 && i != 1) {
                        z5 = false;
                    }
                    if (z5) {
                        String strF = f(c0331bD);
                        io.sentry.internal.debugmeta.c cVar = this.f3614c;
                        C0330a c0330aA = c0331bD.a();
                        c0330aA.f4689a = strF;
                        c0330aA.f4690b = 3;
                        c0331bD = c0330aA.a();
                        cVar.t(c0331bD);
                    }
                    if (cVarD != null) {
                        cVarD.E();
                    }
                } catch (Throwable th) {
                    if (cVarD != null) {
                        cVarD.E();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
        i(c0331bD);
        this.i.execute(new RunnableC0230b(this, 1));
    }

    public final C0331b b(C0331b c0331b) throws C0233e {
        String str;
        int responseCode;
        String str2;
        C0340b c0340b;
        C0340b c0340bF;
        I2.g gVar = this.f3612a;
        gVar.a();
        String str3 = gVar.f934c.f946a;
        String str4 = c0331b.f4698a;
        I2.g gVar2 = this.f3612a;
        gVar2.a();
        String str5 = gVar2.f934c.f952g;
        String str6 = c0331b.f4701d;
        C0341c c0341c = this.f3613b;
        c3.d dVar = c0341c.f4736c;
        String str7 = "Firebase Installations Service is unavailable. Please try again later.";
        if (!dVar.a()) {
            throw new C0233e("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlA = C0341c.a("projects/" + str5 + "/installations/" + str4 + "/authTokens:generate");
        int i = 0;
        while (i <= 1) {
            TrafficStats.setThreadStatsTag(32771);
            HttpURLConnection httpURLConnectionC = c0341c.c(urlA, str3);
            try {
                try {
                    httpURLConnectionC.setRequestMethod("POST");
                    httpURLConnectionC.addRequestProperty("Authorization", "FIS_v2 " + str6);
                    httpURLConnectionC.setDoOutput(true);
                    C0341c.h(httpURLConnectionC);
                    responseCode = httpURLConnectionC.getResponseCode();
                    dVar.b(responseCode);
                } catch (IOException | AssertionError unused) {
                    str = str7;
                }
                if (responseCode >= 200 && responseCode < 300) {
                    c0340bF = C0341c.f(httpURLConnectionC);
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                    str2 = str7;
                } else {
                    C0341c.b(httpURLConnectionC, null, str3, str5);
                    str2 = str7;
                    if (responseCode == 401 || responseCode == 404) {
                        try {
                            if (((byte) (0 | 1)) == 1) {
                                c0340b = new C0340b(0L, null, 3);
                            } else {
                                str = str2;
                                try {
                                    throw new IllegalStateException("Missing required properties: tokenExpirationTimestamp");
                                } catch (IOException | AssertionError unused2) {
                                    i++;
                                    str7 = str;
                                }
                            }
                        } catch (IOException | AssertionError unused3) {
                            str = str2;
                        }
                    } else {
                        if (responseCode == 429) {
                            throw new C0233e("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) {
                            Log.e("Firebase-Installations", "Firebase Installations can not communicate with Firebase server APIs due to invalid configuration. Please update your Firebase initialization process and set valid Firebase options (API key, Project ID, Application ID) when initializing Firebase.");
                            if (((byte) (0 | 1)) != 1) {
                                throw new IllegalStateException("Missing required properties: tokenExpirationTimestamp");
                            }
                            c0340b = new C0340b(0L, null, 2);
                        } else {
                            httpURLConnectionC.disconnect();
                            TrafficStats.clearThreadStatsTag();
                            str = str2;
                            i++;
                            str7 = str;
                        }
                    }
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                    c0340bF = c0340b;
                }
                int iC = u.e.c(c0340bF.f4731c);
                if (iC != 0) {
                    if (iC == 1) {
                        C0330a c0330aA = c0331b.a();
                        c0330aA.f4695g = "BAD CONFIG";
                        c0330aA.f4690b = 5;
                        return c0330aA.a();
                    }
                    if (iC != 2) {
                        throw new C0233e(str2);
                    }
                    synchronized (this) {
                        this.f3620j = null;
                    }
                    C0330a c0330aA2 = c0331b.a();
                    c0330aA2.f4690b = 2;
                    return c0330aA2.a();
                }
                j jVar = this.f3615d;
                jVar.getClass();
                TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                jVar.f3631a.getClass();
                long seconds = timeUnit.toSeconds(System.currentTimeMillis());
                C0330a c0330aA3 = c0331b.a();
                c0330aA3.f4691c = c0340bF.f4729a;
                c0330aA3.f4693e = c0340bF.f4730b;
                byte b2 = (byte) (c0330aA3.f4696h | 1);
                c0330aA3.f4694f = seconds;
                c0330aA3.f4696h = (byte) (b2 | 2);
                return c0330aA3.a();
            } finally {
                httpURLConnectionC.disconnect();
                TrafficStats.clearThreadStatsTag();
            }
        }
        throw new C0233e(str7);
    }

    public final P1.o c() {
        String str;
        e();
        synchronized (this) {
            str = this.f3620j;
        }
        if (str != null) {
            return C1.l(str);
        }
        P1.i iVar = new P1.i();
        C0235g c0235g = new C0235g(iVar);
        synchronized (this.f3618g) {
            this.f3622l.add(c0235g);
        }
        P1.o oVar = iVar.f2505a;
        this.f3619h.execute(new RunnableC0230b(this, 0));
        return oVar;
    }

    public final P1.o d() {
        e();
        P1.i iVar = new P1.i();
        C0234f c0234f = new C0234f(this.f3615d, iVar);
        synchronized (this.f3618g) {
            this.f3622l.add(c0234f);
        }
        this.f3619h.execute(new RunnableC0230b(this, 2));
        return iVar.f2505a;
    }

    public final void e() {
        I2.g gVar = this.f3612a;
        gVar.a();
        z.e(gVar.f934c.f947b, "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.");
        gVar.a();
        z.e(gVar.f934c.f952g, "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.");
        gVar.a();
        z.e(gVar.f934c.f946a, "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.");
        gVar.a();
        String str = gVar.f934c.f947b;
        Pattern pattern = j.f3629c;
        z.a("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(":"));
        gVar.a();
        z.a("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.", j.f3629c.matcher(gVar.f934c.f946a).matches());
    }

    /* JADX WARN: Removed duplicated region for block: B:6:0x001e  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public final java.lang.String f(b3.C0331b r6) {
        /*
            r5 = this;
            I2.g r0 = r5.f3612a
            r0.a()
            java.lang.String r0 = r0.f933b
            java.lang.String r1 = "CHIME_ANDROID_SDK"
            boolean r0 = r0.equals(r1)
            if (r0 != 0) goto L1e
            I2.g r0 = r5.f3612a
            r0.a()
            java.lang.String r1 = "[DEFAULT]"
            java.lang.String r0 = r0.f933b
            boolean r0 = r1.equals(r0)
            if (r0 == 0) goto L5c
        L1e:
            r0 = 1
            int r6 = r6.f4699b
            if (r6 != r0) goto L5c
            N2.o r6 = r5.f3616e
            java.lang.Object r6 = r6.get()
            b3.c r6 = (b3.c) r6
            android.content.SharedPreferences r0 = r6.f4706a
            monitor-enter(r0)
            android.content.SharedPreferences r1 = r6.f4706a     // Catch: java.lang.Throwable -> L3f
            monitor-enter(r1)     // Catch: java.lang.Throwable -> L3f
            android.content.SharedPreferences r2 = r6.f4706a     // Catch: java.lang.Throwable -> L57
            java.lang.String r3 = "|S|id"
            r4 = 0
            java.lang.String r2 = r2.getString(r3, r4)     // Catch: java.lang.Throwable -> L57
            monitor-exit(r1)     // Catch: java.lang.Throwable -> L57
            if (r2 == 0) goto L41
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L3f
            goto L46
        L3f:
            r6 = move-exception
            goto L5a
        L41:
            java.lang.String r2 = r6.a()     // Catch: java.lang.Throwable -> L3f
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L3f
        L46:
            boolean r6 = android.text.TextUtils.isEmpty(r2)
            if (r6 == 0) goto L56
            a3.h r6 = r5.f3617f
            r6.getClass()
            java.lang.String r6 = a3.h.a()
            return r6
        L56:
            return r2
        L57:
            r6 = move-exception
            monitor-exit(r1)     // Catch: java.lang.Throwable -> L57
            throw r6     // Catch: java.lang.Throwable -> L3f
        L5a:
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L3f
            throw r6
        L5c:
            a3.h r6 = r5.f3617f
            r6.getClass()
            java.lang.String r6 = a3.h.a()
            return r6
        */
        throw new UnsupportedOperationException("Method not decompiled: a3.C0231c.f(b3.b):java.lang.String");
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r2v2, types: [c3.c] */
    /* JADX WARN: Type inference failed for: r2v25 */
    /* JADX WARN: Type inference failed for: r2v26 */
    /* JADX WARN: Type inference failed for: r2v27 */
    /* JADX WARN: Type inference failed for: r2v3 */
    /* JADX WARN: Type inference failed for: r2v4 */
    /* JADX WARN: Type inference failed for: r2v6 */
    /* JADX WARN: Type inference failed for: r2v7, types: [c3.a] */
    public final C0331b g(C0331b c0331b) throws C0233e {
        int responseCode;
        String str = c0331b.f4698a;
        String string = null;
        if (str != null && str.length() == 11) {
            b3.c cVar = (b3.c) this.f3616e.get();
            synchronized (cVar.f4706a) {
                try {
                    String[] strArr = b3.c.f4705c;
                    int i = 0;
                    while (true) {
                        if (i < 4) {
                            String str2 = strArr[i];
                            String string2 = cVar.f4706a.getString("|T|" + cVar.f4707b + "|" + 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 {
                }
            }
        }
        C0341c c0341c = this.f3613b;
        I2.g gVar = this.f3612a;
        gVar.a();
        String str3 = gVar.f934c.f946a;
        String str4 = c0331b.f4698a;
        I2.g gVar2 = this.f3612a;
        gVar2.a();
        String str5 = gVar2.f934c.f952g;
        I2.g gVar3 = this.f3612a;
        gVar3.a();
        String str6 = gVar3.f934c.f947b;
        c3.d dVar = c0341c.f4736c;
        if (!dVar.a()) {
            throw new C0233e("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlA = C0341c.a("projects/" + str5 + "/installations");
        int i5 = 0;
        C0339a c0339a = c0341c;
        while (i5 <= 1) {
            TrafficStats.setThreadStatsTag(32769);
            HttpURLConnection httpURLConnectionC = c0339a.c(urlA, str3);
            try {
                try {
                    httpURLConnectionC.setRequestMethod("POST");
                    httpURLConnectionC.setDoOutput(true);
                    if (string != null) {
                        httpURLConnectionC.addRequestProperty("x-goog-fis-android-iid-migration-auth", string);
                    }
                    C0341c.g(httpURLConnectionC, str4, str6);
                    responseCode = httpURLConnectionC.getResponseCode();
                    dVar.b(responseCode);
                } catch (IOException | AssertionError unused2) {
                }
                if (responseCode >= 200 && responseCode < 300) {
                    C0339a c0339aE = C0341c.e(httpURLConnectionC);
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                    c0339a = c0339aE;
                } else {
                    try {
                        C0341c.b(httpURLConnectionC, str6, str3, str5);
                    } catch (IOException | AssertionError unused3) {
                        httpURLConnectionC.disconnect();
                        TrafficStats.clearThreadStatsTag();
                    }
                    if (responseCode == 429) {
                        throw new C0233e("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) {
                        Log.e("Firebase-Installations", "Firebase Installations can not communicate with Firebase server APIs due to invalid configuration. Please update your Firebase initialization process and set valid Firebase options (API key, Project ID, Application ID) when initializing Firebase.");
                        C0339a c0339a2 = new C0339a(null, null, null, null, 2);
                        httpURLConnectionC.disconnect();
                        TrafficStats.clearThreadStatsTag();
                        c0339a = c0339a2;
                    } else {
                        httpURLConnectionC.disconnect();
                        TrafficStats.clearThreadStatsTag();
                        i5++;
                        c0339a = c0339a;
                    }
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                    i5++;
                    c0339a = c0339a;
                }
                int iC = u.e.c(c0339a.f4728e);
                if (iC != 0) {
                    if (iC != 1) {
                        throw new C0233e("Firebase Installations Service is unavailable. Please try again later.");
                    }
                    C0330a c0330aA = c0331b.a();
                    c0330aA.f4695g = "BAD CONFIG";
                    c0330aA.f4690b = 5;
                    return c0330aA.a();
                }
                String str7 = c0339a.f4725b;
                String str8 = c0339a.f4726c;
                j jVar = this.f3615d;
                jVar.getClass();
                TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                jVar.f3631a.getClass();
                long seconds = timeUnit.toSeconds(System.currentTimeMillis());
                C0340b c0340b = c0339a.f4727d;
                String str9 = c0340b.f4729a;
                long j5 = c0340b.f4730b;
                C0330a c0330aA2 = c0331b.a();
                c0330aA2.f4689a = str7;
                c0330aA2.f4690b = 4;
                c0330aA2.f4691c = str9;
                c0330aA2.f4692d = str8;
                c0330aA2.f4693e = j5;
                byte b2 = (byte) (c0330aA2.f4696h | 1);
                c0330aA2.f4694f = seconds;
                c0330aA2.f4696h = (byte) (b2 | 2);
                return c0330aA2.a();
            } finally {
                httpURLConnectionC.disconnect();
                TrafficStats.clearThreadStatsTag();
            }
        }
        throw new C0233e("Firebase Installations Service is unavailable. Please try again later.");
    }

    public final void h(Exception exc) {
        synchronized (this.f3618g) {
            try {
                Iterator it = this.f3622l.iterator();
                while (it.hasNext()) {
                    if (((i) it.next()).a(exc)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void i(C0331b c0331b) {
        synchronized (this.f3618g) {
            try {
                Iterator it = this.f3622l.iterator();
                while (it.hasNext()) {
                    if (((i) it.next()).b(c0331b)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
