package p025.p026;

import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import kotlinx.coroutines.CompletionHandlerException;
import p025.p026.p027.C1296;
import p138.p176.p177.p242.p246.p251.C4303;
import p402.C6542;
import p402.C6549;
import p402.p405.InterfaceC6575;
import p402.p405.InterfaceC6578;
import p402.p405.p406.EnumC6586;
import p402.p405.p407.p408.InterfaceC6592;
import p402.p411.p412.InterfaceC6614;

/* JADX INFO: renamed from: އ.֏.ޅ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public class C1307<T> extends AbstractC1339<T> implements InterfaceC1306<T>, InterfaceC6592 {

    /* JADX INFO: renamed from: ވ, reason: contains not printable characters */
    public static final AtomicIntegerFieldUpdater f4443 = AtomicIntegerFieldUpdater.newUpdater(C1307.class, "_decision");

    /* JADX INFO: renamed from: މ, reason: contains not printable characters */
    public static final AtomicReferenceFieldUpdater f4444 = AtomicReferenceFieldUpdater.newUpdater(C1307.class, Object.class, "_state");
    public volatile int _decision;
    public volatile Object _parentHandle;
    public volatile Object _state;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public final InterfaceC6578 f4445;

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public final InterfaceC6575<T> f4446;

    /* JADX WARN: Multi-variable type inference failed */
    public C1307(InterfaceC6575<? super T> interfaceC6575, int i) {
        super(i);
        this.f4446 = interfaceC6575;
        this.f4445 = interfaceC6575.mo2607();
        this._decision = 0;
        this._state = C1302.f4441;
        this._parentHandle = null;
    }

    public String toString() {
        return mo2638() + '(' + C4303.m7156((InterfaceC6575<?>) this.f4446) + "){" + this._state + "}@" + C4303.m7180(this);
    }

    @Override // p025.p026.InterfaceC1306
    /* JADX INFO: renamed from: ֏ */
    public Object mo2620(T t, Object obj) {
        Object obj2;
        do {
            obj2 = this._state;
            if (!(obj2 instanceof InterfaceC1379)) {
                if ((obj2 instanceof C1319) && ((C1319) obj2).f4457 == obj) {
                    return C1308.f4447;
                }
                return null;
            }
        } while (!f4444.compareAndSet(this, obj2, obj == null ? t : new C1319(obj, t)));
        m2635();
        return C1308.f4447;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public Throwable mo2626(InterfaceC1363 interfaceC1363) {
        return interfaceC1363.mo2686();
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final C1309 m2627(Object obj, int i) {
        Object obj2;
        do {
            obj2 = this._state;
            if (!(obj2 instanceof InterfaceC1379)) {
                if (obj2 instanceof C1309) {
                    C1309 c1309 = (C1309) obj2;
                    if (c1309 == null) {
                        throw null;
                    }
                    if (C1309.f4448.compareAndSet(c1309, 0, 1)) {
                        return c1309;
                    }
                }
                throw new IllegalStateException(("Already resumed, but proposed with update " + obj).toString());
            }
        } while (!f4444.compareAndSet(this, obj2, obj));
        m2635();
        m2628(i);
        return null;
    }

    @Override // p402.p405.InterfaceC6575
    /* JADX INFO: renamed from: ֏ */
    public InterfaceC6578 mo2607() {
        return this.f4445;
    }

    /* JADX WARN: Finally extract failed */
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m2628(int i) {
        boolean z;
        while (true) {
            int i2 = this._decision;
            if (i2 != 0) {
                if (i2 != 1) {
                    throw new IllegalStateException("Already resumed".toString());
                }
                z = false;
            } else if (f4443.compareAndSet(this, 0, 2)) {
                z = true;
                break;
            }
        }
        if (z) {
            return;
        }
        InterfaceC6575<T> interfaceC6575 = this.f4446;
        if (!(i == 0 || i == 1) || !(interfaceC6575 instanceof C1336) || C4303.m7169(i) != C4303.m7169(this.f4486)) {
            C4303.m7097(this, interfaceC6575, i);
            return;
        }
        AbstractC1323 abstractC1323 = ((C1336) interfaceC6575).f4481;
        InterfaceC6578 interfaceC6578Mo2607 = interfaceC6575.mo2607();
        if (abstractC1323.mo2652(interfaceC6578Mo2607)) {
            abstractC1323.mo2643(interfaceC6578Mo2607, this);
            return;
        }
        C1385 c1385 = C1385.f4539;
        AbstractC1345 abstractC1345M2728 = C1385.m2728();
        if (abstractC1345M2728.m2673()) {
            abstractC1345M2728.m2669((AbstractC1339<?>) this);
            return;
        }
        abstractC1345M2728.m2672(true);
        try {
            C4303.m7097(this, this.f4446, 2);
            do {
            } while (abstractC1345M2728.m2674());
        } catch (Throwable th) {
            try {
                m2664(th, (Throwable) null);
            } finally {
                abstractC1345M2728.m2670(true);
            }
        }
    }

    @Override // p402.p405.InterfaceC6575
    /* JADX INFO: renamed from: ֏ */
    public void mo2608(Object obj) {
        Throwable thM9768 = C6542.m9768(obj);
        if (thM9768 != null) {
            obj = new C1318(thM9768, false, 2, null);
        }
        m2627(obj, this.f4486);
    }

    @Override // p025.p026.AbstractC1339
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public void mo2629(Object obj, Throwable th) {
        if (obj instanceof C1320) {
            try {
                ((C1320) obj).f4460.mo1606(th);
            } catch (Throwable th2) {
                C4303.m7111(this.f4445, (Throwable) new CompletionHandlerException("Exception in cancellation handler for " + this, th2));
            }
        }
    }

    @Override // p025.p026.InterfaceC1306
    /* JADX INFO: renamed from: ֏ */
    public void mo2621(AbstractC1323 abstractC1323, T t) {
        InterfaceC6575<T> interfaceC6575 = this.f4446;
        if (!(interfaceC6575 instanceof C1336)) {
            interfaceC6575 = null;
        }
        C1336 c1336 = (C1336) interfaceC6575;
        m2627(t, (c1336 != null ? c1336.f4481 : null) == abstractC1323 ? 2 : this.f4486);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m2630(InterfaceC6614<? super Throwable, C6549> interfaceC6614, Object obj) {
        throw new IllegalStateException(("It's prohibited to register multiple handlers, tried to register " + interfaceC6614 + ", already has " + obj).toString());
    }

    @Override // p025.p026.InterfaceC1306
    /* JADX INFO: renamed from: ֏ */
    public boolean mo2622(Throwable th) {
        Object obj;
        boolean z;
        do {
            obj = this._state;
            if (!(obj instanceof InterfaceC1379)) {
                return false;
            }
            z = obj instanceof AbstractC1304;
        } while (!f4444.compareAndSet(this, obj, new C1309(this, th, z)));
        if (z) {
            try {
                ((AbstractC1304) obj).mo2619(th);
            } catch (Throwable th2) {
                C4303.m7111(this.f4445, (Throwable) new CompletionHandlerException("Exception in cancellation handler for " + this, th2));
            }
        }
        m2635();
        m2628(0);
        return true;
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // p025.p026.AbstractC1339
    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public <T> T mo2631(Object obj) {
        return obj instanceof C1319 ? (T) ((C1319) obj).f4458 : obj instanceof C1320 ? (T) ((C1320) obj).f4459 : obj;
    }

    @Override // p025.p026.InterfaceC1306
    /* JADX INFO: renamed from: ؠ */
    public Object mo2623(Throwable th) {
        Object obj;
        do {
            obj = this._state;
            if (!(obj instanceof InterfaceC1379)) {
                return null;
            }
        } while (!f4444.compareAndSet(this, obj, new C1318(th, false, 2, null)));
        m2635();
        return C1308.f4447;
    }

    @Override // p025.p026.AbstractC1339
    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final InterfaceC6575<T> mo2632() {
        return this.f4446;
    }

    @Override // p025.p026.InterfaceC1306
    /* JADX INFO: renamed from: ؠ */
    public void mo2624(InterfaceC6614<? super Throwable, C6549> interfaceC6614) {
        Object obj;
        Object c1360 = null;
        do {
            obj = this._state;
            if (!(obj instanceof C1302)) {
                if (obj instanceof AbstractC1304) {
                    m2630(interfaceC6614, obj);
                    throw null;
                }
                if (obj instanceof C1309) {
                    C1309 c1309 = (C1309) obj;
                    if (c1309 == null) {
                        throw null;
                    }
                    if (!C1318.f4455.compareAndSet(c1309, 0, 1)) {
                        m2630(interfaceC6614, obj);
                        throw null;
                    }
                    try {
                        if (!(obj instanceof C1318)) {
                            obj = null;
                        }
                        C1318 c1318 = (C1318) obj;
                        interfaceC6614.mo1606(c1318 != null ? c1318.f4456 : null);
                        return;
                    } catch (Throwable th) {
                        C4303.m7111(this.f4445, (Throwable) new CompletionHandlerException("Exception in cancellation handler for " + this, th));
                        return;
                    }
                }
                return;
            }
            if (c1360 == null) {
                c1360 = interfaceC6614 instanceof AbstractC1304 ? (AbstractC1304) interfaceC6614 : new C1360(interfaceC6614);
            }
        } while (!f4444.compareAndSet(this, obj, c1360));
    }

    @Override // p025.p026.AbstractC1339
    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public Object mo2633() {
        return this._state;
    }

    @Override // p025.p026.InterfaceC1306
    /* JADX INFO: renamed from: ހ */
    public void mo2625(Object obj) {
        m2628(this.f4486);
    }

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final void m2634() {
        InterfaceC1341 interfaceC1341 = (InterfaceC1341) this._parentHandle;
        if (interfaceC1341 != null) {
            interfaceC1341.mo2666();
        }
        this._parentHandle = C1378.f4536;
    }

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public final void m2635() {
        if (m2637()) {
            return;
        }
        m2634();
    }

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final Object m2636() {
        boolean z;
        InterfaceC1363 interfaceC1363;
        m2639();
        while (true) {
            int i = this._decision;
            z = false;
            if (i != 0) {
                if (i != 2) {
                    throw new IllegalStateException("Already suspended".toString());
                }
            } else if (f4443.compareAndSet(this, 0, 1)) {
                z = true;
                break;
            }
        }
        if (z) {
            return EnumC6586.COROUTINE_SUSPENDED;
        }
        Object obj = this._state;
        if (obj instanceof C1318) {
            throw ((C1318) obj).f4456;
        }
        if (this.f4486 != 1 || (interfaceC1363 = (InterfaceC1363) this.f4445.get(InterfaceC1363.f4514)) == null || interfaceC1363.mo2610()) {
            return mo2631(obj);
        }
        CancellationException cancellationExceptionMo2686 = interfaceC1363.mo2686();
        mo2629(obj, (Throwable) cancellationExceptionMo2686);
        throw cancellationExceptionMo2686;
    }

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final boolean m2637() {
        InterfaceC6575<T> interfaceC6575 = this.f4446;
        if (!(interfaceC6575 instanceof C1336)) {
            return false;
        }
        Object obj = ((C1336) interfaceC6575)._reusableCancellableContinuation;
        return obj != null && (!(obj instanceof C1307) || obj == this);
    }

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public String mo2638() {
        return "CancellableContinuation";
    }

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public final void m2639() {
        InterfaceC1363 interfaceC1363;
        boolean z = !(this._state instanceof InterfaceC1379);
        if (this.f4486 == 0) {
            InterfaceC6575<T> interfaceC6575 = this.f4446;
            Throwable th = null;
            if (!(interfaceC6575 instanceof C1336)) {
                interfaceC6575 = null;
            }
            C1336 c1336 = (C1336) interfaceC6575;
            if (c1336 != null) {
                while (true) {
                    Object obj = c1336._reusableCancellableContinuation;
                    C1296 c1296 = C1337.f4484;
                    if (obj == c1296) {
                        if (C1336.f4477.compareAndSet(c1336, c1296, this)) {
                            break;
                        }
                    } else if (obj != null) {
                        if (!(obj instanceof Throwable)) {
                            throw new IllegalStateException(("Inconsistent state " + obj).toString());
                        }
                        if (!C1336.f4477.compareAndSet(c1336, obj, null)) {
                            throw new IllegalArgumentException("Failed requirement.".toString());
                        }
                        th = (Throwable) obj;
                    }
                }
                if (th != null) {
                    if (!z) {
                        mo2622(th);
                    }
                    z = true;
                }
            }
        }
        if (z || ((InterfaceC1341) this._parentHandle) != null || (interfaceC1363 = (InterfaceC1363) this.f4446.mo2607().get(InterfaceC1363.f4514)) == null) {
            return;
        }
        interfaceC1363.start();
        InterfaceC1341 interfaceC1341M7051 = C4303.m7051(interfaceC1363, true, false, (InterfaceC6614) new C1310(interfaceC1363, this), 2, (Object) null);
        this._parentHandle = interfaceC1341M7051;
        if (!(true ^ (this._state instanceof InterfaceC1379)) || m2637()) {
            return;
        }
        interfaceC1341M7051.mo2666();
        this._parentHandle = C1378.f4536;
    }
}
