package o6;

import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import p6.AbstractC1439e;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final ArrayList f15929d;

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

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

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

    static {
        ArrayList arrayList = new ArrayList(5);
        f15929d = arrayList;
        arrayList.add(AbstractC1380C.f15839a);
        arrayList.add(C1388h.f15870c);
        arrayList.add(C1382b.f15850e);
        arrayList.add(C1382b.f15849d);
        arrayList.add(y.f15933a);
        arrayList.add(C1387g.f15866d);
    }

    public x(N0.c cVar) {
        ArrayList arrayList = cVar.f4375a;
        int size = arrayList.size();
        ArrayList arrayList2 = f15929d;
        ArrayList arrayList3 = new ArrayList(arrayList2.size() + size);
        arrayList3.addAll(arrayList);
        arrayList3.addAll(arrayList2);
        this.f15930a = Collections.unmodifiableList(arrayList3);
    }

    public final AbstractC1391k a(Class cls) {
        return b(cls, AbstractC1439e.f16150a, null);
    }

    public final AbstractC1391k b(Type type, Set set, String str) {
        AbstractC1391k abstractC1391k;
        if (type == null) {
            throw new NullPointerException("type == null");
        }
        if (set == null) {
            throw new NullPointerException("annotations == null");
        }
        Type typeA = AbstractC1439e.a(type);
        if (typeA instanceof WildcardType) {
            WildcardType wildcardType = (WildcardType) typeA;
            if (wildcardType.getLowerBounds().length == 0) {
                Type[] upperBounds = wildcardType.getUpperBounds();
                if (upperBounds.length != 1) {
                    throw new IllegalArgumentException();
                }
                typeA = upperBounds[0];
            }
        }
        Object objAsList = set.isEmpty() ? typeA : Arrays.asList(typeA, set);
        synchronized (this.f15932c) {
            try {
                AbstractC1391k abstractC1391k2 = (AbstractC1391k) this.f15932c.get(objAsList);
                if (abstractC1391k2 != null) {
                    return abstractC1391k2;
                }
                C1403w c1403w = (C1403w) this.f15931b.get();
                if (c1403w == null) {
                    c1403w = new C1403w(this);
                    this.f15931b.set(c1403w);
                }
                ArrayList arrayList = c1403w.f15925a;
                int size = arrayList.size();
                int i = 0;
                while (true) {
                    ArrayDeque arrayDeque = c1403w.f15926b;
                    if (i >= size) {
                        C1402v c1402v = new C1402v(typeA, str, objAsList);
                        arrayList.add(c1402v);
                        arrayDeque.add(c1402v);
                        abstractC1391k = null;
                        break;
                    }
                    C1402v c1402v2 = (C1402v) arrayList.get(i);
                    if (c1402v2.f15923c.equals(objAsList)) {
                        arrayDeque.add(c1402v2);
                        AbstractC1391k abstractC1391k3 = c1402v2.f15924d;
                        abstractC1391k = c1402v2;
                        if (abstractC1391k3 != null) {
                            abstractC1391k = abstractC1391k3;
                        }
                    } else {
                        i++;
                    }
                }
                try {
                    if (abstractC1391k != null) {
                        return abstractC1391k;
                    }
                    try {
                        int size2 = this.f15930a.size();
                        for (int i5 = 0; i5 < size2; i5++) {
                            AbstractC1391k abstractC1391kA = ((C1381a) this.f15930a.get(i5)).a(typeA, set, this);
                            if (abstractC1391kA != null) {
                                ((C1402v) c1403w.f15926b.getLast()).f15924d = abstractC1391kA;
                                c1403w.b(true);
                                return abstractC1391kA;
                            }
                        }
                        throw new IllegalArgumentException("No JsonAdapter for " + AbstractC1439e.h(typeA, set));
                    } catch (IllegalArgumentException e8) {
                        throw c1403w.a(e8);
                    }
                } finally {
                    c1403w.b(false);
                }
            } finally {
            }
        }
    }
}
