package kotlinx.coroutines.channels;

import kotlin.coroutines.Continuation;
import kotlinx.coroutines.internal.UndeliveredElementException;
import kotlinx.coroutines.v1;

/* JADX INFO: loaded from: classes.dex */
public final class j extends b {

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final BufferOverflow f29341l;

    public j(int i10, BufferOverflow bufferOverflow, sd.l lVar) {
        super(i10, lVar);
        this.f29341l = bufferOverflow;
        if (bufferOverflow != BufferOverflow.f29297a) {
            if (i10 < 1) {
                throw new IllegalArgumentException(defpackage.e.e("Buffered channel capacity must be at least 1, but ", i10, " was specified").toString());
            }
        } else {
            throw new IllegalArgumentException(("This implementation does not support suspension for senders, use " + kotlin.jvm.internal.g.a(b.class).c() + " instead").toString());
        }
    }

    public final Object H(Object obj, boolean z10) {
        i iVar;
        sd.l lVar;
        UndeliveredElementException undeliveredElementExceptionB;
        BufferOverflow bufferOverflow = BufferOverflow.f29299c;
        kd.e eVar = kd.e.f29109a;
        if (this.f29341l == bufferOverflow) {
            Object objC = super.c(obj);
            if ((!(objC instanceof g)) || (objC instanceof f)) {
                return objC;
            }
            if (!z10 || (lVar = this.f29315b) == null || (undeliveredElementExceptionB = kotlinx.coroutines.internal.a.b(lVar, obj, null)) == null) {
                return eVar;
            }
            throw undeliveredElementExceptionB;
        }
        Object obj2 = c.f29319d;
        i iVar2 = (i) b.f29309g.get(this);
        while (true) {
            long andIncrement = b.f29305c.getAndIncrement(this);
            long j10 = andIncrement & 1152921504606846975L;
            boolean zU = u(andIncrement, false);
            int i10 = c.f29317b;
            long j11 = i10;
            long j12 = j10 / j11;
            int i11 = (int) (j10 % j11);
            if (iVar2.f29487c != j12) {
                i iVarD = b.d(this, j12, iVar2);
                if (iVarD != null) {
                    iVar = iVarD;
                } else if (zU) {
                    return new f(r());
                }
            } else {
                iVar = iVar2;
            }
            int iG = b.g(this, iVar, i11, obj, j10, obj2, zU);
            if (iG == 0) {
                iVar.a();
                return eVar;
            }
            if (iG == 1) {
                return eVar;
            }
            if (iG == 2) {
                if (zU) {
                    iVar.h();
                    return new f(r());
                }
                v1 v1Var = obj2 instanceof v1 ? (v1) obj2 : null;
                if (v1Var != null) {
                    v1Var.a(iVar, i11 + i10);
                }
                m((iVar.f29487c * j11) + ((long) i11));
                return eVar;
            }
            if (iG == 3) {
                throw new IllegalStateException("unexpected".toString());
            }
            if (iG == 4) {
                if (j10 < b.f29306d.get(this)) {
                    iVar.a();
                }
                return new f(r());
            }
            if (iG == 5) {
                iVar.a();
            }
            iVar2 = iVar;
        }
    }

    @Override // kotlinx.coroutines.channels.b, kotlinx.coroutines.channels.n
    public final Object c(Object obj) {
        return H(obj, false);
    }

    @Override // kotlinx.coroutines.channels.b, kotlinx.coroutines.channels.n
    public final Object i(Object obj, Continuation continuation) throws Throwable {
        UndeliveredElementException undeliveredElementExceptionB;
        Object objH = H(obj, true);
        boolean z10 = objH instanceof f;
        if (!z10) {
            return kd.e.f29109a;
        }
        if (z10) {
        }
        sd.l lVar = this.f29315b;
        if (lVar == null || (undeliveredElementExceptionB = kotlinx.coroutines.internal.a.b(lVar, obj, null)) == null) {
            throw r();
        }
        kotlin.a.a(undeliveredElementExceptionB, r());
        throw undeliveredElementExceptionB;
    }

    @Override // kotlinx.coroutines.channels.b
    public final boolean v() {
        return this.f29341l == BufferOverflow.f29298b;
    }
}
