package defpackage;

import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.upstream.CmcdData;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.jvm.internal.ContinuationImpl;
import kotlin.coroutines.jvm.internal.DebugProbesKt;
import kotlin.coroutines.jvm.internal.SuspendLambda;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\u001e\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0003\b\u0011\u0018\u0000*\u0004\b\u0000\u0010\u00012\u00020\u00022\b\u0012\u0004\u0012\u00028\u00000\u00032\b\u0012\u0004\u0012\u00028\u00000\u0004:\u0001\bR\u0011\u0010\u0007\u001a\b\u0012\u0004\u0012\u00020\u00060\u00058\u0002X\u0082\u0004¨\u0006\t"}, d2 = {"Lif8;", "R", "Lks0;", "Lye8;", "Lmf8;", "Lww;", "", "state", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "kotlinx-coroutines-core"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@wl7
@vx8
public class if8<R> implements ks0, ye8<R>, mf8<R> {
    public static final /* synthetic */ AtomicReferenceFieldUpdater f = AtomicReferenceFieldUpdater.newUpdater(if8.class, Object.class, "state$volatile");
    public final gj1 a;
    public Object c;
    private volatile /* synthetic */ Object state$volatile = nf8.b;
    public ArrayList b = new ArrayList(2);
    public int d = -1;
    public Object e = nf8.e;

    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\b\u0080\u0004\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Lif8$a;", "", "kotlinx-coroutines-core"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @vx8
    public final class a {
        public final Object a;
        public final ir3 b;
        public final ir3 c;
        public final Object d;
        public final SuspendLambda e;
        public final ir3 f;
        public Object g;
        public int h = -1;

        public a(Object obj, ir3 ir3Var, ir3 ir3Var2, kg9 kg9Var, SuspendLambda suspendLambda, ir3 ir3Var3) {
            this.a = obj;
            this.b = ir3Var;
            this.c = ir3Var2;
            this.d = kg9Var;
            this.e = suspendLambda;
            this.f = ir3Var3;
        }

        public final void a() {
            Object obj = this.g;
            if (obj instanceof ne8) {
                ((ne8) obj).h(this.h, if8.this.a);
                return;
            }
            ld2 ld2Var = obj instanceof ld2 ? (ld2) obj : null;
            if (ld2Var != null) {
                ld2Var.c();
            }
        }
    }

    public if8(gj1 gj1Var) {
        this.a = gj1Var;
    }

    @Override // defpackage.bna
    public final void a(ne8 ne8Var, int i) {
        this.c = ne8Var;
        this.d = i;
    }

    @Override // defpackage.ks0
    public final void b(Throwable th) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f;
            Object obj = atomicReferenceFieldUpdater.get(this);
            if (obj == nf8.c) {
                return;
            }
            kg9 kg9Var = nf8.d;
            while (!atomicReferenceFieldUpdater.compareAndSet(this, obj, kg9Var)) {
                if (atomicReferenceFieldUpdater.get(this) != obj) {
                    break;
                }
            }
            ArrayList arrayList = this.b;
            if (arrayList == null) {
                return;
            }
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                ((a) it.next()).a();
            }
            this.e = nf8.e;
            this.b = null;
            return;
        }
    }

    @Override // defpackage.lf8
    public final void c(Object obj) {
        this.e = obj;
    }

    @Override // defpackage.lf8
    public final boolean d(Object obj, Object obj2) {
        return l(obj, obj2) == 0;
    }

    @Override // defpackage.lf8
    public final void e(ld2 ld2Var) {
        this.c = ld2Var;
    }

    public final Object f(ContinuationImpl continuationImpl) {
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f;
        Object obj = atomicReferenceFieldUpdater.get(this);
        cr4.e(obj, "null cannot be cast to non-null type kotlinx.coroutines.selects.SelectImplementation.ClauseData<R of kotlinx.coroutines.selects.SelectImplementation>");
        a aVar = (a) obj;
        Object obj2 = this.e;
        ArrayList<a> arrayList = this.b;
        if (arrayList != null) {
            for (a aVar2 : arrayList) {
                if (aVar2 != aVar) {
                    aVar2.a();
                }
            }
            atomicReferenceFieldUpdater.set(this, nf8.c);
            this.e = nf8.e;
            this.b = null;
        }
        Object objInvoke = aVar.c.invoke(aVar.a, aVar.d, obj2);
        Continuation continuation = aVar.e;
        return aVar.d == nf8.f ? ((sq3) continuation).invoke(continuationImpl) : ((gr3) continuation).invoke(objInvoke, continuationImpl);
    }

    public Object g(Continuation continuation) {
        return f.get(this) instanceof a ? f((ContinuationImpl) continuation) : h((ContinuationImpl) continuation);
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object h(ContinuationImpl continuationImpl) {
        jf8 jf8Var;
        Object obj;
        if8<R> if8Var;
        if (continuationImpl instanceof jf8) {
            jf8Var = (jf8) continuationImpl;
            int i = jf8Var.d;
            if ((i & Integer.MIN_VALUE) != 0) {
                jf8Var.d = i - Integer.MIN_VALUE;
            } else {
                jf8Var = new jf8(this, continuationImpl);
            }
        }
        Object obj2 = jf8Var.b;
        ck1 ck1Var = ck1.a;
        int i2 = jf8Var.d;
        if (i2 == 0) {
            s38.b(obj2);
            jf8Var.a = this;
            jf8Var.d = 1;
            ts0 ts0Var = new ts0(1, dr4.d(jf8Var));
            ts0Var.r();
            loop0: while (true) {
                AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f;
                Object obj3 = atomicReferenceFieldUpdater.get(this);
                kg9 kg9Var = nf8.b;
                obj = e6a.a;
                if (obj3 == kg9Var) {
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, obj3, ts0Var)) {
                        if (atomicReferenceFieldUpdater.get(this) != obj3) {
                            break;
                        }
                    }
                    ts0Var.u(this);
                    break loop0;
                }
                if (obj3 instanceof List) {
                    while (true) {
                        if (atomicReferenceFieldUpdater.compareAndSet(this, obj3, kg9Var)) {
                            Iterator it = ((Iterable) obj3).iterator();
                            while (it.hasNext()) {
                                a aVarI = i(it.next());
                                cr4.d(aVarI);
                                aVarI.g = null;
                                aVarI.h = -1;
                                k(aVarI, true);
                            }
                        } else if (atomicReferenceFieldUpdater.get(this) != obj3) {
                            break;
                        }
                    }
                } else {
                    if (!(obj3 instanceof a)) {
                        throw new IllegalStateException(("unexpected state: " + obj3).toString());
                    }
                    a aVar = (a) obj3;
                    Object obj4 = this.e;
                    ir3 ir3Var = aVar.f;
                    ts0Var.m(obj, ir3Var != null ? (ir3) ir3Var.invoke(this, aVar.d, obj4) : null);
                }
            }
            Object objQ = ts0Var.q();
            ck1 ck1Var2 = ck1.a;
            if (objQ == ck1Var2) {
                DebugProbesKt.probeCoroutineSuspended(jf8Var);
            }
            if (objQ == ck1Var2) {
                obj = objQ;
            }
            if (obj != ck1Var) {
                if8Var = this;
            }
        }
        if (i2 != 1) {
            if (i2 != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            s38.b(obj2);
            return obj2;
        }
        if8Var = jf8Var.a;
        s38.b(obj2);
        jf8Var.a = null;
        jf8Var.d = 2;
        Object objF = if8Var.f(jf8Var);
        return objF == ck1Var ? ck1Var : objF;
    }

    public final a i(Object obj) {
        ArrayList arrayList = this.b;
        Object obj2 = null;
        if (arrayList == null) {
            return null;
        }
        Iterator it = arrayList.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            Object next = it.next();
            if (((a) next).a == obj) {
                obj2 = next;
                break;
            }
        }
        a aVar = (a) obj2;
        if (aVar != null) {
            return aVar;
        }
        throw new IllegalStateException(("Clause with object " + obj + " is not found").toString());
    }

    /* JADX WARN: Multi-variable type inference failed */
    public void j(df8 df8Var, gr3 gr3Var) {
        k(new a(df8Var.getA(), df8Var.getB(), df8Var.getC(), null, (SuspendLambda) gr3Var, df8Var.b()), false);
    }

    public final void k(a aVar, boolean z) {
        Object obj = aVar.a;
        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f;
        if (atomicReferenceFieldUpdater.get(this) instanceof a) {
            return;
        }
        if (!z) {
            ArrayList arrayList = this.b;
            cr4.d(arrayList);
            if (!arrayList.isEmpty()) {
                Iterator it = arrayList.iterator();
                while (it.hasNext()) {
                    if (((a) it.next()).a == obj) {
                        throw new IllegalStateException(("Cannot use select clauses on the same object: " + obj).toString());
                    }
                }
            }
        }
        aVar.b.invoke(obj, this, aVar.d);
        if (this.e != nf8.e) {
            atomicReferenceFieldUpdater.set(this, aVar);
            return;
        }
        if (!z) {
            ArrayList arrayList2 = this.b;
            cr4.d(arrayList2);
            arrayList2.add(aVar);
        }
        aVar.g = this.c;
        aVar.h = this.d;
        this.c = null;
        this.d = -1;
    }

    public final int l(Object obj, Object obj2) {
        while (true) {
            AtomicReferenceFieldUpdater atomicReferenceFieldUpdater = f;
            Object obj3 = atomicReferenceFieldUpdater.get(this);
            if (!(obj3 instanceof ss0)) {
                if (cr4.b(obj3, nf8.c) || (obj3 instanceof a)) {
                    return 3;
                }
                if (cr4.b(obj3, nf8.d)) {
                    return 2;
                }
                if (cr4.b(obj3, nf8.b)) {
                    List listAm = y41.am(obj);
                    while (!atomicReferenceFieldUpdater.compareAndSet(this, obj3, listAm)) {
                        if (atomicReferenceFieldUpdater.get(this) != obj3) {
                            break;
                        }
                    }
                    return 1;
                }
                if (!(obj3 instanceof List)) {
                    throw new IllegalStateException(("Unexpected state: " + obj3).toString());
                }
                ArrayList arrayListAv = y41.av((Collection) obj3, obj);
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj3, arrayListAv)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj3) {
                        break;
                    }
                }
                return 1;
            }
            a aVarI = i(obj);
            if (aVarI != null) {
                ir3 ir3Var = aVarI.f;
                ir3 ir3Var2 = ir3Var != null ? (ir3) ir3Var.invoke(this, aVarI.d, obj2) : null;
                while (!atomicReferenceFieldUpdater.compareAndSet(this, obj3, aVarI)) {
                    if (atomicReferenceFieldUpdater.get(this) != obj3) {
                        break;
                    }
                }
                ss0 ss0Var = (ss0) obj3;
                this.e = obj2;
                ir3 ir3Var3 = nf8.a;
                kg9 kg9VarE = ss0Var.e(e6a.a, ir3Var2);
                if (kg9VarE == null) {
                    this.e = nf8.e;
                    return 2;
                }
                ss0Var.v(kg9VarE);
                return 0;
            }
            continue;
        }
    }
}
