package o6;

import d5.AbstractC0844a;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import n4.u0;
import p6.AbstractC1439e;
import p6.C1435a;

/* JADX INFO: renamed from: o6.a, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1381a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f15848a;

    public /* synthetic */ C1381a(int i) {
        this.f15848a = i;
    }

    public static void b(Type type, Class cls) {
        Class<?> clsD = AbstractC1380C.d(type);
        if (cls.isAssignableFrom(clsD)) {
            throw new IllegalArgumentException("No JsonAdapter for " + type + ", you should probably use " + cls.getSimpleName() + " instead of " + clsD.getSimpleName() + " (Moshi only supports the collection interfaces by default) or else register a custom JsonAdapter.");
        }
    }

    public final AbstractC1391k a(Type type, Set set, x xVar) {
        int i;
        AbstractC1380C c1383c;
        Class cls;
        boolean z8;
        Field[] fieldArr;
        boolean z9;
        Class clsD;
        Type[] actualTypeArguments;
        char c3;
        C1435a c1435aC;
        Class<?> cls2;
        Class<?> cls3;
        Constructor<?> declaredConstructor;
        Object[] objArr;
        Type typeF = type;
        Class cls4 = Integer.TYPE;
        boolean z10 = true;
        switch (this.f15848a) {
            case u0.f15495q /* 0 */:
                Type genericComponentType = typeF instanceof GenericArrayType ? ((GenericArrayType) typeF).getGenericComponentType() : typeF instanceof Class ? ((Class) typeF).getComponentType() : null;
                if (genericComponentType == null || !set.isEmpty()) {
                    return null;
                }
                Class clsD2 = AbstractC1380C.d(genericComponentType);
                xVar.getClass();
                return new C1382b(clsD2, xVar.b(genericComponentType, AbstractC1439e.f16150a, null)).c();
            case 1:
                if ((typeF instanceof Class) || (typeF instanceof ParameterizedType)) {
                    Class clsD3 = AbstractC1380C.d(typeF);
                    if (!clsD3.isInterface() && !clsD3.isEnum() && set.isEmpty()) {
                        if (AbstractC1439e.d(clsD3)) {
                            b(typeF, List.class);
                            b(typeF, Set.class);
                            b(typeF, Map.class);
                            b(typeF, Collection.class);
                            String str = "Platform " + clsD3;
                            if (typeF instanceof ParameterizedType) {
                                str = str + " in " + typeF;
                            }
                            throw new IllegalArgumentException(AbstractC0844a.l(str, " requires explicit JsonAdapter to be registered"));
                        }
                        if (clsD3.isAnonymousClass()) {
                            throw new IllegalArgumentException("Cannot serialize anonymous class ".concat(clsD3.getName()));
                        }
                        if (clsD3.isLocalClass()) {
                            throw new IllegalArgumentException("Cannot serialize local class ".concat(clsD3.getName()));
                        }
                        if (clsD3.getEnclosingClass() != null && !Modifier.isStatic(clsD3.getModifiers())) {
                            throw new IllegalArgumentException("Cannot serialize non-static nested class ".concat(clsD3.getName()));
                        }
                        if (Modifier.isAbstract(clsD3.getModifiers())) {
                            throw new IllegalArgumentException("Cannot serialize abstract class ".concat(clsD3.getName()));
                        }
                        Class<? extends Annotation> cls5 = AbstractC1439e.f16153d;
                        if (cls5 != null && clsD3.isAnnotationPresent(cls5)) {
                            throw new IllegalArgumentException("Cannot serialize Kotlin type " + clsD3.getName() + ". Reflective serialization of Kotlin classes without using kotlin-reflect has undefined and unexpected behavior. Please use KotlinJsonAdapterFactory from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.");
                        }
                        try {
                            try {
                                try {
                                    try {
                                        Constructor declaredConstructor2 = clsD3.getDeclaredConstructor(null);
                                        declaredConstructor2.setAccessible(true);
                                        c1383c = new C1383c(declaredConstructor2, clsD3);
                                    } catch (NoSuchMethodException unused) {
                                        Class<?> cls6 = Class.forName("sun.misc.Unsafe");
                                        Field declaredField = cls6.getDeclaredField("theUnsafe");
                                        declaredField.setAccessible(true);
                                        c1383c = new C1384d(cls6.getMethod("allocateInstance", Class.class), declaredField.get(null), clsD3);
                                    }
                                } catch (Exception unused2) {
                                    throw new IllegalArgumentException("cannot construct instances of ".concat(clsD3.getName()));
                                }
                            } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException unused3) {
                                Method declaredMethod = ObjectStreamClass.class.getDeclaredMethod("getConstructorId", Class.class);
                                declaredMethod.setAccessible(true);
                                int iIntValue = ((Integer) declaredMethod.invoke(null, Object.class)).intValue();
                                Method declaredMethod2 = ObjectStreamClass.class.getDeclaredMethod("newInstance", Class.class, cls4);
                                declaredMethod2.setAccessible(true);
                                c1383c = new C1385e(declaredMethod2, clsD3, iIntValue);
                            } catch (IllegalAccessException unused4) {
                                throw new AssertionError();
                            }
                            i = 0;
                            break;
                        } catch (IllegalAccessException unused5) {
                            throw new AssertionError();
                        } catch (NoSuchMethodException unused6) {
                            i = 0;
                            Method declaredMethod3 = ObjectInputStream.class.getDeclaredMethod("newInstance", Class.class, Class.class);
                            declaredMethod3.setAccessible(true);
                            c1383c = new C1383c(declaredMethod3, clsD3);
                            break;
                        } catch (InvocationTargetException e8) {
                            AbstractC1439e.g(e8);
                            throw null;
                        }
                        TreeMap treeMap = new TreeMap();
                        while (typeF != Object.class) {
                            Class clsD4 = AbstractC1380C.d(typeF);
                            boolean zD = AbstractC1439e.d(clsD4);
                            Field[] declaredFields = clsD4.getDeclaredFields();
                            int length = declaredFields.length;
                            int i5 = i;
                            while (i5 < length) {
                                Field field = declaredFields[i5];
                                int modifiers = field.getModifiers();
                                if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || !(Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers) || !zD)) {
                                    cls = clsD4;
                                    z8 = zD;
                                    fieldArr = declaredFields;
                                } else {
                                    InterfaceC1389i interfaceC1389i = (InterfaceC1389i) field.getAnnotation(InterfaceC1389i.class);
                                    if (interfaceC1389i == null || !interfaceC1389i.ignore()) {
                                        Type typeF2 = AbstractC1439e.f(typeF, clsD4, field.getGenericType(), new LinkedHashSet());
                                        Annotation[] annotations = field.getAnnotations();
                                        boolean z11 = z10;
                                        int length2 = annotations.length;
                                        cls = clsD4;
                                        int i8 = i;
                                        LinkedHashSet linkedHashSet = null;
                                        while (i8 < length2) {
                                            int i9 = i8;
                                            Annotation annotation = annotations[i9];
                                            boolean z12 = zD;
                                            Field[] fieldArr2 = declaredFields;
                                            if (annotation.annotationType().isAnnotationPresent(InterfaceC1393m.class)) {
                                                if (linkedHashSet == null) {
                                                    linkedHashSet = new LinkedHashSet();
                                                }
                                                LinkedHashSet linkedHashSet2 = linkedHashSet;
                                                linkedHashSet2.add(annotation);
                                                linkedHashSet = linkedHashSet2;
                                            }
                                            i8 = i9 + 1;
                                            zD = z12;
                                            declaredFields = fieldArr2;
                                        }
                                        z8 = zD;
                                        fieldArr = declaredFields;
                                        Set setUnmodifiableSet = linkedHashSet != null ? Collections.unmodifiableSet(linkedHashSet) : AbstractC1439e.f16150a;
                                        String name = field.getName();
                                        AbstractC1391k abstractC1391kB = xVar.b(typeF2, setUnmodifiableSet, name);
                                        field.setAccessible(z11);
                                        if (interfaceC1389i != null) {
                                            String strName = interfaceC1389i.name();
                                            if (!"\u0000".equals(strName)) {
                                                name = strName;
                                            }
                                        }
                                        C1386f c1386f = (C1386f) treeMap.put(name, new C1386f(name, field, abstractC1391kB));
                                        if (c1386f != null) {
                                            throw new IllegalArgumentException("Conflicting fields:\n    " + c1386f.f15864b + "\n    " + field);
                                        }
                                    } else {
                                        cls = clsD4;
                                        z8 = zD;
                                        fieldArr = declaredFields;
                                        z9 = z10;
                                        i5++;
                                        clsD4 = cls;
                                        z10 = z9;
                                        zD = z8;
                                        declaredFields = fieldArr;
                                    }
                                }
                                z9 = true;
                                i5++;
                                clsD4 = cls;
                                z10 = z9;
                                zD = z8;
                                declaredFields = fieldArr;
                            }
                            Class clsD5 = AbstractC1380C.d(typeF);
                            typeF = AbstractC1439e.f(typeF, clsD5, clsD5.getGenericSuperclass(), new LinkedHashSet());
                            z10 = z10;
                        }
                        return new C1387g(c1383c, treeMap).c();
                    }
                }
                return null;
            case 2:
                Class clsD6 = AbstractC1380C.d(typeF);
                if (set.isEmpty()) {
                    if (clsD6 == List.class || clsD6 == Collection.class) {
                        Type typeA = AbstractC1380C.a(typeF);
                        xVar.getClass();
                        return new C1388h(xVar.b(typeA, AbstractC1439e.f16150a, null), 0).c();
                    }
                    if (clsD6 == Set.class) {
                        Type typeA2 = AbstractC1380C.a(typeF);
                        xVar.getClass();
                        return new C1388h(xVar.b(typeA2, AbstractC1439e.f16150a, null), 1).c();
                    }
                }
                return null;
            case 3:
                if (!set.isEmpty() || (clsD = AbstractC1380C.d(typeF)) != Map.class) {
                    return null;
                }
                if (typeF == Properties.class) {
                    actualTypeArguments = new Type[]{String.class, String.class};
                } else {
                    if (!Map.class.isAssignableFrom(clsD)) {
                        throw new IllegalArgumentException();
                    }
                    Type typeF3 = AbstractC1439e.f(typeF, clsD, AbstractC1439e.c(typeF, clsD, Map.class), new LinkedHashSet());
                    if (!(typeF3 instanceof ParameterizedType)) {
                        c3 = 0;
                        actualTypeArguments = new Type[]{Object.class, Object.class};
                        return new C1382b(xVar, actualTypeArguments[c3], actualTypeArguments[1]).c();
                    }
                    actualTypeArguments = ((ParameterizedType) typeF3).getActualTypeArguments();
                }
                c3 = 0;
                return new C1382b(xVar, actualTypeArguments[c3], actualTypeArguments[1]).c();
            case 4:
                return null;
            default:
                if (set.isEmpty()) {
                    z zVar = AbstractC1380C.f15840b;
                    if (typeF == Boolean.TYPE) {
                        return zVar;
                    }
                    z zVar2 = AbstractC1380C.f15841c;
                    if (typeF == Byte.TYPE) {
                        return zVar2;
                    }
                    z zVar3 = AbstractC1380C.f15842d;
                    if (typeF == Character.TYPE) {
                        return zVar3;
                    }
                    z zVar4 = AbstractC1380C.f15843e;
                    if (typeF == Double.TYPE) {
                        return zVar4;
                    }
                    z zVar5 = AbstractC1380C.f15844f;
                    if (typeF == Float.TYPE) {
                        return zVar5;
                    }
                    z zVar6 = AbstractC1380C.f15845g;
                    if (typeF == cls4) {
                        return zVar6;
                    }
                    z zVar7 = AbstractC1380C.f15846h;
                    if (typeF == Long.TYPE) {
                        return zVar7;
                    }
                    z zVar8 = AbstractC1380C.i;
                    if (typeF == Short.TYPE) {
                        return zVar8;
                    }
                    if (typeF == Boolean.class) {
                        return zVar.c();
                    }
                    if (typeF == Byte.class) {
                        return zVar2.c();
                    }
                    if (typeF == Character.class) {
                        return zVar3.c();
                    }
                    if (typeF == Double.class) {
                        return zVar4.c();
                    }
                    if (typeF == Float.class) {
                        return zVar5.c();
                    }
                    if (typeF == Integer.class) {
                        return zVar6.c();
                    }
                    if (typeF == Long.class) {
                        return zVar7.c();
                    }
                    if (typeF == Short.class) {
                        return zVar8.c();
                    }
                    if (typeF == String.class) {
                        return AbstractC1380C.f15847j.c();
                    }
                    if (typeF == Object.class) {
                        return new C1379B(xVar).c();
                    }
                    Class clsD7 = AbstractC1380C.d(typeF);
                    Set set2 = AbstractC1439e.f16150a;
                    InterfaceC1392l interfaceC1392l = (InterfaceC1392l) clsD7.getAnnotation(InterfaceC1392l.class);
                    if (interfaceC1392l == null || !interfaceC1392l.generateAdapter()) {
                        c1435aC = null;
                    } else {
                        try {
                            try {
                                cls3 = Class.forName(clsD7.getName().replace("$", "_") + "JsonAdapter", true, clsD7.getClassLoader());
                            } catch (NoSuchMethodException e9) {
                                e = e9;
                                cls2 = null;
                            }
                            try {
                                if (typeF instanceof ParameterizedType) {
                                    Type[] actualTypeArguments2 = ((ParameterizedType) typeF).getActualTypeArguments();
                                    try {
                                        declaredConstructor = cls3.getDeclaredConstructor(x.class, Type[].class);
                                        objArr = new Object[]{xVar, actualTypeArguments2};
                                    } catch (NoSuchMethodException unused7) {
                                        declaredConstructor = cls3.getDeclaredConstructor(Type[].class);
                                        objArr = new Object[]{actualTypeArguments2};
                                    }
                                } else {
                                    try {
                                        declaredConstructor = cls3.getDeclaredConstructor(x.class);
                                        objArr = new Object[]{xVar};
                                    } catch (NoSuchMethodException unused8) {
                                        declaredConstructor = cls3.getDeclaredConstructor(null);
                                        objArr = new Object[0];
                                    }
                                }
                                declaredConstructor.setAccessible(true);
                                c1435aC = ((AbstractC1391k) declaredConstructor.newInstance(objArr)).c();
                            } catch (NoSuchMethodException e10) {
                                e = e10;
                                cls2 = cls3;
                                if ((typeF instanceof ParameterizedType) || cls2.getTypeParameters().length == 0) {
                                    throw new RuntimeException("Failed to find the generated JsonAdapter constructor for " + typeF, e);
                                }
                                throw new RuntimeException("Failed to find the generated JsonAdapter constructor for '" + typeF + "'. Suspiciously, the type was not parameterized but the target class '" + cls2.getCanonicalName() + "' is generic. Consider using Types#newParameterizedType() to define these missing type variables.", e);
                            }
                        } catch (ClassNotFoundException e11) {
                            throw new RuntimeException("Failed to find the generated JsonAdapter class for " + typeF, e11);
                        } catch (IllegalAccessException e12) {
                            throw new RuntimeException("Failed to access the generated JsonAdapter for " + typeF, e12);
                        } catch (InstantiationException e13) {
                            throw new RuntimeException("Failed to instantiate the generated JsonAdapter for " + typeF, e13);
                        } catch (InvocationTargetException e14) {
                            AbstractC1439e.g(e14);
                            throw null;
                        }
                    }
                    if (c1435aC != null) {
                        return c1435aC;
                    }
                    if (clsD7.isEnum()) {
                        return new C1378A(clsD7).c();
                    }
                    break;
                }
                return null;
        }
    }
}
