package wp;

import java.util.Arrays;
import rm.n0;
import rm.x;
import tp.b1;
import tp.u1;
import tp.y1;

/* loaded from: classes3.dex */
public class y extends xp.b implements s, e, xp.m {
    private int A;

    /* renamed from: t, reason: collision with root package name */
    private final int f49689t;

    /* renamed from: u, reason: collision with root package name */
    private final int f49690u;

    /* renamed from: v, reason: collision with root package name */
    private final vp.a f49691v;

    /* renamed from: w, reason: collision with root package name */
    private Object[] f49692w;

    /* renamed from: x, reason: collision with root package name */
    private long f49693x;

    /* renamed from: y, reason: collision with root package name */
    private long f49694y;

    /* renamed from: z, reason: collision with root package name */
    private int f49695z;

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes3.dex */
    public static final class a implements b1 {

        /* renamed from: p, reason: collision with root package name */
        public final y f49696p;

        /* renamed from: q, reason: collision with root package name */
        public long f49697q;

        /* renamed from: r, reason: collision with root package name */
        public final Object f49698r;

        /* renamed from: s, reason: collision with root package name */
        public final vm.d f49699s;

        public a(y yVar, long j10, Object obj, vm.d dVar) {
            this.f49696p = yVar;
            this.f49697q = j10;
            this.f49698r = obj;
            this.f49699s = dVar;
        }

        @Override // tp.b1
        public void a() {
            this.f49696p.y(this);
        }
    }

    /* loaded from: classes3.dex */
    public /* synthetic */ class b {

        /* renamed from: a, reason: collision with root package name */
        public static final /* synthetic */ int[] f49700a;

        static {
            int[] iArr = new int[vp.a.values().length];
            try {
                iArr[vp.a.SUSPEND.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                iArr[vp.a.DROP_LATEST.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                iArr[vp.a.DROP_OLDEST.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            f49700a = iArr;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public static final class c extends kotlin.coroutines.jvm.internal.d {

        /* renamed from: p, reason: collision with root package name */
        Object f49701p;

        /* renamed from: q, reason: collision with root package name */
        Object f49702q;

        /* renamed from: r, reason: collision with root package name */
        Object f49703r;

        /* renamed from: s, reason: collision with root package name */
        Object f49704s;

        /* renamed from: t, reason: collision with root package name */
        /* synthetic */ Object f49705t;

        /* renamed from: v, reason: collision with root package name */
        int f49707v;

        c(vm.d dVar) {
            super(dVar);
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final Object invokeSuspend(Object obj) {
            this.f49705t = obj;
            this.f49707v |= Integer.MIN_VALUE;
            return y.A(y.this, null, this);
        }
    }

    public y(int i10, int i11, vp.a aVar) {
        this.f49689t = i10;
        this.f49690u = i11;
        this.f49691v = aVar;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:20:0x009a A[Catch: all -> 0x005e, TryCatch #2 {all -> 0x005e, blocks: (B:14:0x003b, B:18:0x0092, B:20:0x009a, B:28:0x00ad, B:29:0x00b0, B:36:0x005a), top: B:7:0x0022 }] */
    /* JADX WARN: Removed duplicated region for block: B:26:0x00ab A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:51:0x0077  */
    /* JADX WARN: Removed duplicated region for block: B:9:0x0024  */
    /* JADX WARN: Type inference failed for: r5v1, types: [xp.b] */
    /* JADX WARN: Type inference failed for: r5v14 */
    /* JADX WARN: Type inference failed for: r5v2 */
    /* JADX WARN: Type inference failed for: r5v4, types: [wp.y, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r5v5 */
    /* JADX WARN: Type inference failed for: r5v7 */
    /* JADX WARN: Type inference failed for: r9v0, types: [wp.f] */
    /* JADX WARN: Type inference failed for: r9v1 */
    /* JADX WARN: Type inference failed for: r9v19 */
    /* JADX WARN: Type inference failed for: r9v2, types: [xp.d] */
    /* JADX WARN: Type inference failed for: r9v20 */
    /* JADX WARN: Type inference failed for: r9v21 */
    /* JADX WARN: Type inference failed for: r9v3 */
    /* JADX WARN: Type inference failed for: r9v4 */
    /* JADX WARN: Type inference failed for: r9v5, types: [wp.a0, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r9v9, types: [wp.a0] */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:26:0x00be -> B:15:0x003e). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    static /* synthetic */ Object A(y yVar, f fVar, vm.d dVar) {
        c cVar;
        Object f10;
        int i10;
        ?? r52;
        f fVar2;
        u1 u1Var;
        u1 u1Var2;
        f fVar3;
        Object U;
        a0 a0Var;
        try {
            try {
                if (dVar instanceof c) {
                    cVar = (c) dVar;
                    int i11 = cVar.f49707v;
                    if ((i11 & Integer.MIN_VALUE) != 0) {
                        cVar.f49707v = i11 - Integer.MIN_VALUE;
                        Object obj = cVar.f49705t;
                        f10 = wm.d.f();
                        i10 = cVar.f49707v;
                        if (i10 != 0) {
                            rm.y.b(obj);
                            fVar2 = fVar;
                            fVar = (a0) yVar.h();
                        } else {
                            if (i10 != 1) {
                                if (i10 == 2) {
                                    u1Var2 = (u1) cVar.f49704s;
                                    a0 a0Var2 = (a0) cVar.f49703r;
                                    fVar3 = (f) cVar.f49702q;
                                    y yVar2 = (y) cVar.f49701p;
                                    rm.y.b(obj);
                                    r52 = yVar2;
                                    fVar = a0Var2;
                                    do {
                                        U = r52.U(fVar);
                                        if (U == z.f49708a) {
                                        }
                                    } while (r52.x(fVar, cVar) != f10);
                                    return f10;
                                }
                                if (i10 != 3) {
                                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                                }
                                u1Var2 = (u1) cVar.f49704s;
                                a0 a0Var3 = (a0) cVar.f49703r;
                                fVar3 = (f) cVar.f49702q;
                                y yVar3 = (y) cVar.f49701p;
                                rm.y.b(obj);
                                y yVar4 = yVar3;
                                a0 a0Var4 = a0Var3;
                                fVar2 = fVar3;
                                u1Var = u1Var2;
                                yVar = yVar4;
                                a0Var = a0Var4;
                                r52 = yVar;
                                u1Var2 = u1Var;
                                fVar3 = fVar2;
                                fVar = a0Var;
                                do {
                                    U = r52.U(fVar);
                                    if (U == z.f49708a) {
                                        if (u1Var2 != null) {
                                            y1.i(u1Var2);
                                        }
                                        cVar.f49701p = r52;
                                        cVar.f49702q = fVar3;
                                        cVar.f49703r = fVar;
                                        cVar.f49704s = u1Var2;
                                        cVar.f49707v = 3;
                                        yVar4 = r52;
                                        a0Var4 = fVar;
                                        if (fVar3.emit(U, cVar) == f10) {
                                            return f10;
                                        }
                                        fVar2 = fVar3;
                                        u1Var = u1Var2;
                                        yVar = yVar4;
                                        a0Var = a0Var4;
                                        r52 = yVar;
                                        u1Var2 = u1Var;
                                        fVar3 = fVar2;
                                        fVar = a0Var;
                                        U = r52.U(fVar);
                                        if (U == z.f49708a) {
                                            cVar.f49701p = r52;
                                            cVar.f49702q = fVar3;
                                            cVar.f49703r = fVar;
                                            cVar.f49704s = u1Var2;
                                            cVar.f49707v = 2;
                                        }
                                    }
                                } while (r52.x(fVar, cVar) != f10);
                                return f10;
                            }
                            fVar = (a0) cVar.f49703r;
                            f fVar4 = (f) cVar.f49702q;
                            y yVar5 = (y) cVar.f49701p;
                            try {
                                rm.y.b(obj);
                                fVar2 = fVar4;
                                yVar = yVar5;
                                fVar = fVar;
                            } catch (Throwable th2) {
                                th = th2;
                                r52 = yVar5;
                                r52.k(fVar);
                                throw th;
                            }
                        }
                        u1Var = (u1) cVar.getContext().d(u1.f36592j);
                        a0Var = fVar;
                        r52 = yVar;
                        u1Var2 = u1Var;
                        fVar3 = fVar2;
                        fVar = a0Var;
                        do {
                            U = r52.U(fVar);
                            if (U == z.f49708a) {
                            }
                        } while (r52.x(fVar, cVar) != f10);
                        return f10;
                    }
                }
                u1Var = (u1) cVar.getContext().d(u1.f36592j);
                a0Var = fVar;
                r52 = yVar;
                u1Var2 = u1Var;
                fVar3 = fVar2;
                fVar = a0Var;
                do {
                    U = r52.U(fVar);
                    if (U == z.f49708a) {
                    }
                } while (r52.x(fVar, cVar) != f10);
                return f10;
            } catch (Throwable th3) {
                r52 = yVar;
                th = th3;
                r52.k(fVar);
                throw th;
            }
            if (i10 != 0) {
            }
        } catch (Throwable th4) {
            th = th4;
        }
        cVar = new c(dVar);
        Object obj2 = cVar.f49705t;
        f10 = wm.d.f();
        i10 = cVar.f49707v;
    }

    private final void B(long j10) {
        xp.d[] f10;
        if (xp.b.e(this) != 0 && (f10 = xp.b.f(this)) != null) {
            for (xp.d dVar : f10) {
                if (dVar != null) {
                    a0 a0Var = (a0) dVar;
                    long j11 = a0Var.f49558a;
                    if (j11 >= 0 && j11 < j10) {
                        a0Var.f49558a = j10;
                    }
                }
            }
        }
        this.f49694y = j10;
    }

    private final void E() {
        Object[] objArr = this.f49692w;
        kotlin.jvm.internal.t.d(objArr);
        z.g(objArr, K(), null);
        this.f49695z--;
        long K = K() + 1;
        if (this.f49693x < K) {
            this.f49693x = K;
        }
        if (this.f49694y < K) {
            B(K);
        }
    }

    static /* synthetic */ Object F(y yVar, Object obj, vm.d dVar) {
        Object f10;
        if (yVar.d(obj)) {
            return n0.f34904a;
        }
        Object G = yVar.G(obj, dVar);
        f10 = wm.d.f();
        return G == f10 ? G : n0.f34904a;
    }

    private final Object G(Object obj, vm.d dVar) {
        vm.d d10;
        vm.d[] dVarArr;
        a aVar;
        Object f10;
        Object f11;
        d10 = wm.c.d(dVar);
        tp.p pVar = new tp.p(d10, 1);
        pVar.u();
        vm.d[] dVarArr2 = xp.c.f50958a;
        synchronized (this) {
            if (R(obj)) {
                x.a aVar2 = rm.x.f34910p;
                pVar.resumeWith(rm.x.a(n0.f34904a));
                dVarArr = I(dVarArr2);
                aVar = null;
            } else {
                a aVar3 = new a(this, P() + K(), obj, pVar);
                H(aVar3);
                this.A++;
                if (this.f49690u == 0) {
                    dVarArr2 = I(dVarArr2);
                }
                dVarArr = dVarArr2;
                aVar = aVar3;
            }
        }
        if (aVar != null) {
            tp.r.a(pVar, aVar);
        }
        for (vm.d dVar2 : dVarArr) {
            if (dVar2 != null) {
                x.a aVar4 = rm.x.f34910p;
                dVar2.resumeWith(rm.x.a(n0.f34904a));
            }
        }
        Object r10 = pVar.r();
        f10 = wm.d.f();
        if (r10 == f10) {
            kotlin.coroutines.jvm.internal.h.c(dVar);
        }
        f11 = wm.d.f();
        return r10 == f11 ? r10 : n0.f34904a;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void H(Object obj) {
        int P = P();
        Object[] objArr = this.f49692w;
        if (objArr == null) {
            objArr = Q(null, 0, 2);
        } else if (P >= objArr.length) {
            objArr = Q(objArr, P, objArr.length * 2);
        }
        z.g(objArr, K() + P, obj);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r11v6, types: [java.lang.Object[], java.lang.Object] */
    public final vm.d[] I(vm.d[] dVarArr) {
        xp.d[] f10;
        a0 a0Var;
        vm.d dVar;
        int length = dVarArr.length;
        if (xp.b.e(this) != 0 && (f10 = xp.b.f(this)) != null) {
            int length2 = f10.length;
            int i10 = 0;
            dVarArr = dVarArr;
            while (i10 < length2) {
                xp.d dVar2 = f10[i10];
                if (dVar2 != null && (dVar = (a0Var = (a0) dVar2).f49559b) != null && T(a0Var) >= 0) {
                    int length3 = dVarArr.length;
                    dVarArr = dVarArr;
                    if (length >= length3) {
                        ?? copyOf = Arrays.copyOf(dVarArr, Math.max(2, dVarArr.length * 2));
                        kotlin.jvm.internal.t.f(copyOf, "copyOf(this, newSize)");
                        dVarArr = copyOf;
                    }
                    dVarArr[length] = dVar;
                    a0Var.f49559b = null;
                    length++;
                }
                i10++;
                dVarArr = dVarArr;
            }
        }
        return dVarArr;
    }

    private final long J() {
        return K() + this.f49695z;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final long K() {
        return Math.min(this.f49694y, this.f49693x);
    }

    private final Object M(long j10) {
        Object f10;
        Object[] objArr = this.f49692w;
        kotlin.jvm.internal.t.d(objArr);
        f10 = z.f(objArr, j10);
        return f10 instanceof a ? ((a) f10).f49698r : f10;
    }

    private final long N() {
        return K() + this.f49695z + this.A;
    }

    private final int O() {
        return (int) ((K() + this.f49695z) - this.f49693x);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final int P() {
        return this.f49695z + this.A;
    }

    private final Object[] Q(Object[] objArr, int i10, int i11) {
        Object f10;
        if (!(i11 > 0)) {
            throw new IllegalStateException("Buffer size overflow".toString());
        }
        Object[] objArr2 = new Object[i11];
        this.f49692w = objArr2;
        if (objArr == null) {
            return objArr2;
        }
        long K = K();
        for (int i12 = 0; i12 < i10; i12++) {
            long j10 = i12 + K;
            f10 = z.f(objArr, j10);
            z.g(objArr2, j10, f10);
        }
        return objArr2;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final boolean R(Object obj) {
        if (l() == 0) {
            return S(obj);
        }
        if (this.f49695z >= this.f49690u && this.f49694y <= this.f49693x) {
            int i10 = b.f49700a[this.f49691v.ordinal()];
            if (i10 == 1) {
                return false;
            }
            if (i10 == 2) {
                return true;
            }
        }
        H(obj);
        int i11 = this.f49695z + 1;
        this.f49695z = i11;
        if (i11 > this.f49690u) {
            E();
        }
        if (O() > this.f49689t) {
            V(this.f49693x + 1, this.f49694y, J(), N());
        }
        return true;
    }

    private final boolean S(Object obj) {
        if (this.f49689t == 0) {
            return true;
        }
        H(obj);
        int i10 = this.f49695z + 1;
        this.f49695z = i10;
        if (i10 > this.f49689t) {
            E();
        }
        this.f49694y = K() + this.f49695z;
        return true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final long T(a0 a0Var) {
        long j10 = a0Var.f49558a;
        if (j10 < J()) {
            return j10;
        }
        if (this.f49690u <= 0 && j10 <= K() && this.A != 0) {
            return j10;
        }
        return -1L;
    }

    private final Object U(a0 a0Var) {
        Object obj;
        vm.d[] dVarArr = xp.c.f50958a;
        synchronized (this) {
            long T = T(a0Var);
            if (T < 0) {
                obj = z.f49708a;
            } else {
                long j10 = a0Var.f49558a;
                Object M = M(T);
                a0Var.f49558a = T + 1;
                dVarArr = W(j10);
                obj = M;
            }
        }
        for (vm.d dVar : dVarArr) {
            if (dVar != null) {
                x.a aVar = rm.x.f34910p;
                dVar.resumeWith(rm.x.a(n0.f34904a));
            }
        }
        return obj;
    }

    private final void V(long j10, long j11, long j12, long j13) {
        long min = Math.min(j11, j10);
        for (long K = K(); K < min; K++) {
            Object[] objArr = this.f49692w;
            kotlin.jvm.internal.t.d(objArr);
            z.g(objArr, K, null);
        }
        this.f49693x = j10;
        this.f49694y = j11;
        this.f49695z = (int) (j12 - min);
        this.A = (int) (j13 - j12);
    }

    private final Object x(a0 a0Var, vm.d dVar) {
        vm.d d10;
        Object f10;
        Object f11;
        d10 = wm.c.d(dVar);
        tp.p pVar = new tp.p(d10, 1);
        pVar.u();
        synchronized (this) {
            if (T(a0Var) < 0) {
                a0Var.f49559b = pVar;
            } else {
                x.a aVar = rm.x.f34910p;
                pVar.resumeWith(rm.x.a(n0.f34904a));
            }
            n0 n0Var = n0.f34904a;
        }
        Object r10 = pVar.r();
        f10 = wm.d.f();
        if (r10 == f10) {
            kotlin.coroutines.jvm.internal.h.c(dVar);
        }
        f11 = wm.d.f();
        return r10 == f11 ? r10 : n0.f34904a;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void y(a aVar) {
        Object f10;
        synchronized (this) {
            if (aVar.f49697q < K()) {
                return;
            }
            Object[] objArr = this.f49692w;
            kotlin.jvm.internal.t.d(objArr);
            f10 = z.f(objArr, aVar.f49697q);
            if (f10 != aVar) {
                return;
            }
            z.g(objArr, aVar.f49697q, z.f49708a);
            z();
            n0 n0Var = n0.f34904a;
        }
    }

    private final void z() {
        Object f10;
        if (this.f49690u != 0 || this.A > 1) {
            Object[] objArr = this.f49692w;
            kotlin.jvm.internal.t.d(objArr);
            while (this.A > 0) {
                f10 = z.f(objArr, (K() + P()) - 1);
                if (f10 != z.f49708a) {
                    return;
                }
                this.A--;
                z.g(objArr, K() + P(), null);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // xp.b
    /* renamed from: C, reason: merged with bridge method [inline-methods] */
    public a0 i() {
        return new a0();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // xp.b
    /* renamed from: D, reason: merged with bridge method [inline-methods] */
    public a0[] j(int i10) {
        return new a0[i10];
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public final Object L() {
        Object f10;
        Object[] objArr = this.f49692w;
        kotlin.jvm.internal.t.d(objArr);
        f10 = z.f(objArr, (this.f49693x + O()) - 1);
        return f10;
    }

    public final vm.d[] W(long j10) {
        long j11;
        long j12;
        Object f10;
        Object f11;
        long j13;
        xp.d[] f12;
        if (j10 > this.f49694y) {
            return xp.c.f50958a;
        }
        long K = K();
        long j14 = this.f49695z + K;
        if (this.f49690u == 0 && this.A > 0) {
            j14++;
        }
        if (xp.b.e(this) != 0 && (f12 = xp.b.f(this)) != null) {
            for (xp.d dVar : f12) {
                if (dVar != null) {
                    long j15 = ((a0) dVar).f49558a;
                    if (j15 >= 0 && j15 < j14) {
                        j14 = j15;
                    }
                }
            }
        }
        if (j14 <= this.f49694y) {
            return xp.c.f50958a;
        }
        long J = J();
        int min = l() > 0 ? Math.min(this.A, this.f49690u - ((int) (J - j14))) : this.A;
        vm.d[] dVarArr = xp.c.f50958a;
        long j16 = this.A + J;
        if (min > 0) {
            dVarArr = new vm.d[min];
            Object[] objArr = this.f49692w;
            kotlin.jvm.internal.t.d(objArr);
            long j17 = J;
            int i10 = 0;
            while (true) {
                if (J >= j16) {
                    j11 = j14;
                    j12 = j16;
                    break;
                }
                f11 = z.f(objArr, J);
                j11 = j14;
                zp.h0 h0Var = z.f49708a;
                if (f11 != h0Var) {
                    kotlin.jvm.internal.t.e(f11, "null cannot be cast to non-null type kotlinx.coroutines.flow.SharedFlowImpl.Emitter");
                    a aVar = (a) f11;
                    int i11 = i10 + 1;
                    j12 = j16;
                    dVarArr[i10] = aVar.f49699s;
                    z.g(objArr, J, h0Var);
                    z.g(objArr, j17, aVar.f49698r);
                    j13 = 1;
                    j17++;
                    if (i11 >= min) {
                        break;
                    }
                    i10 = i11;
                } else {
                    j12 = j16;
                    j13 = 1;
                }
                J += j13;
                j14 = j11;
                j16 = j12;
            }
            J = j17;
        } else {
            j11 = j14;
            j12 = j16;
        }
        int i12 = (int) (J - K);
        long j18 = l() == 0 ? J : j11;
        long max = Math.max(this.f49693x, J - Math.min(this.f49689t, i12));
        if (this.f49690u == 0 && max < j12) {
            Object[] objArr2 = this.f49692w;
            kotlin.jvm.internal.t.d(objArr2);
            f10 = z.f(objArr2, max);
            if (kotlin.jvm.internal.t.b(f10, z.f49708a)) {
                J++;
                max++;
            }
        }
        V(max, j18, J, j12);
        z();
        return (dVarArr.length == 0) ^ true ? I(dVarArr) : dVarArr;
    }

    public final long X() {
        long j10 = this.f49693x;
        if (j10 < this.f49694y) {
            this.f49694y = j10;
        }
        return j10;
    }

    @Override // xp.m
    public e b(vm.g gVar, int i10, vp.a aVar) {
        return z.e(this, gVar, i10, aVar);
    }

    @Override // wp.s
    public void c() {
        synchronized (this) {
            V(J(), this.f49694y, J(), N());
            n0 n0Var = n0.f34904a;
        }
    }

    @Override // wp.x, wp.e
    public Object collect(f fVar, vm.d dVar) {
        return A(this, fVar, dVar);
    }

    @Override // wp.s
    public boolean d(Object obj) {
        int i10;
        boolean z10;
        vm.d[] dVarArr = xp.c.f50958a;
        synchronized (this) {
            if (R(obj)) {
                dVarArr = I(dVarArr);
                z10 = true;
            } else {
                z10 = false;
            }
        }
        for (vm.d dVar : dVarArr) {
            if (dVar != null) {
                x.a aVar = rm.x.f34910p;
                dVar.resumeWith(rm.x.a(n0.f34904a));
            }
        }
        return z10;
    }

    @Override // wp.s, wp.f
    public Object emit(Object obj, vm.d dVar) {
        return F(this, obj, dVar);
    }
}
