package defpackage;

import android.net.TrafficStats;
import android.text.TextUtils;
import android.util.Log;
import androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import com.google.android.gms.common.internal.Preconditions;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.android.gms.tasks.Tasks;
import defpackage.a50;
import defpackage.g40;
import defpackage.g67;
import defpackage.h67;
import defpackage.qn4;
import defpackage.up9;
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.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public class k13 implements m13 {
    public static final Object m = new Object();
    public final y03 a;
    public final i13 b;
    public final g67 c;
    public final uda d;
    public final i65 e;
    public final lr7 f;
    public final Object g;
    public final ExecutorService h;
    public final Executor i;
    public String j;
    public final HashSet k;
    public final ArrayList l;

    public class a implements ThreadFactory {
        public final AtomicInteger a = new AtomicInteger(1);

        @Override // java.util.concurrent.ThreadFactory
        public final Thread newThread(Runnable runnable) {
            return new Thread(runnable, String.format("firebase-installations-executor-%d", Integer.valueOf(this.a.getAndIncrement())));
        }
    }

    public static /* synthetic */ class b {
        public static final /* synthetic */ int[] a;
        public static final /* synthetic */ int[] b;

        static {
            int[] iArr = new int[up9.b.values().length];
            b = iArr;
            try {
                iArr[0] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                b[1] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                b[2] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            int[] iArr2 = new int[qn4.b.values().length];
            a = iArr2;
            try {
                iArr2[0] = 1;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                a[1] = 2;
            } catch (NoSuchFieldError unused5) {
            }
        }
    }

    static {
        new a();
    }

    public k13(y03 y03Var, gl7 gl7Var, ExecutorService executorService, Executor executor) {
        y03Var.a();
        i13 i13Var = new i13(y03Var.a, gl7Var);
        g67 g67Var = new g67(y03Var);
        if (zh9.a == null) {
            zh9.a = new zh9();
        }
        zh9 zh9Var = zh9.a;
        if (uda.d == null) {
            uda.d = new uda(zh9Var);
        }
        uda udaVar = uda.d;
        i65 i65Var = new i65(new q91(y03Var, 2));
        lr7 lr7Var = new lr7();
        this.g = new Object();
        this.k = new HashSet();
        this.l = new ArrayList();
        this.a = y03Var;
        this.b = i13Var;
        this.c = g67Var;
        this.d = udaVar;
        this.e = i65Var;
        this.f = lr7Var;
        this.h = executorService;
        this.i = executor;
    }

    public static k13 d() {
        y03 y03VarC = y03.c();
        Preconditions.checkArgument(true, "Null is not a valid value of FirebaseApp.");
        return (k13) y03VarC.b(m13.class);
    }

    @Override // defpackage.m13
    public final Task a() {
        e();
        TaskCompletionSource taskCompletionSource = new TaskCompletionSource();
        au3 au3Var = new au3(this.d, taskCompletionSource);
        synchronized (this.g) {
            this.l.add(au3Var);
        }
        Task task = taskCompletionSource.getTask();
        this.h.execute(new j13(this, 2));
        return task;
    }

    /* JADX WARN: Finally extract failed */
    public final void b() {
        h67 h67VarC;
        synchronized (m) {
            try {
                y03 y03Var = this.a;
                y03Var.a();
                hn1 hn1VarA = hn1.a(y03Var.a);
                try {
                    h67VarC = this.c.c();
                    if (h67VarC.f() == g67.a.b || h67VarC.f() == g67.a.a) {
                        String strF = f(h67VarC);
                        g67 g67Var = this.c;
                        h67.a aVarH = h67VarC.h();
                        aVarH.d(strF);
                        aVarH.f(g67.a.c);
                        h67VarC = aVarH.a();
                        g67Var.b(h67VarC);
                    }
                    if (hn1VarA != null) {
                        hn1VarA.b();
                    }
                } catch (Throwable th) {
                    if (hn1VarA != null) {
                        hn1VarA.b();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
        i(h67VarC);
        this.i.execute(new j13(this, 0));
    }

    /* JADX WARN: Code restructure failed: missing block: B:36:0x00d5, code lost:
    
        r9 = (a50.b) defpackage.up9.a();
        r9.c = up9.b.c;
        r0 = r9.a();
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final h67 c(h67 h67Var) {
        int responseCode;
        up9 up9VarF;
        i13 i13Var = this.b;
        y03 y03Var = this.a;
        y03Var.a();
        String str = y03Var.c.a;
        String strC = h67Var.c();
        y03 y03Var2 = this.a;
        y03Var2.a();
        String str2 = y03Var2.c.g;
        String strE = h67Var.e();
        y08 y08Var = i13Var.c;
        if (!y08Var.a()) {
            throw new n13("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlA = i13.a("projects/" + str2 + "/installations/" + strC + "/authTokens:generate");
        int i = 0;
        while (true) {
            boolean z = true;
            if (i > 1) {
                throw new n13("Firebase Installations Service is unavailable. Please try again later.");
            }
            TrafficStats.setThreadStatsTag(32771);
            HttpURLConnection httpURLConnectionC = i13Var.c(urlA, str);
            try {
                try {
                    httpURLConnectionC.setRequestMethod(HlsMediaPlaylist.Interstitial.CUE_TRIGGER_POST);
                    httpURLConnectionC.addRequestProperty(RtspHeaders.AUTHORIZATION, "FIS_v2 " + strE);
                    httpURLConnectionC.setDoOutput(true);
                    i13.h(httpURLConnectionC);
                    responseCode = httpURLConnectionC.getResponseCode();
                    y08Var.b(responseCode);
                    if (responseCode < 200 || responseCode >= 300) {
                        z = false;
                    }
                } finally {
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                }
            } catch (IOException | AssertionError unused) {
            }
            if (z) {
                up9VarF = i13.f(httpURLConnectionC);
                break;
            }
            i13.b(httpURLConnectionC, null, str, str2);
            if (responseCode == 401 || responseCode == 404) {
                break;
            }
            if (responseCode == 429) {
                throw new n13("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) {
                break;
            }
            i++;
        }
        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.");
        a50.b bVar = (a50.b) up9.a();
        bVar.c = up9.b.b;
        up9VarF = bVar.a();
        int iOrdinal = up9VarF.b().ordinal();
        if (iOrdinal != 0) {
            if (iOrdinal == 1) {
                return h67Var.i();
            }
            if (iOrdinal != 2) {
                throw new n13("Firebase Installations Service is unavailable. Please try again later.");
            }
            synchronized (this) {
                this.j = null;
            }
            h67.a aVarH = h67Var.h();
            aVarH.f(g67.a.b);
            return aVarH.a();
        }
        String strC2 = up9VarF.c();
        long jD = up9VarF.d();
        uda udaVar = this.d;
        udaVar.getClass();
        long seconds = TimeUnit.MILLISECONDS.toSeconds(udaVar.a.currentTimeMillis());
        h67.a aVarH2 = h67Var.h();
        aVarH2.b(strC2);
        aVarH2.c(jD);
        aVarH2.g(seconds);
        return aVarH2.a();
    }

    public final void e() {
        y03 y03Var = this.a;
        y03Var.a();
        Preconditions.checkNotEmpty(y03Var.c.b, "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.");
        y03Var.a();
        Preconditions.checkNotEmpty(y03Var.c.g, "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.");
        y03Var.a();
        Preconditions.checkNotEmpty(y03Var.c.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.");
        y03Var.a();
        String str = y03Var.c.b;
        Pattern pattern = uda.c;
        Preconditions.checkArgument(str.contains(":"), "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.");
        y03Var.a();
        Preconditions.checkArgument(uda.c.matcher(y03Var.c.a).matches(), "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 f(h67 h67Var) {
        String string;
        y03 y03Var = this.a;
        y03Var.a();
        if (!y03Var.b.equals("CHIME_ANDROID_SDK")) {
            y03 y03Var2 = this.a;
            y03Var2.a();
            if ("[DEFAULT]".equals(y03Var2.b)) {
                if (h67Var.f() == g67.a.a) {
                    kh4 kh4Var = (kh4) this.e.get();
                    synchronized (kh4Var.a) {
                        try {
                            synchronized (kh4Var.a) {
                                string = kh4Var.a.getString("|S|id", null);
                            }
                            if (string == null) {
                                string = kh4Var.a();
                            }
                        } finally {
                        }
                    }
                    if (!TextUtils.isEmpty(string)) {
                        return string;
                    }
                    this.f.getClass();
                    return lr7.a();
                }
            }
        }
        this.f.getClass();
        return lr7.a();
    }

    public final h67 g(h67 h67Var) {
        HttpURLConnection httpURLConnectionC;
        i13 i13Var;
        int responseCode;
        qn4 qn4VarE;
        String string = null;
        if (h67Var.c() != null && h67Var.c().length() == 11) {
            kh4 kh4Var = (kh4) this.e.get();
            synchronized (kh4Var.a) {
                try {
                    String[] strArr = kh4.c;
                    int i = 0;
                    while (true) {
                        if (i < 4) {
                            String str = strArr[i];
                            String string2 = kh4Var.a.getString("|T|" + kh4Var.b + "|" + str, 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 {
                }
            }
        }
        i13 i13Var2 = this.b;
        y03 y03Var = this.a;
        y03Var.a();
        String str2 = y03Var.c.a;
        String strC = h67Var.c();
        y03 y03Var2 = this.a;
        y03Var2.a();
        String str3 = y03Var2.c.g;
        y03 y03Var3 = this.a;
        y03Var3.a();
        String str4 = y03Var3.c.b;
        y08 y08Var = i13Var2.c;
        if (!y08Var.a()) {
            throw new n13("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlA = i13.a("projects/" + str3 + "/installations");
        int i2 = 0;
        while (true) {
            boolean z = true;
            if (i2 > 1) {
                throw new n13("Firebase Installations Service is unavailable. Please try again later.");
            }
            TrafficStats.setThreadStatsTag(32769);
            httpURLConnectionC = i13Var2.c(urlA, str2);
            try {
                try {
                    httpURLConnectionC.setRequestMethod(HlsMediaPlaylist.Interstitial.CUE_TRIGGER_POST);
                    httpURLConnectionC.setDoOutput(true);
                    if (string != null) {
                        httpURLConnectionC.addRequestProperty("x-goog-fis-android-iid-migration-auth", string);
                    }
                    i13.g(httpURLConnectionC, strC, str4);
                    responseCode = httpURLConnectionC.getResponseCode();
                    y08Var.b(responseCode);
                    if (responseCode < 200 || responseCode >= 300) {
                        z = false;
                    }
                } catch (IOException | AssertionError unused2) {
                    i13Var = i13Var2;
                }
                if (z) {
                    qn4VarE = i13.e(httpURLConnectionC);
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                    break;
                }
                i13.b(httpURLConnectionC, str4, str2, str3);
                if (responseCode == 429) {
                    throw new n13("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) {
                    break;
                }
                httpURLConnectionC.disconnect();
                TrafficStats.clearThreadStatsTag();
                i13Var = i13Var2;
                i2++;
                i13Var2 = i13Var;
            } finally {
                httpURLConnectionC.disconnect();
                TrafficStats.clearThreadStatsTag();
            }
        }
        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.");
        g40.b bVar = new g40.b();
        g40 g40Var = new g40(bVar.a, bVar.b, bVar.c, bVar.d, qn4.b.b);
        httpURLConnectionC.disconnect();
        TrafficStats.clearThreadStatsTag();
        qn4VarE = g40Var;
        int iOrdinal = qn4VarE.d().ordinal();
        if (iOrdinal != 0) {
            if (iOrdinal == 1) {
                return h67Var.i();
            }
            throw new n13("Firebase Installations Service is unavailable. Please try again later.");
        }
        String strB = qn4VarE.b();
        String strC2 = qn4VarE.c();
        uda udaVar = this.d;
        udaVar.getClass();
        long seconds = TimeUnit.MILLISECONDS.toSeconds(udaVar.a.currentTimeMillis());
        String strC3 = qn4VarE.a().c();
        long jD = qn4VarE.a().d();
        h67.a aVarH = h67Var.h();
        aVarH.d(strB);
        aVarH.f(g67.a.d);
        aVarH.b(strC3);
        aVarH.e(strC2);
        aVarH.c(jD);
        aVarH.g(seconds);
        return aVarH.a();
    }

    @Override // defpackage.m13
    public final Task getId() {
        String str;
        e();
        synchronized (this) {
            str = this.j;
        }
        if (str != null) {
            return Tasks.forResult(str);
        }
        TaskCompletionSource taskCompletionSource = new TaskCompletionSource();
        eu3 eu3Var = new eu3(taskCompletionSource);
        synchronized (this.g) {
            this.l.add(eu3Var);
        }
        Task task = taskCompletionSource.getTask();
        this.h.execute(new j13(this, 1));
        return task;
    }

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

    public final void i(h67 h67Var) {
        synchronized (this.g) {
            try {
                Iterator it = this.l.iterator();
                while (it.hasNext()) {
                    if (((l49) it.next()).b(h67Var)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
