package W3;

import O1.C0225a;
import P2.C0253j;
import P2.w;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Trace;
import android.util.Base64;
import android.util.Log;
import com.google.firebase.FirebaseCommonRegistrar;
import com.google.firebase.components.ComponentDiscoveryService;
import com.google.firebase.concurrent.ExecutorsRegistrar;
import com.google.firebase.provider.FirebaseInitProvider;
import d4.C0843b;
import d4.l;
import e4.m;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: loaded from: classes.dex */
public final class f {

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public static final q.e f7112l = new q.e(0);

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final d4.e f7116d;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final D4.b f7120h;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final AtomicBoolean f7117e = new AtomicBoolean(false);

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final AtomicBoolean f7118f = new AtomicBoolean();
    public final CopyOnWriteArrayList i = new CopyOnWriteArrayList();

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final CopyOnWriteArrayList f7121j = new CopyOnWriteArrayList();

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r1v4 */
    /* JADX WARN: Type inference failed for: r1v5 */
    /* JADX WARN: Type inference failed for: r2v12, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r2v6, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r2v7, types: [java.util.List] */
    public f(Context context, String str, h hVar) {
        ?? arrayList;
        int i = 2;
        ?? r12 = 1;
        this.f7113a = context;
        w.c(str);
        this.f7114b = str;
        this.f7115c = hVar;
        a aVar = FirebaseInitProvider.f10556q;
        Trace.beginSection("Firebase");
        Trace.beginSection("ComponentDiscovery");
        ArrayList arrayList2 = new ArrayList();
        Bundle bundle = null;
        try {
            PackageManager packageManager = context.getPackageManager();
            if (packageManager == null) {
                Log.w("ComponentDiscovery", "Context has no PackageManager.");
            } else {
                ServiceInfo serviceInfo = packageManager.getServiceInfo(new ComponentName(context, (Class<?>) ComponentDiscoveryService.class), 128);
                if (serviceInfo == null) {
                    Log.w("ComponentDiscovery", ComponentDiscoveryService.class + " has no service info.");
                } else {
                    bundle = serviceInfo.metaData;
                }
            }
        } catch (PackageManager.NameNotFoundException unused) {
            Log.w("ComponentDiscovery", "Application info not found.");
        }
        if (bundle == null) {
            Log.w("ComponentDiscovery", "Could not retrieve metadata, returning empty list of registrars.");
            arrayList = Collections.EMPTY_LIST;
        } else {
            arrayList = new ArrayList();
            for (String str2 : bundle.keySet()) {
                if ("com.google.firebase.components.ComponentRegistrar".equals(bundle.get(str2)) && str2.startsWith("com.google.firebase.components:")) {
                    arrayList.add(str2.substring(31));
                }
            }
        }
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            arrayList2.add(new E4.b(r12 == true ? 1 : 0, (String) it.next()));
        }
        Trace.endSection();
        Trace.beginSection("Runtime");
        m mVar = m.f12473q;
        ArrayList arrayList3 = new ArrayList();
        ArrayList arrayList4 = new ArrayList();
        arrayList3.addAll(arrayList2);
        arrayList3.add(new E4.b(i, new FirebaseCommonRegistrar()));
        arrayList3.add(new E4.b(i, new ExecutorsRegistrar()));
        arrayList4.add(C0843b.c(context, Context.class, new Class[0]));
        arrayList4.add(C0843b.c(this, f.class, new Class[0]));
        arrayList4.add(C0843b.c(hVar, h.class, new Class[0]));
        C0253j c0253j = new C0253j(17);
        if ((Build.VERSION.SDK_INT >= 24 ? B.g.g(context) : true) && FirebaseInitProvider.f10557r.get()) {
            arrayList4.add(C0843b.c(aVar, a.class, new Class[0]));
        }
        d4.e eVar = new d4.e(arrayList3, arrayList4, c0253j);
        this.f7116d = eVar;
        Trace.endSection();
        this.f7119g = new l(new C4.c(this, context));
        this.f7120h = eVar.d(C4.e.class);
        c cVar = new c(this);
        a();
        if (this.f7117e.get()) {
            O2.c.f5095u.f5096q.get();
        }
        this.i.add(cVar);
        Trace.endSection();
    }

    public static f c() {
        f fVar;
        synchronized (f7111k) {
            try {
                fVar = (f) f7112l.get("[DEFAULT]");
                if (fVar == null) {
                    throw new IllegalStateException("Default FirebaseApp is not initialized in this process " + T2.b.b() + ". Make sure to call FirebaseApp.initializeApp(Context) first.");
                }
                ((C4.e) fVar.f7120h.get()).b();
            } catch (Throwable th) {
                throw th;
            }
        }
        return fVar;
    }

    public static f f(Context context) {
        synchronized (f7111k) {
            try {
                if (f7112l.containsKey("[DEFAULT]")) {
                    return c();
                }
                h hVarA = h.a(context);
                if (hVarA == null) {
                    Log.w("FirebaseApp", "Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project.");
                    return null;
                }
                return g(context, hVarA);
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public static f g(Context context, h hVar) {
        f fVar;
        AtomicReference atomicReference = d.f7108a;
        if (context.getApplicationContext() instanceof Application) {
            Application application = (Application) context.getApplicationContext();
            AtomicReference atomicReference2 = d.f7108a;
            if (atomicReference2.get() == null) {
                d dVar = new d();
                while (true) {
                    if (atomicReference2.compareAndSet(null, dVar)) {
                        O2.c.b(application);
                        O2.c.f5095u.a(dVar);
                        break;
                    }
                    if (atomicReference2.get() != null) {
                        break;
                    }
                }
            }
        }
        if (context.getApplicationContext() != null) {
            context = context.getApplicationContext();
        }
        synchronized (f7111k) {
            q.e eVar = f7112l;
            w.h("FirebaseApp name [DEFAULT] already exists!", !eVar.containsKey("[DEFAULT]"));
            w.g(context, "Application context cannot be null.");
            fVar = new f(context, "[DEFAULT]", hVar);
            eVar.put("[DEFAULT]", fVar);
        }
        fVar.e();
        return fVar;
    }

    public final void a() {
        w.h("FirebaseApp was deleted", !this.f7118f.get());
    }

    public final Object b(Class cls) {
        a();
        return this.f7116d.a(cls);
    }

    public final String d() {
        StringBuilder sb = new StringBuilder();
        a();
        byte[] bytes = this.f7114b.getBytes(Charset.defaultCharset());
        sb.append(bytes == null ? null : Base64.encodeToString(bytes, 11));
        sb.append("+");
        a();
        byte[] bytes2 = this.f7115c.f7128b.getBytes(Charset.defaultCharset());
        sb.append(bytes2 != null ? Base64.encodeToString(bytes2, 11) : null);
        return sb.toString();
    }

    public final void e() {
        HashMap map;
        if (!(Build.VERSION.SDK_INT >= 24 ? B.g.g(this.f7113a) : true)) {
            StringBuilder sb = new StringBuilder("Device in Direct Boot Mode: postponing initialization of Firebase APIs for app ");
            a();
            sb.append(this.f7114b);
            Log.i("FirebaseApp", sb.toString());
            Context context = this.f7113a;
            AtomicReference atomicReference = e.f7109b;
            if (atomicReference.get() == null) {
                e eVar = new e(context);
                while (!atomicReference.compareAndSet(null, eVar)) {
                    if (atomicReference.get() != null) {
                        return;
                    }
                }
                context.registerReceiver(eVar, new IntentFilter("android.intent.action.USER_UNLOCKED"));
                return;
            }
            return;
        }
        StringBuilder sb2 = new StringBuilder("Device unlocked: initializing all Firebase APIs for app ");
        a();
        sb2.append(this.f7114b);
        Log.i("FirebaseApp", sb2.toString());
        d4.e eVar2 = this.f7116d;
        a();
        boolean zEquals = "[DEFAULT]".equals(this.f7114b);
        AtomicReference atomicReference2 = eVar2.f12259v;
        Boolean boolValueOf = Boolean.valueOf(zEquals);
        while (true) {
            if (atomicReference2.compareAndSet(null, boolValueOf)) {
                synchronized (eVar2) {
                    map = new HashMap(eVar2.f12254q);
                }
                eVar2.b(map, zEquals);
                break;
            } else if (atomicReference2.get() != null) {
                break;
            }
        }
        ((C4.e) this.f7120h.get()).b();
    }

    public final boolean equals(Object obj) {
        if (!(obj instanceof f)) {
            return false;
        }
        f fVar = (f) obj;
        fVar.a();
        return this.f7114b.equals(fVar.f7114b);
    }

    public final boolean h() {
        boolean z8;
        a();
        I4.a aVar = (I4.a) this.f7119g.get();
        synchronized (aVar) {
            z8 = aVar.f3375a;
        }
        return z8;
    }

    public final int hashCode() {
        return this.f7114b.hashCode();
    }

    public final String toString() {
        C0225a c0225a = new C0225a(this);
        c0225a.d(this.f7114b, "name");
        c0225a.d(this.f7115c, "options");
        return c0225a.toString();
    }
}
