package e.a;

import d.j0.q;
import d.n;
import d.n0.c.s;
import d.n0.c.x;
import d.p;
import d.u;
import e.a.q.j1;
import e.a.q.z0;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/* JADX INFO: compiled from: SerializersJvm.kt */
/* JADX INFO: loaded from: classes.dex */
@n
final /* synthetic */ class l {
    private static final b<Object> a(e.a.s.c cVar, GenericArrayType genericArrayType, boolean z) {
        b<Object> bVarB;
        d.q0.b bVarC;
        Type genericComponentType = genericArrayType.getGenericComponentType();
        if (genericComponentType instanceof WildcardType) {
            Type[] upperBounds = ((WildcardType) genericComponentType).getUpperBounds();
            s.d(upperBounds, "it.upperBounds");
            genericComponentType = (Type) d.j0.f.k(upperBounds);
        }
        s.d(genericComponentType, "eType");
        if (z) {
            bVarB = k.a(cVar, genericComponentType);
        } else {
            bVarB = k.b(cVar, genericComponentType);
            if (bVarB == null) {
                return null;
            }
        }
        if (genericComponentType instanceof ParameterizedType) {
            Type rawType = ((ParameterizedType) genericComponentType).getRawType();
            s.c(rawType, "null cannot be cast to non-null type java.lang.Class<*>");
            bVarC = d.n0.a.c((Class) rawType);
        } else {
            if (!(genericComponentType instanceof d.q0.b)) {
                throw new IllegalStateException("unsupported type in GenericArray: " + x.b(genericComponentType.getClass()));
            }
            bVarC = (d.q0.b) genericComponentType;
        }
        s.c(bVarC, "null cannot be cast to non-null type kotlin.reflect.KClass<kotlin.Any>");
        b<Object> bVarA = e.a.n.a.a(bVarC, bVarB);
        s.c(bVarA, "null cannot be cast to non-null type kotlinx.serialization.KSerializer<kotlin.Any>");
        return bVarA;
    }

    private static final Class<?> b(Type type) {
        if (type instanceof Class) {
            return (Class) type;
        }
        if (type instanceof ParameterizedType) {
            Type rawType = ((ParameterizedType) type).getRawType();
            s.d(rawType, "it.rawType");
            return b(rawType);
        }
        if (type instanceof WildcardType) {
            Type[] upperBounds = ((WildcardType) type).getUpperBounds();
            s.d(upperBounds, "it.upperBounds");
            Object objK = d.j0.f.k(upperBounds);
            s.d(objK, "it.upperBounds.first()");
            return b((Type) objK);
        }
        if (type instanceof GenericArrayType) {
            Type genericComponentType = ((GenericArrayType) type).getGenericComponentType();
            s.d(genericComponentType, "it.genericComponentType");
            return b(genericComponentType);
        }
        throw new IllegalArgumentException("typeToken should be an instance of Class<?>, GenericArray, ParametrizedType or WildcardType, but actual type is " + type + ' ' + x.b(type.getClass()));
    }

    private static final <T> b<T> c(e.a.s.c cVar, Class<T> cls, List<? extends b<Object>> list) throws IllegalAccessException, InvocationTargetException {
        Object[] array = list.toArray(new b[0]);
        s.c(array, "null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.toTypedArray>");
        b[] bVarArr = (b[]) array;
        b<T> bVarB = z0.b(cls, (b[]) Arrays.copyOf(bVarArr, bVarArr.length));
        if (bVarB != null) {
            return bVarB;
        }
        d.q0.b<T> bVarC = d.n0.a.c(cls);
        b<T> bVarB2 = j1.b(bVarC);
        return bVarB2 == null ? cVar.b(bVarC, list) : bVarB2;
    }

    public static final b<Object> d(e.a.s.c cVar, Type type) {
        s.e(cVar, "<this>");
        s.e(type, "type");
        b<Object> bVarE = e(cVar, type, true);
        if (bVarE != null) {
            return bVarE;
        }
        z0.j(b(type));
        throw new d.h();
    }

    private static final b<Object> e(e.a.s.c cVar, Type type, boolean z) {
        ArrayList<b> arrayList;
        if (type instanceof GenericArrayType) {
            return a(cVar, (GenericArrayType) type, z);
        }
        if (type instanceof Class) {
            return h(cVar, (Class) type, z);
        }
        if (!(type instanceof ParameterizedType)) {
            if (type instanceof WildcardType) {
                Type[] upperBounds = ((WildcardType) type).getUpperBounds();
                s.d(upperBounds, "type.upperBounds");
                Object objK = d.j0.f.k(upperBounds);
                s.d(objK, "type.upperBounds.first()");
                return f(cVar, (Type) objK, false, 2, null);
            }
            throw new IllegalArgumentException("typeToken should be an instance of Class<?>, GenericArray, ParametrizedType or WildcardType, but actual type is " + type + ' ' + x.b(type.getClass()));
        }
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type rawType = parameterizedType.getRawType();
        s.c(rawType, "null cannot be cast to non-null type java.lang.Class<*>");
        Class cls = (Class) rawType;
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        s.d(actualTypeArguments, "args");
        if (z) {
            arrayList = new ArrayList(actualTypeArguments.length);
            for (Type type2 : actualTypeArguments) {
                s.d(type2, "it");
                arrayList.add(k.a(cVar, type2));
            }
        } else {
            arrayList = new ArrayList(actualTypeArguments.length);
            for (Type type3 : actualTypeArguments) {
                s.d(type3, "it");
                b<Object> bVarB = k.b(cVar, type3);
                if (bVarB == null) {
                    return null;
                }
                arrayList.add(bVarB);
            }
        }
        if (Set.class.isAssignableFrom(cls)) {
            b<Object> bVarM = e.a.n.a.m((b) arrayList.get(0));
            s.c(bVarM, "null cannot be cast to non-null type kotlinx.serialization.KSerializer<kotlin.Any>");
            return bVarM;
        }
        if (List.class.isAssignableFrom(cls) || Collection.class.isAssignableFrom(cls)) {
            b<Object> bVarH = e.a.n.a.h((b) arrayList.get(0));
            s.c(bVarH, "null cannot be cast to non-null type kotlinx.serialization.KSerializer<kotlin.Any>");
            return bVarH;
        }
        if (Map.class.isAssignableFrom(cls)) {
            b<Object> bVarK = e.a.n.a.k((b) arrayList.get(0), (b) arrayList.get(1));
            s.c(bVarK, "null cannot be cast to non-null type kotlinx.serialization.KSerializer<kotlin.Any>");
            return bVarK;
        }
        if (Map.Entry.class.isAssignableFrom(cls)) {
            b<Object> bVarJ = e.a.n.a.j((b) arrayList.get(0), (b) arrayList.get(1));
            s.c(bVarJ, "null cannot be cast to non-null type kotlinx.serialization.KSerializer<kotlin.Any>");
            return bVarJ;
        }
        if (p.class.isAssignableFrom(cls)) {
            b<Object> bVarL = e.a.n.a.l((b) arrayList.get(0), (b) arrayList.get(1));
            s.c(bVarL, "null cannot be cast to non-null type kotlinx.serialization.KSerializer<kotlin.Any>");
            return bVarL;
        }
        if (u.class.isAssignableFrom(cls)) {
            b<Object> bVarO = e.a.n.a.o((b) arrayList.get(0), (b) arrayList.get(1), (b) arrayList.get(2));
            s.c(bVarO, "null cannot be cast to non-null type kotlinx.serialization.KSerializer<kotlin.Any>");
            return bVarO;
        }
        ArrayList arrayList2 = new ArrayList(q.j(arrayList, 10));
        for (b bVar : arrayList) {
            s.c(bVar, "null cannot be cast to non-null type kotlinx.serialization.KSerializer<kotlin.Any?>");
            arrayList2.add(bVar);
        }
        return c(cVar, cls, arrayList2);
    }

    static /* synthetic */ b f(e.a.s.c cVar, Type type, boolean z, int i, Object obj) {
        if ((i & 2) != 0) {
            z = true;
        }
        return e(cVar, type, z);
    }

    public static final b<Object> g(e.a.s.c cVar, Type type) {
        s.e(cVar, "<this>");
        s.e(type, "type");
        return e(cVar, type, false);
    }

    private static final b<Object> h(e.a.s.c cVar, Class<?> cls, boolean z) {
        b<Object> bVarB;
        if (!cls.isArray() || cls.getComponentType().isPrimitive()) {
            s.c(cls, "null cannot be cast to non-null type java.lang.Class<kotlin.Any>");
            return c(cVar, cls, d.j0.p.d());
        }
        Class<?> componentType = cls.getComponentType();
        s.d(componentType, "type.componentType");
        if (z) {
            bVarB = k.a(cVar, componentType);
        } else {
            bVarB = k.b(cVar, componentType);
            if (bVarB == null) {
                return null;
            }
        }
        d.q0.b bVarC = d.n0.a.c(componentType);
        s.c(bVarC, "null cannot be cast to non-null type kotlin.reflect.KClass<kotlin.Any>");
        b<Object> bVarA = e.a.n.a.a(bVarC, bVarB);
        s.c(bVarA, "null cannot be cast to non-null type kotlinx.serialization.KSerializer<kotlin.Any>");
        return bVarA;
    }
}
