package k.a;

import j.k0.o;
import j.p;
import j.p0.d.d0;
import j.p0.d.r;
import j.u;
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;
import k.a.p.a1;
import k.a.p.k1;

/* JADX INFO: compiled from: SerializersJvm.kt */
/* JADX INFO: loaded from: classes.dex */
final /* synthetic */ class k {
    private static final b<Object> a(k.a.r.c cVar, GenericArrayType genericArrayType, boolean z) {
        b<Object> bVarB;
        j.s0.c cVarC;
        Type genericComponentType = genericArrayType.getGenericComponentType();
        if (genericComponentType instanceof WildcardType) {
            Type[] upperBounds = ((WildcardType) genericComponentType).getUpperBounds();
            r.e(upperBounds, "it.upperBounds");
            genericComponentType = (Type) j.k0.f.u(upperBounds);
        }
        r.e(genericComponentType, "eType");
        if (z) {
            bVarB = j.a(cVar, genericComponentType);
        } else {
            bVarB = j.b(cVar, genericComponentType);
            if (bVarB == null) {
                return null;
            }
        }
        if (genericComponentType instanceof ParameterizedType) {
            Type rawType = ((ParameterizedType) genericComponentType).getRawType();
            r.d(rawType, "null cannot be cast to non-null type java.lang.Class<*>");
            cVarC = j.p0.a.c((Class) rawType);
        } else {
            if (!(genericComponentType instanceof j.s0.c)) {
                throw new IllegalStateException("unsupported type in GenericArray: " + d0.b(genericComponentType.getClass()));
            }
            cVarC = (j.s0.c) genericComponentType;
        }
        r.d(cVarC, "null cannot be cast to non-null type kotlin.reflect.KClass<kotlin.Any>");
        b<Object> bVarA = k.a.m.a.a(cVarC, bVarB);
        r.d(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();
            r.e(rawType, "it.rawType");
            return b(rawType);
        }
        if (type instanceof WildcardType) {
            Type[] upperBounds = ((WildcardType) type).getUpperBounds();
            r.e(upperBounds, "it.upperBounds");
            Object objU = j.k0.f.u(upperBounds);
            r.e(objU, "it.upperBounds.first()");
            return b((Type) objU);
        }
        if (type instanceof GenericArrayType) {
            Type genericComponentType = ((GenericArrayType) type).getGenericComponentType();
            r.e(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 + ' ' + d0.b(type.getClass()));
    }

    private static final <T> b<T> c(k.a.r.c cVar, Class<T> cls, List<? extends b<Object>> list) throws IllegalAccessException, InvocationTargetException {
        Object[] array = list.toArray(new b[0]);
        r.d(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 = a1.b(cls, (b[]) Arrays.copyOf(bVarArr, bVarArr.length));
        if (bVarB != null) {
            return bVarB;
        }
        j.s0.c<T> cVarC = j.p0.a.c(cls);
        b<T> bVarB2 = k1.b(cVarC);
        return bVarB2 == null ? cVar.b(cVarC, list) : bVarB2;
    }

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

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

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

    private static final b<Object> h(k.a.r.c cVar, Class<?> cls, boolean z) {
        b<Object> bVarB;
        if (!cls.isArray() || cls.getComponentType().isPrimitive()) {
            r.d(cls, "null cannot be cast to non-null type java.lang.Class<kotlin.Any>");
            return c(cVar, cls, o.g());
        }
        Class<?> componentType = cls.getComponentType();
        r.e(componentType, "type.componentType");
        if (z) {
            bVarB = j.a(cVar, componentType);
        } else {
            bVarB = j.b(cVar, componentType);
            if (bVarB == null) {
                return null;
            }
        }
        j.s0.c cVarC = j.p0.a.c(componentType);
        r.d(cVarC, "null cannot be cast to non-null type kotlin.reflect.KClass<kotlin.Any>");
        b<Object> bVarA = k.a.m.a.a(cVarC, bVarB);
        r.d(bVarA, "null cannot be cast to non-null type kotlinx.serialization.KSerializer<kotlin.Any>");
        return bVarA;
    }
}
