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;

/* JADX INFO: renamed from: ᵫᵘˆ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C3983 implements InterfaceC4093 {

    /* JADX INFO: renamed from: ᵝᵶ, reason: contains not printable characters */
    public static final Object f15245 = new Object();

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final C1754 f15246;

    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    public final ExecutorService f15247;

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final C3714 f15248;

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public final C2592 f15249;

    /* JADX INFO: renamed from: ᵮᵣᵗˈ, reason: contains not printable characters */
    public final ArrayList f15250;

    /* JADX INFO: renamed from: ᵱᵽʡᵚ, reason: contains not printable characters */
    public String f15251;

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final C1191 f15252;

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public final Object f15253;

    /* JADX INFO: renamed from: ᵶᵥᵝ, reason: contains not printable characters */
    public final HashSet f15254;

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final C2447 f15255;

    /* JADX INFO: renamed from: ᵻʽᵯ, reason: contains not printable characters */
    public final ExecutorC0249 f15256;

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final C2713 f15257;

    static {
        new AtomicInteger(1);
    }

    public C3983(C1191 c1191, InterfaceC5081 interfaceC5081, ExecutorService executorService, ExecutorC0249 executorC0249) {
        c1191.m3677();
        C1754 c1754 = new C1754(c1191.f5076, interfaceC5081);
        C3714 c3714 = new C3714(10, c1191);
        if (ᵨᵡᵱ.ᵜᵪʾ == null) {
            ᵨᵡᵱ.ᵜᵪʾ = new ᵨᵡᵱ(10);
        }
        ᵨᵡᵱ r1 = ᵨᵡᵱ.ᵜᵪʾ;
        if (C2447.f9506 == null) {
            C2447.f9506 = new C2447(r1);
        }
        C2447 c2447 = C2447.f9506;
        C2592 c2592 = new C2592(new C0118(2, c1191));
        C2713 c2713 = new C2713();
        this.f15253 = new Object();
        this.f15254 = new HashSet();
        this.f15250 = new ArrayList();
        this.f15252 = c1191;
        this.f15246 = c1754;
        this.f15248 = c3714;
        this.f15255 = c2447;
        this.f15249 = c2592;
        this.f15257 = c2713;
        this.f15247 = executorService;
        this.f15256 = executorC0249;
    }

    /* JADX WARN: Finally extract failed */
    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final void m8751() {
        C3400 c3400M8358;
        int i;
        synchronized (f15245) {
            try {
                C1191 c1191 = this.f15252;
                c1191.m3677();
                C0767 c0767M2756 = C0767.m2756(c1191.f5076);
                try {
                    c3400M8358 = this.f15248.m8358();
                    int i2 = c3400M8358.f13095;
                    i = 2;
                    boolean z = true;
                    if (i2 != 2 && i2 != 1) {
                        z = false;
                    }
                    if (z) {
                        String strM8752 = m8752(c3400M8358);
                        C3714 c3714 = this.f15248;
                        C4180 c4180M7800 = c3400M8358.m7800();
                        c4180M7800.f15949 = strM8752;
                        c4180M7800.f15946 = 3;
                        c3400M8358 = c4180M7800.m9116();
                        c3714.m8368(c3400M8358);
                    }
                    if (c0767M2756 != null) {
                        c0767M2756.m2768();
                    }
                } catch (Throwable th) {
                    if (c0767M2756 != null) {
                        c0767M2756.m2768();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
        m8760(c3400M8358);
        this.f15256.execute(new RunnableC3407(this, i));
    }

    /* JADX WARN: Removed duplicated region for block: B:6:0x001e  */
    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String m8752(C3400 c3400) {
        String strM7150;
        C1191 c1191 = this.f15252;
        c1191.m3677();
        if (!c1191.f5071.equals("CHIME_ANDROID_SDK")) {
            C1191 c11912 = this.f15252;
            c11912.m3677();
            if ("[DEFAULT]".equals(c11912.f5071)) {
                if (c3400.f13095 == 1) {
                    C3052 c3052 = (C3052) this.f15249.get();
                    synchronized (c3052.f11695) {
                        try {
                            strM7150 = c3052.m7150();
                            if (strM7150 == null) {
                                strM7150 = c3052.m7149();
                            }
                        } finally {
                        }
                    }
                    if (!TextUtils.isEmpty(strM7150)) {
                        return strM7150;
                    }
                    this.f15257.getClass();
                    return C2713.m6604();
                }
            }
        }
        this.f15257.getClass();
        return C2713.m6604();
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final C3400 m8753(C3400 c3400) throws FirebaseInstallationsException {
        int responseCode;
        C0400 c0400M4803;
        C1191 c1191 = this.f15252;
        c1191.m3677();
        String str = c1191.f5073.f16226;
        String str2 = c3400.f13098;
        c1191.m3677();
        String str3 = c1191.f5073.f16227;
        String str4 = c3400.f13100;
        C1754 c1754 = this.f15246;
        C5019 c5019 = c1754.f6999;
        if (!c5019.m10354()) {
            throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlM4799 = C1754.m4799("projects/" + str3 + "/installations/" + str2 + "/authTokens:generate");
        for (int i = 0; i <= 1; i++) {
            TrafficStats.setThreadStatsTag(32771);
            HttpURLConnection httpURLConnectionM4804 = c1754.m4804(urlM4799, str);
            try {
                try {
                    httpURLConnectionM4804.setRequestMethod("POST");
                    httpURLConnectionM4804.addRequestProperty("Authorization", "FIS_v2 " + str4);
                    httpURLConnectionM4804.setDoOutput(true);
                    C1754.m4797(httpURLConnectionM4804);
                    responseCode = httpURLConnectionM4804.getResponseCode();
                    c5019.m10357(responseCode);
                } finally {
                    httpURLConnectionM4804.disconnect();
                    TrafficStats.clearThreadStatsTag();
                }
            } catch (IOException | AssertionError unused) {
            }
            if (responseCode >= 200 && responseCode < 300) {
                c0400M4803 = C1754.m4803(httpURLConnectionM4804);
            } else {
                C1754.m4796(httpURLConnectionM4804, null);
                if (responseCode == 401 || responseCode == 404) {
                    C1404 c1404M1719 = C0400.m1719();
                    c1404M1719.f5808 = 3;
                    c0400M4803 = c1404M1719.m4040();
                } 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) {
                        C1404 c1404M17192 = C0400.m1719();
                        c1404M17192.f5808 = 2;
                        c0400M4803 = c1404M17192.m4040();
                    }
                }
            }
            int iM8218 = AbstractC3653.m8218(c0400M4803.f2273);
            if (iM8218 == 0) {
                String str5 = c0400M4803.f2274;
                long j = c0400M4803.f2272;
                this.f15255.f9507.getClass();
                long jCurrentTimeMillis = System.currentTimeMillis() / 1000;
                C4180 c4180M7800 = c3400.m7800();
                c4180M7800.f15947 = str5;
                c4180M7800.f15948 = Long.valueOf(j);
                c4180M7800.f15952 = Long.valueOf(jCurrentTimeMillis);
                return c4180M7800.m9116();
            }
            if (iM8218 == 1) {
                C4180 c4180M78002 = c3400.m7800();
                c4180M78002.f15950 = "BAD CONFIG";
                c4180M78002.f15946 = 5;
                return c4180M78002.m9116();
            }
            if (iM8218 != 2) {
                throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
            }
            m8756(null);
            C4180 c4180M78003 = c3400.m7800();
            c4180M78003.f15946 = 2;
            return c4180M78003.m9116();
        }
        throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
    }

    /* JADX INFO: renamed from: ᵝᵶ, reason: contains not printable characters */
    public final synchronized void m8754(C3400 c3400, C3400 c34002) {
        try {
            if (this.f15254.size() != 0 && !TextUtils.equals(c3400.f13098, c34002.f13098)) {
                Iterator it = this.f15254.iterator();
                if (it.hasNext()) {
                    if (it.next() != null) {
                        throw new ClassCastException();
                    }
                    throw null;
                }
            }
        } finally {
        }
    }

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public final C1300 m8755() {
        m8759();
        C0589 c0589 = new C0589();
        m8758(new C0940(this.f15255, c0589));
        this.f15247.execute(new RunnableC3407(this, 1));
        return c0589.f2890;
    }

    /* JADX INFO: renamed from: ᵮᵣᵗˈ, reason: contains not printable characters */
    public final synchronized void m8756(String str) {
        this.f15251 = str;
    }

    /* JADX INFO: renamed from: ᵱᵽʡᵚ, reason: contains not printable characters */
    public final void m8757(Exception exc) {
        synchronized (this.f15253) {
            try {
                Iterator it = this.f15250.iterator();
                while (it.hasNext()) {
                    if (((InterfaceC0432) it.next()).mo1832(exc)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final void m8758(InterfaceC0432 interfaceC0432) {
        synchronized (this.f15253) {
            this.f15250.add(interfaceC0432);
        }
    }

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public final void m8759() {
        C1191 c1191 = this.f15252;
        c1191.m3677();
        ᵘᵞʿⁱ.ᵻʽᵯ(c1191.f5073.f16223, "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.");
        c1191.m3677();
        ᵘᵞʿⁱ.ᵻʽᵯ(c1191.f5073.f16227, "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.");
        c1191.m3677();
        ᵘᵞʿⁱ.ᵻʽᵯ(c1191.f5073.f16226, "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.");
        c1191.m3677();
        String str = c1191.f5073.f16223;
        Pattern pattern = C2447.f9505;
        ᵘᵞʿⁱ.ᵨᵣ("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(":"));
        c1191.m3677();
        ᵘᵞʿⁱ.ᵨᵣ("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.", C2447.f9505.matcher(c1191.f5073.f16226).matches());
    }

    /* JADX INFO: renamed from: ᵶᵥᵝ, reason: contains not printable characters */
    public final void m8760(C3400 c3400) {
        synchronized (this.f15253) {
            try {
                Iterator it = this.f15250.iterator();
                while (it.hasNext()) {
                    if (((InterfaceC0432) it.next()).mo1831(c3400)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final C1300 m8761() {
        String str;
        m8759();
        synchronized (this) {
            str = this.f15251;
        }
        if (str != null) {
            return ᵥˈᵛ.ᵸˆˁ(str);
        }
        C0589 c0589 = new C0589();
        m8758(new C1885(c0589));
        C1300 c1300 = c0589.f2890;
        this.f15247.execute(new RunnableC3407(this, 0));
        return c1300;
    }

    /* JADX INFO: renamed from: ᵻʽᵯ, reason: contains not printable characters */
    public final C3400 m8762(C3400 c3400) throws FirebaseInstallationsException {
        int responseCode;
        C1169 c1169M4798;
        String str = c3400.f13098;
        String string = null;
        if (str != null && str.length() == 11) {
            C3052 c3052 = (C3052) this.f15249.get();
            synchronized (c3052.f11695) {
                try {
                    String[] strArr = C3052.f11693;
                    int i = 0;
                    while (true) {
                        if (i < 4) {
                            String str2 = strArr[i];
                            String string2 = c3052.f11695.getString("|T|" + c3052.f11694 + "|" + 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 {
                }
            }
        }
        C1754 c1754 = this.f15246;
        C1191 c1191 = this.f15252;
        c1191.m3677();
        String str3 = c1191.f5073.f16226;
        String str4 = c3400.f13098;
        C1191 c11912 = this.f15252;
        c11912.m3677();
        String str5 = c11912.f5073.f16227;
        C1191 c11913 = this.f15252;
        c11913.m3677();
        String str6 = c11913.f5073.f16223;
        C5019 c5019 = c1754.f6999;
        if (!c5019.m10354()) {
            throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlM4799 = C1754.m4799("projects/" + str5 + "/installations");
        for (int i2 = 0; i2 <= 1; i2++) {
            TrafficStats.setThreadStatsTag(32769);
            HttpURLConnection httpURLConnectionM4804 = c1754.m4804(urlM4799, str3);
            try {
                try {
                    httpURLConnectionM4804.setRequestMethod("POST");
                    httpURLConnectionM4804.setDoOutput(true);
                    if (string != null) {
                        httpURLConnectionM4804.addRequestProperty("x-goog-fis-android-iid-migration-auth", string);
                    }
                    C1754.m4800(httpURLConnectionM4804, str4, str6);
                    responseCode = httpURLConnectionM4804.getResponseCode();
                    c5019.m10357(responseCode);
                } finally {
                    httpURLConnectionM4804.disconnect();
                    TrafficStats.clearThreadStatsTag();
                }
            } catch (IOException | AssertionError unused2) {
            }
            if (responseCode >= 200 && responseCode < 300) {
                c1169M4798 = C1754.m4798(httpURLConnectionM4804);
                httpURLConnectionM4804.disconnect();
                TrafficStats.clearThreadStatsTag();
            } else {
                C1754.m4796(httpURLConnectionM4804, 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) {
                    C1169 c1169 = new C1169(null, null, null, null, 2);
                    httpURLConnectionM4804.disconnect();
                    TrafficStats.clearThreadStatsTag();
                    c1169M4798 = c1169;
                }
                httpURLConnectionM4804.disconnect();
                TrafficStats.clearThreadStatsTag();
            }
            int iM8218 = AbstractC3653.m8218(c1169M4798.f4979);
            if (iM8218 != 0) {
                if (iM8218 != 1) {
                    throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
                }
                C4180 c4180M7800 = c3400.m7800();
                c4180M7800.f15950 = "BAD CONFIG";
                c4180M7800.f15946 = 5;
                return c4180M7800.m9116();
            }
            String str7 = c1169M4798.f4977;
            String str8 = c1169M4798.f4978;
            this.f15255.f9507.getClass();
            long jCurrentTimeMillis = System.currentTimeMillis() / 1000;
            C0400 c0400 = c1169M4798.f4981;
            String str9 = c0400.f2274;
            long j = c0400.f2272;
            C4180 c4180M78002 = c3400.m7800();
            c4180M78002.f15949 = str7;
            c4180M78002.f15946 = 4;
            c4180M78002.f15947 = str9;
            c4180M78002.f15951 = str8;
            c4180M78002.f15948 = Long.valueOf(j);
            c4180M78002.f15952 = Long.valueOf(jCurrentTimeMillis);
            return c4180M78002.m9116();
        }
        throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.");
    }

    /* JADX WARN: Finally extract failed */
    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final void m8763(C3400 c3400) {
        synchronized (f15245) {
            try {
                C1191 c1191 = this.f15252;
                c1191.m3677();
                C0767 c0767M2756 = C0767.m2756(c1191.f5076);
                try {
                    this.f15248.m8368(c3400);
                    if (c0767M2756 != null) {
                        c0767M2756.m2768();
                    }
                } catch (Throwable th) {
                    if (c0767M2756 != null) {
                        c0767M2756.m2768();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }
}
