package defpackage;

import androidx.media3.container.NalUnitUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import kotlin.Metadata;
import kotlin.reflect.KClass;
import kotlin.reflect.KType;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\u0002\n\u0000¨\u0006\u0000"}, d2 = {"ktor-serialization-kotlinx"}, k = 2, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class ij8 {
    public static final n25 a(Collection collection, pj8 pj8Var) {
        Collection collection2 = collection;
        ArrayList arrayListX = y41.x(collection2);
        ArrayList arrayList = new ArrayList(y41.p(arrayListX, 10));
        Iterator it = arrayListX.iterator();
        while (it.hasNext()) {
            arrayList.add(b(it.next(), pj8Var));
        }
        HashSet hashSet = new HashSet();
        ArrayList arrayList2 = new ArrayList();
        for (Object obj : arrayList) {
            if (hashSet.add(((n25) obj).getDescriptor().getA())) {
                arrayList2.add(obj);
            }
        }
        if (arrayList2.size() > 1) {
            StringBuilder sb = new StringBuilder("Serializing collections of different element types is not yet supported. Selected serializers: ");
            ArrayList arrayList3 = new ArrayList(y41.p(arrayList2, 10));
            Iterator it2 = arrayList2.iterator();
            while (it2.hasNext()) {
                arrayList3.add(((n25) it2.next()).getDescriptor().getA());
            }
            sb.append(arrayList3);
            throw new IllegalStateException(sb.toString().toString());
        }
        n25 n25Var = (n25) y41.bf(arrayList2);
        if (n25Var == null) {
            n25Var = u79.a;
        }
        if (!n25Var.getDescriptor().b() && (!(collection2 instanceof Collection) || !collection2.isEmpty())) {
            Iterator it3 = collection2.iterator();
            while (it3.hasNext()) {
                if (it3.next() == null) {
                    return qk0.a(n25Var);
                }
            }
        }
        return n25Var;
    }

    public static final n25 b(Object obj, pj8 pj8Var) {
        cr4.g(pj8Var, "module");
        if (obj == null) {
            return qk0.a(u79.a);
        }
        if (obj instanceof List) {
            return new rs(a((Collection) obj, pj8Var));
        }
        if (obj instanceof Object[]) {
            Object objY = st.y((Object[]) obj);
            return objY != null ? b(objY, pj8Var) : new rs(u79.a);
        }
        if (obj instanceof Set) {
            return new ka5(a((Collection) obj, pj8Var));
        }
        if (obj instanceof Map) {
            Map map = (Map) obj;
            return new da5(a(map.keySet(), pj8Var), a(map.values(), pj8Var));
        }
        Class<?> cls = obj.getClass();
        kw7 kw7Var = iw7.a;
        n25 n25VarB = pj8Var.b(kw7Var.getOrCreateKotlinClass(cls), ik2.a);
        if (n25VarB != null) {
            return n25VarB;
        }
        KClass orCreateKotlinClass = kw7Var.getOrCreateKotlinClass(obj.getClass());
        cr4.g(orCreateKotlinClass, "<this>");
        n25 n25VarD = lj8.d(orCreateKotlinClass);
        if (n25VarD != null) {
            return n25VarD;
        }
        r87.d(orCreateKotlinClass);
        throw null;
    }

    public static final n25 c(pj8 pj8Var, fz9 fz9Var) {
        cr4.g(pj8Var, "<this>");
        cr4.g(fz9Var, "typeInfo");
        KClass kClass = fz9Var.a;
        KType kType = fz9Var.b;
        if (kType != null) {
            n25 n25VarC = kType.getB().isEmpty() ? null : lj8.c(pj8Var, kType);
            if (n25VarC != null) {
                return n25VarC;
            }
        }
        n25 n25VarB = pj8Var.b(kClass, ik2.a);
        if (n25VarB != null) {
            return (kType == null || !kType.isMarkedNullable()) ? n25VarB : qk0.a(n25VarB);
        }
        n25 n25VarD = lj8.d(kClass);
        if (n25VarD != null) {
            return (kType == null || !kType.isMarkedNullable()) ? n25VarD : qk0.a(n25VarD);
        }
        r87.d(kClass);
        throw null;
    }
}
