package com.google.firebase.installations;

import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.google.android.gms.common.internal.w;
import com.google.android.gms.internal.atv_ads_framework.d1;
import com.google.firebase.components.m;
import com.google.firebase.concurrent.b;
import com.google.firebase.installations.FirebaseInstallationsException;
import com.google.firebase.installations.local.PersistedInstallation$RegistrationStatus;
import fe.h;
import hb.d;
import hb.e;
import hb.f;
import hb.i;
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.TimeUnit;
import jb.c;
import ua.g;
import w8.k;
import w8.t;

/* JADX INFO: loaded from: classes2.dex */
public final class a implements d {

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

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

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

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

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

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final hb.g f24097f;

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

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

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

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

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

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

    static {
        new o6.a(2);
    }

    public a(g gVar, gb.a aVar, ExecutorService executorService, b bVar) {
        gVar.a();
        c cVar = new c(gVar.f33401a, aVar);
        h hVar = new h(gVar);
        i iVarA = i.a();
        m mVar = new m(new com.google.firebase.components.c(gVar, 2));
        hb.g gVar2 = new hb.g();
        this.f24098g = new Object();
        this.f24102k = new HashSet();
        this.f24103l = new ArrayList();
        this.f24092a = gVar;
        this.f24093b = cVar;
        this.f24094c = hVar;
        this.f24095d = iVarA;
        this.f24096e = mVar;
        this.f24097f = gVar2;
        this.f24099h = executorService;
        this.f24100i = bVar;
    }

    /* JADX WARN: Finally extract failed */
    public final void a(boolean z10) {
        ib.b bVarO;
        synchronized (f24091m) {
            try {
                g gVar = this.f24092a;
                gVar.a();
                d1 d1VarC = d1.c(gVar.f33401a);
                try {
                    bVarO = this.f24094c.O();
                    PersistedInstallation$RegistrationStatus persistedInstallation$RegistrationStatus = PersistedInstallation$RegistrationStatus.f24105b;
                    PersistedInstallation$RegistrationStatus persistedInstallation$RegistrationStatus2 = bVarO.f26600c;
                    if (persistedInstallation$RegistrationStatus2 == persistedInstallation$RegistrationStatus || persistedInstallation$RegistrationStatus2 == PersistedInstallation$RegistrationStatus.f24104a) {
                        String strF = f(bVarO);
                        h hVar = this.f24094c;
                        ib.a aVarA = bVarO.a();
                        aVarA.f26592a = strF;
                        aVarA.b(PersistedInstallation$RegistrationStatus.f24106c);
                        bVarO = aVarA.a();
                        hVar.H(bVarO);
                    }
                    if (d1VarC != null) {
                        d1VarC.o();
                    }
                } catch (Throwable th) {
                    if (d1VarC != null) {
                        d1VarC.o();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
        if (z10) {
            bVarO = bVarO.a().setAuthToken(null).a();
        }
        i(bVarO);
        this.f24100i.execute(new hb.c(this, z10, 0));
    }

    public final ib.b b(ib.b bVar) throws FirebaseInstallationsException {
        jb.b bVarA = this.f24093b.a(getApiKey(), bVar.getFirebaseInstallationId(), getProjectIdentifier(), bVar.getRefreshToken());
        int iOrdinal = bVarA.getResponseCode().ordinal();
        if (iOrdinal == 0) {
            String token = bVarA.getToken();
            i iVar = this.f24095d;
            iVar.getClass();
            TimeUnit timeUnit = TimeUnit.MILLISECONDS;
            iVar.f26465a.getClass();
            long seconds = timeUnit.toSeconds(System.currentTimeMillis());
            ib.a aVar = (ib.a) bVar.a().setAuthToken(token);
            aVar.f26596e = Long.valueOf(bVarA.f28756b);
            aVar.f26597f = Long.valueOf(seconds);
            return aVar.a();
        }
        if (iOrdinal == 1) {
            ib.d fisError = bVar.a().setFisError("BAD CONFIG");
            fisError.b(PersistedInstallation$RegistrationStatus.f24108e);
            return fisError.a();
        }
        if (iOrdinal != 2) {
            throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.", FirebaseInstallationsException.Status.f24088b);
        }
        synchronized (this) {
            this.f24101j = null;
        }
        ib.a aVarA = bVar.a();
        aVarA.b(PersistedInstallation$RegistrationStatus.f24105b);
        return aVarA.a();
    }

    public final t c() {
        String str;
        e();
        synchronized (this) {
            str = this.f24101j;
        }
        if (str != null) {
            return k.e(str);
        }
        w8.i iVar = new w8.i();
        f fVar = new f(iVar);
        synchronized (this.f24098g) {
            this.f24103l.add(fVar);
        }
        t tVar = iVar.f34224a;
        this.f24099h.execute(new y.a(this, 18));
        return tVar;
    }

    public final t d() {
        e();
        w8.i iVar = new w8.i();
        e eVar = new e(this.f24095d, iVar);
        synchronized (this.f24098g) {
            this.f24103l.add(eVar);
        }
        this.f24099h.execute(new hb.c(this, false, 1));
        return iVar.f34224a;
    }

    public final void e() {
        g gVar = this.f24092a;
        gVar.a();
        w.checkNotEmpty(gVar.f33403c.f33416b, "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.");
        w.checkNotEmpty(getProjectIdentifier(), "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.");
        w.checkNotEmpty(getApiKey(), "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();
        w.b(i.isValidAppIdFormat(gVar.f33403c.f33416b), "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.");
        w.b(i.isValidApiKeyFormat(getApiKey()), "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:0x001c  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final String f(ib.b bVar) {
        g gVar = this.f24092a;
        gVar.a();
        String str = gVar.f33402b;
        boolean zEquals = str.equals("CHIME_ANDROID_SDK");
        hb.g gVar2 = this.f24097f;
        if (!zEquals) {
            gVar.a();
            if ("[DEFAULT]".equals(str)) {
                if (bVar.f26600c == PersistedInstallation$RegistrationStatus.f24104a) {
                    String iid = ((ib.c) this.f24096e.get()).readIid();
                    if (!TextUtils.isEmpty(iid)) {
                        return iid;
                    }
                    gVar2.getClass();
                    return hb.g.a();
                }
            }
        }
        gVar2.getClass();
        return hb.g.a();
    }

    public final ib.e g(ib.b bVar) throws FirebaseInstallationsException {
        String token = (bVar.getFirebaseInstallationId() == null || bVar.getFirebaseInstallationId().length() != 11) ? null : ((ib.c) this.f24096e.get()).readToken();
        c cVar = this.f24093b;
        String apiKey = getApiKey();
        String firebaseInstallationId = bVar.getFirebaseInstallationId();
        String projectIdentifier = getProjectIdentifier();
        g gVar = this.f24092a;
        gVar.a();
        jb.d dVarCreateFirebaseInstallation = cVar.createFirebaseInstallation(apiKey, firebaseInstallationId, projectIdentifier, gVar.f33403c.f33416b, token);
        int iOrdinal = dVarCreateFirebaseInstallation.getResponseCode().ordinal();
        if (iOrdinal != 0) {
            if (iOrdinal != 1) {
                throw new FirebaseInstallationsException("Firebase Installations Service is unavailable. Please try again later.", FirebaseInstallationsException.Status.f24088b);
            }
            ib.d fisError = bVar.a().setFisError("BAD CONFIG");
            fisError.b(PersistedInstallation$RegistrationStatus.f24108e);
            return fisError.a();
        }
        String fid = dVarCreateFirebaseInstallation.getFid();
        String refreshToken = dVarCreateFirebaseInstallation.getRefreshToken();
        i iVar = this.f24095d;
        iVar.getClass();
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        iVar.f26465a.getClass();
        return bVar.withRegisteredFid(fid, refreshToken, timeUnit.toSeconds(System.currentTimeMillis()), dVarCreateFirebaseInstallation.getAuthToken().getToken(), ((jb.b) dVarCreateFirebaseInstallation.getAuthToken()).f28756b);
    }

    @Nullable
    public String getApiKey() {
        g gVar = this.f24092a;
        gVar.a();
        return gVar.f33403c.f33415a;
    }

    @Nullable
    public String getProjectIdentifier() {
        g gVar = this.f24092a;
        gVar.a();
        return gVar.f33403c.getProjectId();
    }

    public final void h(Exception exc) {
        synchronized (this.f24098g) {
            try {
                Iterator it = this.f24103l.iterator();
                while (it.hasNext()) {
                    if (((hb.h) it.next()).b(exc)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void i(ib.e eVar) {
        synchronized (this.f24098g) {
            try {
                Iterator it = this.f24103l.iterator();
                while (it.hasNext()) {
                    if (((hb.h) it.next()).a(eVar)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
