package F0;

import L0.C0169y;
import L0.c0;
import d3.R0;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import u0.C1579o;
import x0.AbstractC1709a;
import x0.v;

/* JADX INFO: loaded from: classes.dex */
public final class m implements c0 {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final int f2353q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final q f2354r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public int f2355s = -1;

    public m(q qVar, int i) {
        this.f2354r = qVar;
        this.f2353q = i;
    }

    /* JADX WARN: Removed duplicated region for block: B:11:0x002f  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a() {
        AbstractC1709a.d(this.f2355s == -1);
        q qVar = this.f2354r;
        qVar.w();
        qVar.f2390a0.getClass();
        int[] iArr = qVar.f2390a0;
        int i = this.f2353q;
        int i5 = iArr[i];
        if (i5 == -1) {
            i5 = qVar.f2389Z.contains(qVar.f2388Y.a(i)) ? -3 : -2;
        } else {
            boolean[] zArr = qVar.f2393d0;
            if (!zArr[i5]) {
                zArr[i5] = true;
            }
        }
        this.f2355s = i5;
    }

    public final boolean b() {
        int i = this.f2355s;
        return (i == -1 || i == -3 || i == -2) ? false : true;
    }

    @Override // L0.c0
    public final boolean f() {
        if (this.f2355s == -3) {
            return true;
        }
        if (!b()) {
            return false;
        }
        int i = this.f2355s;
        q qVar = this.f2354r;
        return !qVar.D() && qVar.f2376L[i].u(qVar.f2399j0);
    }

    @Override // L0.c0
    public final int i(R0 r02, A0.h hVar, int i) {
        C1579o c1579o;
        if (this.f2355s == -3) {
            hVar.a(4);
            return -4;
        }
        if (b()) {
            int i5 = this.f2355s;
            q qVar = this.f2354r;
            if (!qVar.D()) {
                ArrayList arrayList = qVar.D;
                int i8 = 0;
                if (!arrayList.isEmpty()) {
                    int i9 = 0;
                    loop0: while (i9 < arrayList.size() - 1) {
                        int i10 = ((j) arrayList.get(i9)).f2305k;
                        int length = qVar.f2376L.length;
                        for (int i11 = 0; i11 < length; i11++) {
                            if (qVar.f2393d0[i11] && qVar.f2376L[i11].x() == i10) {
                                break loop0;
                            }
                        }
                        i9++;
                    }
                    int i12 = v.f18553a;
                    if (i9 > arrayList.size() || i9 < 0) {
                        throw new IllegalArgumentException();
                    }
                    if (i9 != 0) {
                        arrayList.subList(0, i9).clear();
                    }
                    j jVar = (j) arrayList.get(0);
                    C1579o c1579o2 = jVar.f4143d;
                    if (!c1579o2.equals(qVar.f2387W)) {
                        E0.e eVar = qVar.f2367A;
                        eVar.a(new C0.b(eVar, 7, new C0169y(1, qVar.f2405r, c1579o2, jVar.f4144e, jVar.f4145f, v.W(jVar.f4146g), -9223372036854775807L)));
                    }
                    qVar.f2387W = c1579o2;
                }
                if (arrayList.isEmpty() || ((j) arrayList.get(0)).f2304K) {
                    int iY = qVar.f2376L[i5].y(r02, hVar, i, qVar.f2399j0);
                    if (iY == -5) {
                        C1579o c1579oD = (C1579o) r02.f11693s;
                        c1579oD.getClass();
                        if (i5 == qVar.f2382R) {
                            int iH = W3.b.h(qVar.f2376L[i5].x());
                            while (i8 < arrayList.size() && ((j) arrayList.get(i8)).f2305k != iH) {
                                i8++;
                            }
                            if (i8 < arrayList.size()) {
                                c1579o = ((j) arrayList.get(i8)).f4143d;
                            } else {
                                c1579o = qVar.f2386V;
                                c1579o.getClass();
                            }
                            c1579oD = c1579oD.d(c1579o);
                        }
                        r02.f11693s = c1579oD;
                    }
                    return iY;
                }
            }
        }
        return -3;
    }

    @Override // L0.c0
    public final void l() throws IOException {
        int i = this.f2355s;
        q qVar = this.f2354r;
        if (i == -2) {
            qVar.w();
            throw new t(I1.a.p("Unable to bind a sample queue to TrackGroup with MIME type ", qVar.f2388Y.a(this.f2353q).f17203d[0].f17358n, "."));
        }
        if (i == -1) {
            qVar.F();
            return;
        }
        if (i != -3) {
            qVar.F();
            p pVar = qVar.f2376L[i];
            K4.c cVar = pVar.f3776h;
            if (cVar == null || cVar.A() != 1) {
                return;
            }
            E0.c cVarX = pVar.f3776h.x();
            cVarX.getClass();
            throw cVarX;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:18:0x0047  */
    @Override // L0.c0
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final int m(long j8) {
        Object next;
        Object obj;
        if (!b()) {
            return 0;
        }
        int i = this.f2355s;
        q qVar = this.f2354r;
        if (qVar.D()) {
            return 0;
        }
        p pVar = qVar.f2376L[i];
        int iS = pVar.s(j8, qVar.f2399j0);
        ArrayList arrayList = qVar.D;
        if (arrayList != null) {
            obj = arrayList.isEmpty() ? null : arrayList.get(arrayList.size() - 1);
        } else {
            Iterator it = arrayList.iterator();
            if (it.hasNext()) {
                do {
                    next = it.next();
                } while (it.hasNext());
                obj = next;
            }
        }
        j jVar = (j) obj;
        if (jVar != null && !jVar.f2304K) {
            iS = Math.min(iS, jVar.e(i) - pVar.q());
        }
        pVar.D(iS);
        return iS;
    }
}
