package a5;

import I2.C0317a;
import S4.w;
import T0.z;
import V4.A;
import V4.AbstractC0786y;
import V4.C0763h0;
import V4.C0784w;
import V4.E;
import V4.E0;
import V4.F0;
import V4.InterfaceC0765i0;
import V4.L0;
import V4.Y;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import m3.AbstractC2467b;
import n2.AbstractC2613d;

/* JADX INFO: renamed from: a5.a, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public abstract class AbstractC0988a {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final C0317a f10810b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final C0317a f10811c;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final C0317a f10809a = new C0317a("CLOSED", 4);

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final C0317a f10812d = new C0317a("NO_THREAD_ELEMENTS", 4);

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final z f10814f = new z(29);

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final t f10815g = new t(0);

    static {
        int i6 = 4;
        f10810b = new C0317a("UNDEFINED", i6);
        f10811c = new C0317a("REUSABLE_CLAIMED", i6);
    }

    public static final void a(int i6) {
        if (i6 < 1) {
            throw new IllegalArgumentException(Z0.o.l("Expected positive parallelism level, but got ", i6).toString());
        }
    }

    public static final Object b(q qVar, long j4, J4.e eVar) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        while (true) {
            if (qVar.f10849c >= j4 && !qVar.d()) {
                return qVar;
            }
            Object obj = b.f10816a.get(qVar);
            C0317a c0317a = f10809a;
            if (obj == c0317a) {
                return c0317a;
            }
            q qVar2 = (q) ((b) obj);
            if (qVar2 == null) {
                qVar2 = (q) eVar.invoke(Long.valueOf(qVar.f10849c + 1), qVar);
                do {
                    atomicReferenceFieldUpdater = b.f10816a;
                    if (atomicReferenceFieldUpdater.compareAndSet(qVar, null, qVar2)) {
                        if (qVar.d()) {
                            qVar.e();
                        }
                    }
                } while (atomicReferenceFieldUpdater.get(qVar) == null);
            }
            qVar = qVar2;
        }
    }

    public static final q c(Object obj) {
        if (obj != f10809a) {
            return (q) obj;
        }
        throw new IllegalStateException("Does not contain segment");
    }

    public static final void d(A4.j jVar, Throwable th) {
        Throwable runtimeException;
        Iterator it = d.f10819a.iterator();
        while (it.hasNext()) {
            try {
                ((A) it.next()).handleException(jVar, th);
            } catch (Throwable th2) {
                if (th == th2) {
                    runtimeException = th;
                } else {
                    runtimeException = new RuntimeException("Exception while trying to handle coroutine exception", th2);
                    AbstractC2613d.b(runtimeException, th);
                }
                Thread threadCurrentThread = Thread.currentThread();
                threadCurrentThread.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread, runtimeException);
            }
        }
        try {
            AbstractC2613d.b(th, new e(jVar));
        } catch (Throwable unused) {
        }
        Thread threadCurrentThread2 = Thread.currentThread();
        threadCurrentThread2.getUncaughtExceptionHandler().uncaughtException(threadCurrentThread2, th);
    }

    public static final boolean e(Object obj) {
        return obj == f10809a;
    }

    public static final Object f(Object obj, Object obj2) {
        if (obj == null) {
            return obj2;
        }
        if (obj instanceof ArrayList) {
            ((ArrayList) obj).add(obj2);
            return obj;
        }
        ArrayList arrayList = new ArrayList(4);
        arrayList.add(obj);
        arrayList.add(obj2);
        return arrayList;
    }

    public static final void g(A4.j jVar, Object obj) {
        if (obj == f10812d) {
            return;
        }
        if (!(obj instanceof v)) {
            Object objFold = jVar.fold(null, f10814f);
            kotlin.jvm.internal.o.f(objFold, "null cannot be cast to non-null type kotlinx.coroutines.ThreadContextElement<kotlin.Any?>");
            ((E0) objFold).restoreThreadContext(jVar, obj);
            return;
        }
        v vVar = (v) obj;
        E0[] e0Arr = vVar.f10857c;
        int length = e0Arr.length - 1;
        if (length < 0) {
            return;
        }
        while (true) {
            int i6 = length - 1;
            E0 e0 = e0Arr[length];
            kotlin.jvm.internal.o.e(e0);
            e0.restoreThreadContext(jVar, vVar.f10856b[length]);
            if (i6 < 0) {
                return;
            } else {
                length = i6;
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:32:0x008a A[Catch: all -> 0x0069, DONT_GENERATE, TryCatch #2 {all -> 0x0069, blocks: (B:16:0x0049, B:18:0x0057, B:20:0x005d, B:33:0x008d, B:23:0x006b, B:25:0x0079, B:30:0x0084, B:32:0x008a, B:38:0x009a, B:41:0x00a3, B:40:0x00a0, B:28:0x007f), top: B:54:0x0049, inners: #0 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final void h(A4.d dVar, Object obj) {
        InterfaceC0765i0 interfaceC0765i0;
        if (!(dVar instanceof f)) {
            dVar.resumeWith(obj);
            return;
        }
        f fVar = (f) dVar;
        Throwable thA = w4.n.a(obj);
        Object c0784w = thA == null ? obj : new C0784w(thA, false);
        AbstractC0786y abstractC0786y = fVar.f10822d;
        C4.c cVar = fVar.f10823e;
        if (abstractC0786y.g(cVar.getContext())) {
            fVar.f10824f = c0784w;
            fVar.f8257c = 1;
            abstractC0786y.c(cVar.getContext(), fVar);
            return;
        }
        Y yA = F0.a();
        if (yA.f8276b >= 4294967296L) {
            fVar.f10824f = c0784w;
            fVar.f8257c = 1;
            yA.y(fVar);
            return;
        }
        yA.A(true);
        try {
            interfaceC0765i0 = (InterfaceC0765i0) cVar.getContext().get(C0763h0.f8306a);
        } finally {
            try {
            } finally {
            }
        }
        if (interfaceC0765i0 == null || interfaceC0765i0.isActive()) {
            Object obj2 = fVar.f10825j;
            A4.j context = cVar.getContext();
            Object objL = l(context, obj2);
            L0 l0G = objL != f10812d ? E.G(cVar, context, objL) : null;
            try {
                cVar.resumeWith(obj);
            } finally {
                if (l0G == null || l0G.X()) {
                    g(context, objL);
                }
            }
        }
        fVar.resumeWith(AbstractC2467b.d(interfaceC0765i0.getCancellationException()));
        while (yA.C()) {
        }
    }

    public static final long i(long j4, long j6, long j7, String str) {
        String property;
        int i6 = s.f10851a;
        try {
            property = System.getProperty(str);
        } catch (SecurityException unused) {
            property = null;
        }
        if (property == null) {
            return j4;
        }
        Long lC0 = w.C0(property);
        if (lC0 == null) {
            throw new IllegalStateException(("System property '" + str + "' has unrecognized value '" + property + '\'').toString());
        }
        long jLongValue = lC0.longValue();
        if (j6 <= jLongValue && jLongValue <= j7) {
            return jLongValue;
        }
        throw new IllegalStateException(("System property '" + str + "' should be in range " + j6 + ".." + j7 + ", but is '" + jLongValue + '\'').toString());
    }

    public static int j(int i6, int i7, String str) {
        return (int) i(i6, 1, (i7 & 8) != 0 ? Integer.MAX_VALUE : 2097150, str);
    }

    public static final Object k(A4.j jVar) {
        Object objFold = jVar.fold(0, f10813e);
        kotlin.jvm.internal.o.e(objFold);
        return objFold;
    }

    public static final Object l(A4.j jVar, Object obj) {
        if (obj == null) {
            obj = k(jVar);
        }
        return obj == 0 ? f10812d : obj instanceof Integer ? jVar.fold(new v(((Number) obj).intValue(), jVar), f10815g) : ((E0) obj).updateThreadContext(jVar);
    }
}
