package com.google.firebase.components;

import com.google.android.gms.measurement.internal.u1;
import e.t;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: loaded from: classes2.dex */
public final class h implements b, za.a {

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final g f24012h = new g(0);

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

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final HashMap f24013a = new HashMap();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final HashMap f24014b = new HashMap();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final HashMap f24015c = new HashMap();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final HashSet f24016d = new HashSet();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final AtomicReference f24018f = new AtomicReference();

    public h(Executor executor, List list, List list2, f fVar) {
        l lVar = new l(executor);
        this.f24017e = lVar;
        this.f24019g = fVar;
        ArrayList<a> arrayList = new ArrayList();
        int i10 = 1;
        arrayList.add(a.c(lVar, l.class, db.c.class, db.b.class));
        arrayList.add(a.c(this, za.a.class, new Class[0]));
        Iterator it = list2.iterator();
        while (it.hasNext()) {
            a aVar = (a) it.next();
            if (aVar != null) {
                arrayList.add(aVar);
            }
        }
        ArrayList arrayList2 = new ArrayList();
        Iterator it2 = list.iterator();
        while (it2.hasNext()) {
            arrayList2.add(it2.next());
        }
        ArrayList arrayList3 = new ArrayList();
        synchronized (this) {
            Iterator it3 = arrayList2.iterator();
            while (it3.hasNext()) {
                try {
                    ComponentRegistrar componentRegistrar = (ComponentRegistrar) ((gb.a) it3.next()).get();
                    if (componentRegistrar != null) {
                        arrayList.addAll(this.f24019g.c(componentRegistrar));
                        it3.remove();
                    }
                } catch (InvalidRegistrarException unused) {
                    it3.remove();
                }
            }
            Iterator it4 = arrayList.iterator();
            while (it4.hasNext()) {
                Object[] array = ((a) it4.next()).f24003b.toArray();
                int length = array.length;
                int i11 = 0;
                while (true) {
                    if (i11 < length) {
                        Object obj = array[i11];
                        if (obj.toString().contains("kotlinx.coroutines.CoroutineDispatcher")) {
                            if (this.f24016d.contains(obj.toString())) {
                                it4.remove();
                                break;
                            }
                            this.f24016d.add(obj.toString());
                        }
                        i11++;
                    }
                }
            }
            if (this.f24013a.isEmpty()) {
                ua.b.q(arrayList);
            } else {
                ArrayList arrayList4 = new ArrayList(this.f24013a.keySet());
                arrayList4.addAll(arrayList);
                ua.b.q(arrayList4);
            }
            for (a aVar2 : arrayList) {
                this.f24013a.put(aVar2, new m(new ua.c(this, aVar2, i10)));
            }
            arrayList3.addAll(h(arrayList));
            arrayList3.addAll(i());
            g();
        }
        Iterator it5 = arrayList3.iterator();
        while (it5.hasNext()) {
            ((Runnable) it5.next()).run();
        }
        Boolean bool = (Boolean) this.f24018f.get();
        if (bool != null) {
            f(bool.booleanValue(), this.f24013a);
        }
    }

    @Override // com.google.firebase.components.b
    public final synchronized gb.a a(p pVar) {
        if (pVar == null) {
            throw new NullPointerException("Null interface requested.");
        }
        return (gb.a) this.f24014b.get(pVar);
    }

    @Override // com.google.firebase.components.b
    public final synchronized gb.a d(p pVar) {
        n nVar = (n) this.f24015c.get(pVar);
        if (nVar != null) {
            return nVar;
        }
        return f24012h;
    }

    public final void f(boolean z10, Map map) {
        ArrayDeque arrayDeque;
        for (Map.Entry entry : map.entrySet()) {
            a aVar = (a) entry.getKey();
            gb.a aVar2 = (gb.a) entry.getValue();
            int i10 = aVar.f24005d;
            if (i10 == 1 || (i10 == 2 && z10)) {
                aVar2.get();
            }
        }
        l lVar = this.f24017e;
        synchronized (lVar) {
            arrayDeque = lVar.f24029b;
            if (arrayDeque != null) {
                lVar.f24029b = null;
            } else {
                arrayDeque = null;
            }
        }
        if (arrayDeque != null) {
            Iterator it = arrayDeque.iterator();
            if (it.hasNext()) {
                u1.x(it.next());
                throw null;
            }
        }
    }

    public final void g() {
        for (a aVar : this.f24013a.keySet()) {
            for (k kVar : aVar.f24004c) {
                if (kVar.f24026b == 2 && !this.f24015c.containsKey(kVar.f24025a)) {
                    HashMap map = this.f24015c;
                    p pVar = kVar.f24025a;
                    Set setEmptySet = Collections.emptySet();
                    n nVar = new n();
                    nVar.f24035b = null;
                    nVar.f24034a = Collections.newSetFromMap(new ConcurrentHashMap());
                    nVar.f24034a.addAll(setEmptySet);
                    map.put(pVar, nVar);
                } else if (this.f24014b.containsKey(kVar.f24025a)) {
                    continue;
                } else {
                    int i10 = kVar.f24026b;
                    if (i10 == 1) {
                        throw new MissingDependencyException(String.format("Unsatisfied dependency for component %s: %s", aVar, kVar.f24025a));
                    }
                    if (i10 != 2) {
                        HashMap map2 = this.f24014b;
                        p pVar2 = kVar.f24025a;
                        oa.a aVar2 = o.f24036c;
                        g gVar = o.f24037d;
                        o oVar = new o();
                        oVar.f24038a = aVar2;
                        oVar.f24039b = gVar;
                        map2.put(pVar2, oVar);
                    }
                }
            }
        }
    }

    public final ArrayList h(ArrayList arrayList) {
        ArrayList arrayList2 = new ArrayList();
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            a aVar = (a) it.next();
            if (aVar.f24006e == 0) {
                gb.a aVar2 = (gb.a) this.f24013a.get(aVar);
                for (p pVar : aVar.f24003b) {
                    HashMap map = this.f24014b;
                    if (map.containsKey(pVar)) {
                        arrayList2.add(new t((o) ((gb.a) map.get(pVar)), aVar2, 20));
                    } else {
                        map.put(pVar, aVar2);
                    }
                }
            }
        }
        return arrayList2;
    }

    public final ArrayList i() {
        ArrayList arrayList = new ArrayList();
        HashMap map = new HashMap();
        for (Map.Entry entry : this.f24013a.entrySet()) {
            a aVar = (a) entry.getKey();
            if (aVar.f24006e != 0) {
                gb.a aVar2 = (gb.a) entry.getValue();
                for (p pVar : aVar.f24003b) {
                    if (!map.containsKey(pVar)) {
                        map.put(pVar, new HashSet());
                    }
                    ((Set) map.get(pVar)).add(aVar2);
                }
            }
        }
        for (Map.Entry entry2 : map.entrySet()) {
            Object key = entry2.getKey();
            HashMap map2 = this.f24015c;
            if (map2.containsKey(key)) {
                n nVar = (n) map2.get(entry2.getKey());
                Iterator it = ((Set) entry2.getValue()).iterator();
                while (it.hasNext()) {
                    arrayList.add(new t(nVar, (gb.a) it.next(), 21));
                }
            } else {
                p pVar2 = (p) entry2.getKey();
                Set set = (Set) ((Collection) entry2.getValue());
                n nVar2 = new n();
                nVar2.f24035b = null;
                nVar2.f24034a = Collections.newSetFromMap(new ConcurrentHashMap());
                nVar2.f24034a.addAll(set);
                map2.put(pVar2, nVar2);
            }
        }
        return arrayList;
    }
}
