package B0;

import android.util.Pair;
import java.util.ArrayList;
import u0.C1566b;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class Y {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final C0.f f693c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final x0.s f694d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final A1.G f695e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f696f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public int f697g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public boolean f698h;
    public V i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public V f699j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public V f700k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public V f701l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public V f702m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public int f703n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public Object f704o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public long f705p;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final u0.P f691a = new u0.P();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final u0.Q f692b = new u0.Q();

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public ArrayList f706q = new ArrayList();

    public Y(C0.f fVar, x0.s sVar, A1.G g8, C0033u c0033u) {
        this.f693c = fVar;
        this.f694d = sVar;
        this.f695e = g8;
    }

    public static L0.C o(u0.S s8, Object obj, long j8, long j9, u0.Q q8, u0.P p3) {
        s8.g(obj, p3);
        s8.n(p3.f17177c, q8);
        s8.b(obj);
        int i = p3.f17181g.f17263a;
        if (i != 0) {
            if (i == 1) {
                p3.f(0);
            }
            p3.f17181g.getClass();
            p3.g(0);
        }
        s8.g(obj, p3);
        int iC = p3.c(j8);
        return iC == -1 ? new L0.C(p3.b(j8), j9, obj) : new L0.C(obj, iC, p3.e(iC), j9, -1);
    }

    public final V a() {
        V v7 = this.i;
        if (v7 == null) {
            return null;
        }
        if (v7 == this.f699j) {
            this.f699j = v7.f674m;
        }
        if (v7 == this.f700k) {
            this.f700k = v7.f674m;
        }
        v7.i();
        int i = this.f703n - 1;
        this.f703n = i;
        if (i == 0) {
            this.f701l = null;
            V v8 = this.i;
            this.f704o = v8.f664b;
            this.f705p = v8.f669g.f678a.f3628d;
        }
        this.i = this.i.f674m;
        l();
        return this.i;
    }

    public final void b() {
        if (this.f703n == 0) {
            return;
        }
        V v7 = this.i;
        AbstractC1709a.k(v7);
        this.f704o = v7.f664b;
        this.f705p = v7.f669g.f678a.f3628d;
        while (v7 != null) {
            v7.i();
            v7 = v7.f674m;
        }
        this.i = null;
        this.f701l = null;
        this.f699j = null;
        this.f700k = null;
        this.f703n = 0;
        l();
    }

    public final W c(u0.S s8, V v7, long j8) {
        u0.S s9;
        long j9;
        u0.P p3;
        Object obj;
        long j10;
        long j11;
        long j12;
        long jQ;
        W w4 = v7.f669g;
        long j13 = (v7.f677p + w4.f682e) - j8;
        if (w4.f685h) {
            W w6 = v7.f669g;
            L0.C c3 = w6.f678a;
            int iD = s8.d(s8.b(c3.f3625a), this.f691a, this.f692b, this.f697g, this.f698h);
            if (iD != -1) {
                u0.P p4 = this.f691a;
                int i = s8.f(iD, p4, true).f17177c;
                Object obj2 = p4.f17176b;
                obj2.getClass();
                long j14 = c3.f3628d;
                if (s8.m(i, this.f692b, 0L).f17196n == iD) {
                    Pair pairJ = s8.j(this.f692b, this.f691a, i, -9223372036854775807L, Math.max(0L, j13));
                    if (pairJ != null) {
                        Object obj3 = pairJ.first;
                        long jLongValue = ((Long) pairJ.second).longValue();
                        V v8 = v7.f674m;
                        if (v8 == null || !v8.f664b.equals(obj3)) {
                            jQ = q(obj3);
                            if (jQ == -1) {
                                jQ = this.f696f;
                                this.f696f = 1 + jQ;
                            }
                        } else {
                            jQ = v8.f669g.f678a.f3628d;
                        }
                        obj = obj3;
                        j10 = jLongValue;
                        j12 = jQ;
                        j11 = -9223372036854775807L;
                    }
                } else {
                    obj = obj2;
                    j10 = 0;
                    j11 = 0;
                    j12 = j14;
                }
                L0.C cO = o(s8, obj, j10, j12, this.f692b, this.f691a);
                if (j11 != -9223372036854775807L && w6.f680c != -9223372036854775807L) {
                    int i5 = s8.g(c3.f3625a, p4).f17181g.f17263a;
                    p4.f17181g.getClass();
                    if (i5 > 0) {
                        p4.g(0);
                    }
                }
                return d(s8, cO, j11, j10);
            }
            return null;
        }
        L0.C c8 = w4.f678a;
        Object obj4 = c8.f3625a;
        u0.P p8 = this.f691a;
        s8.g(obj4, p8);
        boolean zB = c8.b();
        Object obj5 = c8.f3625a;
        boolean z8 = w4.f684g;
        if (!zB) {
            int i8 = c8.f3629e;
            if (i8 != -1) {
                p8.f(i8);
            }
            int iE = p8.e(i8);
            p8.g(i8);
            if (iE != p8.f17181g.a(i8).f17253a) {
                return e(s8, c8.f3625a, c8.f3629e, iE, w4.f682e, c8.f3628d, z8);
            }
            s8.g(obj5, p8);
            p8.d(i8);
            p8.f17181g.a(i8).getClass();
            return f(s8, c8.f3625a, 0L, w4.f682e, c8.f3628d, false);
        }
        C1566b c1566b = p8.f17181g;
        int i9 = c8.f3626b;
        int i10 = c1566b.a(i9).f17253a;
        if (i10 == -1) {
            return null;
        }
        int iA = p8.f17181g.a(i9).a(c8.f3627c);
        if (iA < i10) {
            return e(s8, c8.f3625a, i9, iA, w4.f680c, c8.f3628d, z8);
        }
        long jLongValue2 = w4.f680c;
        if (jLongValue2 == -9223372036854775807L) {
            int i11 = p8.f17177c;
            long jMax = Math.max(0L, j13);
            j9 = 0;
            p3 = p8;
            s9 = s8;
            Pair pairJ2 = s9.j(this.f692b, p3, i11, -9223372036854775807L, jMax);
            if (pairJ2 == null) {
                return null;
            }
            jLongValue2 = ((Long) pairJ2.second).longValue();
        } else {
            s9 = s8;
            j9 = 0;
            p3 = p8;
        }
        s9.g(obj5, p3);
        int i12 = c8.f3626b;
        p3.d(i12);
        p3.f17181g.a(i12).getClass();
        return f(s9, c8.f3625a, Math.max(j9, jLongValue2), w4.f680c, c8.f3628d, z8);
    }

    public final W d(u0.S s8, L0.C c3, long j8, long j9) {
        s8.g(c3.f3625a, this.f691a);
        if (c3.b()) {
            return e(s8, c3.f3625a, c3.f3626b, c3.f3627c, j8, c3.f3628d, false);
        }
        return f(s8, c3.f3625a, j9, j8, c3.f3628d, false);
    }

    public final W e(u0.S s8, Object obj, int i, int i5, long j8, long j9, boolean z8) {
        L0.C c3 = new L0.C(obj, i, i5, j9, -1);
        u0.P p3 = this.f691a;
        long jA = s8.g(obj, p3).a(i, i5);
        if (i5 == p3.e(i)) {
            p3.f17181g.getClass();
        }
        p3.g(i);
        long jMax = 0;
        if (jA != -9223372036854775807L && 0 >= jA) {
            jMax = Math.max(0L, jA - 1);
        }
        return new W(c3, jMax, j8, -9223372036854775807L, jA, z8, false, false, false, false);
    }

    public final W f(u0.S s8, Object obj, long j8, long j9, long j10, boolean z8) {
        long j11;
        u0.P p3 = this.f691a;
        s8.g(obj, p3);
        int iB = p3.b(j8);
        if (iB != -1) {
            p3.f(iB);
        }
        boolean z9 = false;
        if (iB != -1) {
            p3.g(iB);
        } else if (p3.f17181g.f17263a > 0) {
            p3.g(0);
        }
        L0.C c3 = new L0.C(iB, j10, obj);
        if (!c3.b() && iB == -1) {
            z9 = true;
        }
        boolean zJ = j(s8, c3);
        boolean zI = i(s8, c3, z9);
        if (iB != -1) {
            p3.g(iB);
        }
        if (iB != -1) {
            p3.d(iB);
            j11 = 0;
        } else {
            j11 = -9223372036854775807L;
        }
        long j12 = (j11 == -9223372036854775807L || j11 == Long.MIN_VALUE) ? p3.f17178d : j11;
        return new W(c3, (j12 == -9223372036854775807L || j8 < j12) ? j8 : Math.max(0L, j12 - ((long) 1)), j9, j11, j12, z8, false, z9, zJ, zI);
    }

    public final V g() {
        return this.f700k;
    }

    public final W h(u0.S s8, W w4) {
        long j8;
        L0.C c3 = w4.f678a;
        boolean zB = c3.b();
        int i = c3.f3629e;
        boolean z8 = !zB && i == -1;
        boolean zJ = j(s8, c3);
        boolean zI = i(s8, c3, z8);
        Object obj = c3.f3625a;
        u0.P p3 = this.f691a;
        s8.g(obj, p3);
        if (c3.b() || i == -1) {
            j8 = -9223372036854775807L;
        } else {
            p3.d(i);
            j8 = 0;
        }
        boolean zB2 = c3.b();
        int i5 = c3.f3626b;
        long jA = zB2 ? p3.a(i5, c3.f3627c) : (j8 == -9223372036854775807L || j8 == Long.MIN_VALUE) ? p3.f17178d : j8;
        if (c3.b()) {
            p3.g(i5);
        } else if (i != -1) {
            p3.g(i);
        }
        return new W(c3, w4.f679b, w4.f680c, j8, jA, w4.f683f, false, z8, zJ, zI);
    }

    public final boolean i(u0.S s8, L0.C c3, boolean z8) {
        int iB = s8.b(c3.f3625a);
        if (!s8.m(s8.f(iB, this.f691a, false).f17177c, this.f692b, 0L).i) {
            if (s8.d(iB, this.f691a, this.f692b, this.f697g, this.f698h) == -1 && z8) {
                return true;
            }
        }
        return false;
    }

    public final boolean j(u0.S s8, L0.C c3) {
        if (!c3.b() && c3.f3629e == -1) {
            Object obj = c3.f3625a;
            if (s8.m(s8.g(obj, this.f691a).f17177c, this.f692b, 0L).f17197o == s8.b(obj)) {
                return true;
            }
        }
        return false;
    }

    public final void k() {
        V v7 = this.f702m;
        if (v7 == null || v7.h()) {
            this.f702m = null;
            for (int i = 0; i < this.f706q.size(); i++) {
                V v8 = (V) this.f706q.get(i);
                if (!v8.h()) {
                    this.f702m = v8;
                    return;
                }
            }
        }
    }

    public final void l() {
        C3.D dJ = C3.G.j();
        for (V v7 = this.i; v7 != null; v7 = v7.f674m) {
            dJ.a(v7.f669g.f678a);
        }
        V v8 = this.f699j;
        this.f694d.c(new X(this, dJ, v8 == null ? null : v8.f669g.f678a, 0));
    }

    /* JADX WARN: Type inference failed for: r1v4, types: [L0.e0, java.lang.Object] */
    public final void m(long j8) {
        V v7 = this.f701l;
        if (v7 != null) {
            AbstractC1709a.j(v7.f674m == null);
            if (v7.f667e) {
                v7.f663a.u(j8 - v7.f677p);
            }
        }
    }

    public final int n(V v7) {
        AbstractC1709a.k(v7);
        int i = 0;
        if (v7.equals(this.f701l)) {
            return 0;
        }
        this.f701l = v7;
        while (true) {
            v7 = v7.f674m;
            if (v7 == null) {
                break;
            }
            if (v7 == this.f699j) {
                V v8 = this.i;
                this.f699j = v8;
                this.f700k = v8;
                i = 3;
            }
            if (v7 == this.f700k) {
                this.f700k = this.f699j;
                i |= 2;
            }
            v7.i();
            this.f703n--;
        }
        V v9 = this.f701l;
        v9.getClass();
        if (v9.f674m != null) {
            v9.b();
            v9.f674m = null;
            v9.c();
        }
        l();
        return i;
    }

    public final L0.C p(u0.S s8, Object obj, long j8) {
        long jQ;
        int iB;
        Object obj2 = obj;
        u0.P p3 = this.f691a;
        int i = s8.g(obj2, p3).f17177c;
        Object obj3 = this.f704o;
        if (obj3 == null || (iB = s8.b(obj3)) == -1 || s8.f(iB, p3, false).f17177c != i) {
            V v7 = this.i;
            while (true) {
                if (v7 == null) {
                    V v8 = this.i;
                    while (true) {
                        if (v8 != null) {
                            int iB2 = s8.b(v8.f664b);
                            if (iB2 != -1 && s8.f(iB2, p3, false).f17177c == i) {
                                jQ = v8.f669g.f678a.f3628d;
                                break;
                            }
                            v8 = v8.f674m;
                        } else {
                            jQ = q(obj2);
                            if (jQ == -1) {
                                jQ = this.f696f;
                                this.f696f = 1 + jQ;
                                if (this.i == null) {
                                    this.f704o = obj2;
                                    this.f705p = jQ;
                                }
                            }
                        }
                    }
                } else {
                    if (v7.f664b.equals(obj2)) {
                        jQ = v7.f669g.f678a.f3628d;
                        break;
                    }
                    v7 = v7.f674m;
                }
            }
        } else {
            jQ = this.f705p;
        }
        s8.g(obj2, p3);
        int i5 = p3.f17177c;
        u0.Q q8 = this.f692b;
        s8.n(i5, q8);
        boolean z8 = false;
        for (int iB3 = s8.b(obj); iB3 >= q8.f17196n; iB3--) {
            s8.f(iB3, p3, true);
            boolean z9 = p3.f17181g.f17263a > 0;
            z8 |= z9;
            if (p3.c(p3.f17178d) != -1) {
                obj2 = p3.f17176b;
                obj2.getClass();
            }
            if (z8 && (!z9 || p3.f17178d != 0)) {
                break;
            }
        }
        return o(s8, obj2, j8, jQ, this.f692b, this.f691a);
    }

    public final long q(Object obj) {
        for (int i = 0; i < this.f706q.size(); i++) {
            V v7 = (V) this.f706q.get(i);
            if (v7.f664b.equals(obj)) {
                return v7.f669g.f678a.f3628d;
            }
        }
        return -1L;
    }

    public final int r(u0.S s8) {
        u0.S s9;
        V v7;
        V v8 = this.i;
        if (v8 == null) {
            return 0;
        }
        int iB = s8.b(v8.f664b);
        while (true) {
            s9 = s8;
            iB = s9.d(iB, this.f691a, this.f692b, this.f697g, this.f698h);
            while (true) {
                v8.getClass();
                v7 = v8.f674m;
                if (v7 == null || v8.f669g.f685h) {
                    break;
                }
                v8 = v7;
            }
            if (iB == -1 || v7 == null || s9.b(v7.f664b) != iB) {
                break;
            }
            v8 = v7;
            s8 = s9;
        }
        int iN = n(v8);
        v8.f669g = h(s9, v8.f669g);
        return iN;
    }

    /* JADX WARN: Code restructure failed: missing block: B:49:0x0090, code lost:
    
        return n(r1);
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final int s(u0.S s8, long j8, long j9, long j10) {
        W wH;
        V v7 = this.i;
        V v8 = null;
        while (true) {
            boolean z8 = false;
            if (v7 == null) {
                return 0;
            }
            W w4 = v7.f669g;
            if (v8 == null) {
                wH = h(s8, w4);
            } else {
                W wC = c(s8, v8, j8);
                if (wC == null || w4.f679b != wC.f679b || !w4.f678a.equals(wC.f678a)) {
                    break;
                }
                wH = wC;
            }
            v7.f669g = wH.a(w4.f680c);
            long j11 = w4.f682e;
            if (j11 != -9223372036854775807L) {
                long j12 = wH.f682e;
                if (j11 != j12) {
                    v7.k();
                    long j13 = j12 == -9223372036854775807L ? Long.MAX_VALUE : v7.f677p + j12;
                    int i = (v7 != this.f699j || v7.f669g.f684g || (j9 != Long.MIN_VALUE && j9 < j13)) ? 0 : 1;
                    if (v7 == this.f700k && (j10 == Long.MIN_VALUE || j10 >= j13)) {
                        z8 = true;
                    }
                    int iN = n(v7);
                    return iN != 0 ? iN : z8 ? i | 2 : i;
                }
            }
            v8 = v7;
            v7 = v7.f674m;
        }
    }
}
