package kotlinx.coroutines.internal;

import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlin.Result;
import kotlin.coroutines.Continuation;
import kotlin.jvm.functions.Function2;
import kotlinx.coroutines.g1;
import kotlinx.coroutines.q0;
import kotlinx.coroutines.q1;
import kotlinx.coroutines.t1;
import kotlinx.coroutines.y0;
import tv.danmaku.ijk.media.player.IjkMediaMeta;

/* JADX INFO: loaded from: classes.dex */
public abstract class a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final la.e f29449a = new la.e("NO_DECISION");

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final la.e f29451c = new la.e("UNDEFINED");

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final la.e f29452d = new la.e("REUSABLE_CLAIMED");

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final la.e f29453e = new la.e("CONDITION_FALSE");

    public static final sd.l a(final sd.l lVar, final Object obj, final kotlin.coroutines.h hVar) {
        return new sd.l() { // from class: kotlinx.coroutines.internal.OnUndeliveredElementKt$bindCancellationFun$1
            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            {
                super(1);
            }

            @Override // sd.l
            public final Object invoke(Object obj2) {
                sd.l lVar2 = lVar;
                Object obj3 = obj;
                kotlin.coroutines.h hVar2 = hVar;
                UndeliveredElementException undeliveredElementExceptionB = a.b(lVar2, obj3, null);
                if (undeliveredElementExceptionB != null) {
                    hb.a.g0(hVar2, undeliveredElementExceptionB);
                }
                return kd.e.f29109a;
            }
        };
    }

    public static final UndeliveredElementException b(sd.l lVar, Object obj, UndeliveredElementException undeliveredElementException) {
        try {
            lVar.invoke(obj);
        } catch (Throwable th) {
            if (undeliveredElementException == null || undeliveredElementException.getCause() == th) {
                return new UndeliveredElementException("Exception in undelivered element handler for " + obj, th);
            }
            kotlin.a.a(undeliveredElementException, th);
        }
        return undeliveredElementException;
    }

    public static final Object c(s sVar, long j10, Function2 function2) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater;
        while (true) {
            if (sVar.f29487c >= j10 && !sVar.c()) {
                return sVar;
            }
            Object obj = d.f29456a.get(sVar);
            la.e eVar = f29450b;
            if (obj == eVar) {
                return eVar;
            }
            s sVar2 = (s) ((d) obj);
            if (sVar2 == null) {
                sVar2 = (s) function2.invoke(Long.valueOf(sVar.f29487c + 1), sVar);
                do {
                    atomicReferenceFieldUpdater = d.f29456a;
                    if (atomicReferenceFieldUpdater.compareAndSet(sVar, null, sVar2)) {
                        if (sVar.c()) {
                            sVar.d();
                        }
                    }
                } while (atomicReferenceFieldUpdater.get(sVar) == null);
            }
            sVar = sVar2;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:36:0x00a6 A[Catch: all -> 0x0085, DONT_GENERATE, TryCatch #2 {all -> 0x0085, blocks: (B:20:0x0060, B:22:0x006e, B:24:0x0074, B:37:0x00a9, B:27:0x0087, B:29:0x0095, B:34:0x00a0, B:36:0x00a6, B:42:0x00b6, B:45:0x00bf, B:44:0x00bc, B:32:0x009b), top: B:57:0x0060, inners: #0 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final void d(Continuation continuation, Object obj, sd.l lVar) {
        y0 y0Var;
        if (!(continuation instanceof g)) {
            continuation.resumeWith(obj);
            return;
        }
        g gVar = (g) continuation;
        Throwable thA = Result.a(obj);
        Object rVar = thA == null ? lVar != null ? new kotlinx.coroutines.r(obj, lVar) : obj : new kotlinx.coroutines.q(thA, false);
        Continuation continuation2 = gVar.f29462e;
        kotlin.coroutines.h context = continuation2.getContext();
        kotlinx.coroutines.t tVar = gVar.f29461d;
        if (tVar.M(context)) {
            gVar.f29463f = rVar;
            gVar.f29356c = 1;
            tVar.L(continuation2.getContext(), gVar);
            return;
        }
        q0 q0VarA = q1.a();
        if (q0VarA.f29525c >= IjkMediaMeta.AV_CH_WIDE_RIGHT) {
            gVar.f29463f = rVar;
            gVar.f29356c = 1;
            kotlin.collections.h hVar = q0VarA.f29527e;
            if (hVar == null) {
                hVar = new kotlin.collections.h();
                q0VarA.f29527e = hVar;
            }
            hVar.addLast(gVar);
            return;
        }
        q0VarA.Q(true);
        try {
            y0Var = (y0) continuation2.getContext().m(kotlinx.coroutines.u.f29565b);
        } finally {
            try {
            } finally {
            }
        }
        if (y0Var == null || y0Var.isActive()) {
            Object obj2 = gVar.f29464g;
            kotlin.coroutines.h context2 = continuation2.getContext();
            Object objC = v.c(context2, obj2);
            t1 t1VarH0 = objC != v.f29490a ? kotlinx.coroutines.y.h0(continuation2, context2, objC) : null;
            try {
                continuation2.resumeWith(obj);
            } finally {
                if (t1VarH0 == null || t1VarH0.b0()) {
                    v.a(context2, objC);
                }
            }
        }
        CancellationException cancellationExceptionZ = ((g1) y0Var).z();
        gVar.b(rVar, cancellationExceptionZ);
        gVar.resumeWith(kotlin.a.b(cancellationExceptionZ));
        while (q0VarA.S()) {
        }
    }
}
