package kotlinx.coroutines.internal;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Comparator;
import java.util.Iterator;
import k1.i;

/* JADX INFO: compiled from: ExceptionsConstructor.kt */
/* JADX INFO: loaded from: classes.dex */
public final class h {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static final int f5197a = f(Throwable.class, -1);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static final kotlinx.coroutines.internal.e f5198b;

    /* JADX INFO: compiled from: Comparisons.kt */
    public static final class a<T> implements Comparator {
        /* JADX WARN: Multi-variable type inference failed */
        @Override // java.util.Comparator
        public final int compare(T t2, T t3) {
            return m1.b.a(Integer.valueOf(((Constructor) t3).getParameterTypes().length), Integer.valueOf(((Constructor) t2).getParameterTypes().length));
        }
    }

    /* JADX INFO: compiled from: ExceptionsConstructor.kt */
    static final class b extends w1.h implements v1.l {

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

        b() {
            super(1);
        }

        @Override // v1.l
        /* JADX INFO: renamed from: d, reason: merged with bridge method [inline-methods] */
        public final Void e(Throwable th) {
            return null;
        }
    }

    /* JADX INFO: compiled from: ExceptionsConstructor.kt */
    public static final class c extends w1.h implements v1.l<Throwable, Throwable> {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        final /* synthetic */ Constructor f5200e;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public c(Constructor constructor) {
            super(1);
            this.f5200e = constructor;
        }

        @Override // v1.l
        /* JADX INFO: renamed from: d, reason: merged with bridge method [inline-methods] */
        public final Throwable e(Throwable th) {
            Object objA;
            Object objNewInstance;
            try {
                i.a aVar = k1.i.f5165e;
                objNewInstance = this.f5200e.newInstance(th.getMessage(), th);
            } catch (Throwable th2) {
                i.a aVar2 = k1.i.f5165e;
                objA = k1.i.a(k1.j.a(th2));
            }
            if (objNewInstance == null) {
                throw new NullPointerException("null cannot be cast to non-null type kotlin.Throwable");
            }
            objA = k1.i.a((Throwable) objNewInstance);
            if (k1.i.c(objA)) {
                objA = null;
            }
            return (Throwable) objA;
        }
    }

    /* JADX INFO: compiled from: ExceptionsConstructor.kt */
    public static final class d extends w1.h implements v1.l<Throwable, Throwable> {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        final /* synthetic */ Constructor f5201e;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public d(Constructor constructor) {
            super(1);
            this.f5201e = constructor;
        }

        @Override // v1.l
        /* JADX INFO: renamed from: d, reason: merged with bridge method [inline-methods] */
        public final Throwable e(Throwable th) {
            Object objA;
            Object objNewInstance;
            try {
                i.a aVar = k1.i.f5165e;
                objNewInstance = this.f5201e.newInstance(th);
            } catch (Throwable th2) {
                i.a aVar2 = k1.i.f5165e;
                objA = k1.i.a(k1.j.a(th2));
            }
            if (objNewInstance == null) {
                throw new NullPointerException("null cannot be cast to non-null type kotlin.Throwable");
            }
            objA = k1.i.a((Throwable) objNewInstance);
            if (k1.i.c(objA)) {
                objA = null;
            }
            return (Throwable) objA;
        }
    }

    /* JADX INFO: compiled from: ExceptionsConstructor.kt */
    public static final class e extends w1.h implements v1.l<Throwable, Throwable> {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        final /* synthetic */ Constructor f5202e;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public e(Constructor constructor) {
            super(1);
            this.f5202e = constructor;
        }

        @Override // v1.l
        /* JADX INFO: renamed from: d, reason: merged with bridge method [inline-methods] */
        public final Throwable e(Throwable th) {
            Object objA;
            Object objNewInstance;
            try {
                i.a aVar = k1.i.f5165e;
                objNewInstance = this.f5202e.newInstance(th.getMessage());
            } catch (Throwable th2) {
                i.a aVar2 = k1.i.f5165e;
                objA = k1.i.a(k1.j.a(th2));
            }
            if (objNewInstance == null) {
                throw new NullPointerException("null cannot be cast to non-null type kotlin.Throwable");
            }
            Throwable th3 = (Throwable) objNewInstance;
            th3.initCause(th);
            objA = k1.i.a(th3);
            if (k1.i.c(objA)) {
                objA = null;
            }
            return (Throwable) objA;
        }
    }

    /* JADX INFO: compiled from: ExceptionsConstructor.kt */
    public static final class f extends w1.h implements v1.l<Throwable, Throwable> {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        final /* synthetic */ Constructor f5203e;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public f(Constructor constructor) {
            super(1);
            this.f5203e = constructor;
        }

        @Override // v1.l
        /* JADX INFO: renamed from: d, reason: merged with bridge method [inline-methods] */
        public final Throwable e(Throwable th) {
            Object objA;
            Object objNewInstance;
            try {
                i.a aVar = k1.i.f5165e;
                objNewInstance = this.f5203e.newInstance(new Object[0]);
            } catch (Throwable th2) {
                i.a aVar2 = k1.i.f5165e;
                objA = k1.i.a(k1.j.a(th2));
            }
            if (objNewInstance == null) {
                throw new NullPointerException("null cannot be cast to non-null type kotlin.Throwable");
            }
            Throwable th3 = (Throwable) objNewInstance;
            th3.initCause(th);
            objA = k1.i.a(th3);
            if (k1.i.c(objA)) {
                objA = null;
            }
            return (Throwable) objA;
        }
    }

    static {
        kotlinx.coroutines.internal.e eVar;
        try {
            eVar = j.a() ? j0.f5210a : kotlinx.coroutines.internal.d.f5180a;
        } catch (Throwable unused) {
            eVar = j0.f5210a;
        }
        f5198b = eVar;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final <E extends Throwable> v1.l<Throwable, Throwable> b(Class<E> cls) {
        b bVar = b.f5199e;
        if (f5197a != f(cls, 0)) {
            return bVar;
        }
        Iterator it = l1.e.j(cls.getConstructors(), new a()).iterator();
        while (it.hasNext()) {
            v1.l<Throwable, Throwable> lVarC = c((Constructor) it.next());
            if (lVarC != null) {
                return lVarC;
            }
        }
        return bVar;
    }

    private static final v1.l<Throwable, Throwable> c(Constructor<?> constructor) {
        Class<?>[] parameterTypes = constructor.getParameterTypes();
        int length = parameterTypes.length;
        if (length == 0) {
            return new f(constructor);
        }
        if (length != 1) {
            if (length == 2 && w1.g.a(parameterTypes[0], String.class) && w1.g.a(parameterTypes[1], Throwable.class)) {
                return new c(constructor);
            }
            return null;
        }
        Class<?> cls = parameterTypes[0];
        if (w1.g.a(cls, Throwable.class)) {
            return new d(constructor);
        }
        if (w1.g.a(cls, String.class)) {
            return new e(constructor);
        }
        return null;
    }

    private static final int d(Class<?> cls, int i3) {
        do {
            Field[] declaredFields = cls.getDeclaredFields();
            int length = declaredFields.length;
            int i4 = 0;
            int i5 = 0;
            while (i4 < length) {
                Field field = declaredFields[i4];
                i4++;
                if (!Modifier.isStatic(field.getModifiers())) {
                    i5++;
                }
            }
            i3 += i5;
            cls = cls.getSuperclass();
        } while (cls != null);
        return i3;
    }

    static /* synthetic */ int e(Class cls, int i3, int i4, Object obj) {
        if ((i4 & 1) != 0) {
            i3 = 0;
        }
        return d(cls, i3);
    }

    private static final int f(Class<?> cls, int i3) {
        Object objA;
        u1.a.c(cls);
        try {
            i.a aVar = k1.i.f5165e;
            objA = k1.i.a(Integer.valueOf(e(cls, 0, 1, null)));
        } catch (Throwable th) {
            i.a aVar2 = k1.i.f5165e;
            objA = k1.i.a(k1.j.a(th));
        }
        Integer numValueOf = Integer.valueOf(i3);
        if (k1.i.c(objA)) {
            objA = numValueOf;
        }
        return ((Number) objA).intValue();
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static final <E extends Throwable> E g(E e3) {
        Object objA;
        if (!(e3 instanceof d2.s)) {
            return (E) f5198b.a(e3.getClass()).e(e3);
        }
        try {
            i.a aVar = k1.i.f5165e;
            objA = k1.i.a(((d2.s) e3).a());
        } catch (Throwable th) {
            i.a aVar2 = k1.i.f5165e;
            objA = k1.i.a(k1.j.a(th));
        }
        if (k1.i.c(objA)) {
            objA = null;
        }
        return (E) objA;
    }
}
