package E4;

import A1.H;
import P2.C0253j;
import P2.w;
import android.net.TrafficStats;
import android.text.TextUtils;
import android.util.Log;
import d3.R0;
import d4.l;
import g3.o;
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 u.AbstractC1563h;

/* JADX INFO: loaded from: classes.dex */
public final class d implements e {

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final W3.f f2189a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final G4.c f2190b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final R0 f2191c;

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final ExecutorService f2196h;
    public final e4.l i;

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

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

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

    static {
        new AtomicInteger(1);
    }

    public d(W3.f fVar, D4.b bVar, ExecutorService executorService, e4.l lVar) {
        fVar.a();
        G4.c cVar = new G4.c(fVar.f7113a, bVar);
        R0 r02 = new R0(fVar);
        if (C0253j.f5414s == null) {
            C0253j.f5414s = new C0253j(5);
        }
        C0253j c0253j = C0253j.f5414s;
        if (k.f2207d == null) {
            k.f2207d = new k(c0253j);
        }
        k kVar = k.f2207d;
        l lVar2 = new l(new b(0, fVar));
        i iVar = new i();
        this.f2195g = new Object();
        this.f2198k = new HashSet();
        this.f2199l = new ArrayList();
        this.f2189a = fVar;
        this.f2190b = cVar;
        this.f2191c = r02;
        this.f2192d = kVar;
        this.f2193e = lVar2;
        this.f2194f = iVar;
        this.f2196h = executorService;
        this.i = lVar;
    }

    public static d e() {
        return (d) W3.f.c().b(e.class);
    }

    public final void a(j jVar) {
        synchronized (this.f2195g) {
            this.f2199l.add(jVar);
        }
    }

    /* JADX WARN: Finally extract failed */
    public final void b() {
        F4.b bVarZ;
        synchronized (f2188m) {
            try {
                W3.f fVar = this.f2189a;
                fVar.a();
                R0 r0E = R0.E(fVar.f7113a);
                try {
                    bVarZ = this.f2191c.Z();
                    int i = bVarZ.f2502b;
                    boolean z8 = true;
                    if (i != 2 && i != 1) {
                        z8 = false;
                    }
                    if (z8) {
                        String strI = i(bVarZ);
                        R0 r02 = this.f2191c;
                        F4.a aVarA = bVarZ.a();
                        aVarA.f2495b = strI;
                        aVarA.f2494a = 3;
                        bVarZ = aVarA.i();
                        r02.R(bVarZ);
                    }
                    if (r0E != null) {
                        r0E.a0();
                    }
                } catch (Throwable th) {
                    if (r0E != null) {
                        r0E.a0();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
        l(bVarZ);
        this.i.execute(new c(this, 2));
    }

    public final F4.b c(F4.b bVar) throws f {
        int responseCode;
        G4.b bVarF;
        W3.f fVar = this.f2189a;
        fVar.a();
        String str = fVar.f7115c.f7127a;
        fVar.a();
        String str2 = fVar.f7115c.f7133g;
        String str3 = bVar.f2504d;
        G4.c cVar = this.f2190b;
        G4.d dVar = cVar.f2970c;
        if (!dVar.b()) {
            throw new f("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlA = G4.c.a("projects/" + str2 + "/installations/" + bVar.f2501a + "/authTokens:generate");
        for (int i = 0; i <= 1; i++) {
            TrafficStats.setThreadStatsTag(32771);
            HttpURLConnection httpURLConnectionC = cVar.c(urlA, str);
            try {
                try {
                    httpURLConnectionC.setRequestMethod("POST");
                    httpURLConnectionC.addRequestProperty("Authorization", "FIS_v2 " + str3);
                    httpURLConnectionC.setDoOutput(true);
                    G4.c.h(httpURLConnectionC);
                    responseCode = httpURLConnectionC.getResponseCode();
                    dVar.d(responseCode);
                } catch (Throwable th) {
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                    throw th;
                }
            } catch (IOException | AssertionError unused) {
            }
            if (responseCode >= 200 && responseCode < 300) {
                bVarF = G4.c.f(httpURLConnectionC);
            } else {
                G4.c.b(httpURLConnectionC, null, str, str2);
                if (responseCode == 401 || responseCode == 404) {
                    H hA = G4.b.a();
                    hA.f65r = 3;
                    bVarF = hA.c();
                } else {
                    if (responseCode == 429) {
                        throw new f("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.");
                        H hA2 = G4.b.a();
                        hA2.f65r = 2;
                        bVarF = hA2.c();
                    }
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                }
            }
            httpURLConnectionC.disconnect();
            TrafficStats.clearThreadStatsTag();
            int iC = AbstractC1563h.c(bVarF.f2965c);
            if (iC == 0) {
                k kVar = this.f2192d;
                kVar.getClass();
                TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                kVar.f2208a.getClass();
                long seconds = timeUnit.toSeconds(System.currentTimeMillis());
                F4.a aVarA = bVar.a();
                aVarA.f2496c = bVarF.f2963a;
                aVarA.f2499f = Long.valueOf(bVarF.f2964b);
                aVarA.f2500g = Long.valueOf(seconds);
                return aVarA.i();
            }
            if (iC == 1) {
                F4.a aVarA2 = bVar.a();
                aVarA2.f2498e = "BAD CONFIG";
                aVarA2.f2494a = 5;
                return aVarA2.i();
            }
            if (iC != 2) {
                throw new f("Firebase Installations Service is unavailable. Please try again later.");
            }
            m(null);
            F4.a aVarA3 = bVar.a();
            aVarA3.f2494a = 2;
            return aVarA3.i();
        }
        throw new f("Firebase Installations Service is unavailable. Please try again later.");
    }

    public final o d() {
        String str;
        h();
        synchronized (this) {
            str = this.f2197j;
        }
        if (str != null) {
            return W2.g.p(str);
        }
        g3.h hVar = new g3.h();
        a(new h(hVar));
        o oVar = hVar.f12923a;
        this.f2196h.execute(new c(this, 0));
        return oVar;
    }

    public final o f() {
        h();
        g3.h hVar = new g3.h();
        a(new g(this.f2192d, hVar));
        this.f2196h.execute(new c(this, 1));
        return hVar.f12923a;
    }

    /* JADX WARN: Finally extract failed */
    public final void g(F4.b bVar) {
        synchronized (f2188m) {
            try {
                W3.f fVar = this.f2189a;
                fVar.a();
                R0 r0E = R0.E(fVar.f7113a);
                try {
                    this.f2191c.R(bVar);
                    if (r0E != null) {
                        r0E.a0();
                    }
                } catch (Throwable th) {
                    if (r0E != null) {
                        r0E.a0();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }

    public final void h() {
        W3.f fVar = this.f2189a;
        fVar.a();
        w.d(fVar.f7115c.f7128b, "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.");
        fVar.a();
        w.d(fVar.f7115c.f7133g, "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.");
        fVar.a();
        w.d(fVar.f7115c.f7127a, "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.");
        fVar.a();
        String str = fVar.f7115c.f7128b;
        Pattern pattern = k.f2206c;
        if (!str.contains(":")) {
            throw new IllegalArgumentException("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.");
        }
        fVar.a();
        if (!k.f2206c.matcher(fVar.f7115c.f7127a).matches()) {
            throw new IllegalArgumentException("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.");
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:6:0x001e  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String i(F4.b bVar) {
        String strA;
        W3.f fVar = this.f2189a;
        fVar.a();
        if (!fVar.f7114b.equals("CHIME_ANDROID_SDK")) {
            W3.f fVar2 = this.f2189a;
            fVar2.a();
            if ("[DEFAULT]".equals(fVar2.f7114b)) {
                if (bVar.f2502b == 1) {
                    F4.c cVar = (F4.c) this.f2193e.get();
                    synchronized (cVar.f2509a) {
                        try {
                            strA = cVar.a();
                            if (strA == null) {
                                strA = cVar.b();
                            }
                        } finally {
                        }
                    }
                    if (!TextUtils.isEmpty(strA)) {
                        return strA;
                    }
                    this.f2194f.getClass();
                    return i.a();
                }
            }
        }
        this.f2194f.getClass();
        return i.a();
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r2v2, types: [G4.c] */
    /* JADX WARN: Type inference failed for: r2v22 */
    /* JADX WARN: Type inference failed for: r2v23 */
    /* JADX WARN: Type inference failed for: r2v24 */
    /* 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: [G4.a] */
    public final F4.b j(F4.b bVar) throws f {
        int responseCode;
        String str = bVar.f2501a;
        String string = null;
        if (str != null && str.length() == 11) {
            F4.c cVar = (F4.c) this.f2193e.get();
            synchronized (cVar.f2509a) {
                try {
                    String[] strArr = F4.c.f2508c;
                    int i = 0;
                    while (true) {
                        if (i < 4) {
                            String str2 = strArr[i];
                            String string2 = cVar.f2509a.getString("|T|" + cVar.f2510b + "|" + 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 {
                }
            }
        }
        G4.c cVar2 = this.f2190b;
        W3.f fVar = this.f2189a;
        fVar.a();
        String str3 = fVar.f7115c.f7127a;
        String str4 = bVar.f2501a;
        W3.f fVar2 = this.f2189a;
        fVar2.a();
        String str5 = fVar2.f7115c.f7133g;
        W3.f fVar3 = this.f2189a;
        fVar3.a();
        String str6 = fVar3.f7115c.f7128b;
        G4.d dVar = cVar2.f2970c;
        if (!dVar.b()) {
            throw new f("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlA = G4.c.a("projects/" + str5 + "/installations");
        int i5 = 0;
        G4.a aVar = cVar2;
        while (i5 <= 1) {
            TrafficStats.setThreadStatsTag(32769);
            HttpURLConnection httpURLConnectionC = aVar.c(urlA, str3);
            try {
                try {
                    httpURLConnectionC.setRequestMethod("POST");
                    httpURLConnectionC.setDoOutput(true);
                    if (string != null) {
                        httpURLConnectionC.addRequestProperty("x-goog-fis-android-iid-migration-auth", string);
                    }
                    G4.c.g(httpURLConnectionC, str4, str6);
                    responseCode = httpURLConnectionC.getResponseCode();
                    dVar.d(responseCode);
                } catch (IOException | AssertionError unused2) {
                }
                if (responseCode >= 200 && responseCode < 300) {
                    G4.a aVarE = G4.c.e(httpURLConnectionC);
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                    aVar = aVarE;
                } else {
                    try {
                        G4.c.b(httpURLConnectionC, str6, str3, str5);
                    } catch (IOException | AssertionError unused3) {
                        httpURLConnectionC.disconnect();
                        TrafficStats.clearThreadStatsTag();
                    }
                    if (responseCode == 429) {
                        throw new f("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.");
                        G4.a aVar2 = new G4.a(null, null, null, null, 2);
                        httpURLConnectionC.disconnect();
                        TrafficStats.clearThreadStatsTag();
                        aVar = aVar2;
                    } else {
                        httpURLConnectionC.disconnect();
                        TrafficStats.clearThreadStatsTag();
                        i5++;
                        aVar = aVar;
                    }
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                    i5++;
                    aVar = aVar;
                }
                int iC = AbstractC1563h.c(aVar.f2962e);
                if (iC != 0) {
                    if (iC != 1) {
                        throw new f("Firebase Installations Service is unavailable. Please try again later.");
                    }
                    F4.a aVarA = bVar.a();
                    aVarA.f2498e = "BAD CONFIG";
                    aVarA.f2494a = 5;
                    return aVarA.i();
                }
                String str7 = aVar.f2959b;
                String str8 = aVar.f2960c;
                k kVar = this.f2192d;
                kVar.getClass();
                TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                kVar.f2208a.getClass();
                long seconds = timeUnit.toSeconds(System.currentTimeMillis());
                G4.b bVar2 = aVar.f2961d;
                String str9 = bVar2.f2963a;
                long j8 = bVar2.f2964b;
                F4.a aVarA2 = bVar.a();
                aVarA2.f2495b = str7;
                aVarA2.f2494a = 4;
                aVarA2.f2496c = str9;
                aVarA2.f2497d = str8;
                aVarA2.f2499f = Long.valueOf(j8);
                aVarA2.f2500g = Long.valueOf(seconds);
                return aVarA2.i();
            } finally {
                httpURLConnectionC.disconnect();
                TrafficStats.clearThreadStatsTag();
            }
        }
        throw new f("Firebase Installations Service is unavailable. Please try again later.");
    }

    public final void k(Exception exc) {
        synchronized (this.f2195g) {
            try {
                Iterator it = this.f2199l.iterator();
                while (it.hasNext()) {
                    if (((j) it.next()).b(exc)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void l(F4.b bVar) {
        synchronized (this.f2195g) {
            try {
                Iterator it = this.f2199l.iterator();
                while (it.hasNext()) {
                    if (((j) it.next()).a(bVar)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final synchronized void m(String str) {
        this.f2197j = str;
    }

    public final synchronized void n(F4.b bVar, F4.b bVar2) {
        try {
            if (this.f2198k.size() != 0 && !TextUtils.equals(bVar.f2501a, bVar2.f2501a)) {
                Iterator it = this.f2198k.iterator();
                if (it.hasNext()) {
                    if (it.next() != null) {
                        throw new ClassCastException();
                    }
                    throw null;
                }
            }
        } finally {
        }
    }
}
