package F;

import G0.B0;
import G0.y0;
import G0.z0;
import android.os.Trace;
import androidx.media3.common.PlaybackException;
import h1.C2069a;
import java.util.ArrayList;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public final class j0 implements P {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final C4.f f1301b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final J4.c f1302c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public C2069a f1303d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public z0 f1304e;

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

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public boolean f1310k;
    public i0 l;

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

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

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

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

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public boolean f1315q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final /* synthetic */ E3.j f1316r;

    public j0(E3.j jVar, int i6, C4.f fVar, J4.c cVar) {
        this.f1316r = jVar;
        this.f1300a = i6;
        this.f1301b = fVar;
        this.f1302c = cVar;
        int i7 = T4.e.f6723b;
        this.f1314p = System.nanoTime() - T4.e.f6722a;
    }

    @Override // F.P
    public final void a() {
        this.f1311m = true;
    }

    public final void b() {
        y0 y0Var = this.f1305f;
        if (y0Var != null) {
            y0Var.cancel();
        }
        this.f1305f = null;
        z0 z0Var = this.f1304e;
        if (z0Var != null) {
            z0Var.dispose();
        }
        this.f1304e = null;
        this.l = null;
    }

    public final boolean c(E3.p pVar) {
        boolean zD;
        if (!this.f1316r.f1151b) {
            return false;
        }
        if (this.f1311m) {
            Trace.beginSection("compose:lazy:prefetch:execute:urgent");
            try {
                zD = d(pVar);
            } finally {
                Trace.endSection();
            }
        } else {
            zD = d(pVar);
        }
        W.a.B0("compose:lazy:prefetch:execute:item", -1L);
        return zD;
    }

    @Override // F.P
    public final void cancel() {
        if (this.f1307h) {
            return;
        }
        this.f1307h = true;
        b();
    }

    /* JADX WARN: Removed duplicated region for block: B:101:0x01c3  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean d(E3.p pVar) {
        long j4;
        C2069a c2069a;
        boolean z6;
        List list;
        j0 j0Var = this;
        int i6 = j0Var.f1300a;
        long j6 = i6;
        W.a.B0("compose:lazy:prefetch:execute:item", j6);
        B b5 = (B) ((C0170y) j0Var.f1316r.f1152c).f1356b.invoke();
        if (!j0Var.f1307h) {
            int iA = b5.a();
            if (i6 >= 0 && i6 < iA) {
                Object objB = b5.b(i6);
                Object obj = j0Var.f1309j;
                if (obj != null && !objB.equals(obj)) {
                    j0Var.b();
                    return false;
                }
                Object objC = b5.c(i6);
                C4.f fVar = j0Var.f1301b;
                C0148b c0148b = (C0148b) fVar.f743d;
                if (fVar.f742c != objC || c0148b == null) {
                    n.N n6 = (n.N) fVar.f741b;
                    Object objD = n6.d(objC);
                    Object obj2 = objD;
                    if (objD == null) {
                        C0148b c0148b2 = new C0148b();
                        c0148b2.f1254e = -1;
                        n6.l(objC, c0148b2);
                        obj2 = c0148b2;
                    }
                    c0148b = (C0148b) obj2;
                    fVar.f742c = objC;
                    fVar.f743d = c0148b;
                }
                j0Var.e();
                long jA = pVar.a();
                j0Var.f1312n = jA;
                int i7 = T4.e.f6723b;
                j0Var.f1314p = System.nanoTime() - T4.e.f6722a;
                j0Var.f1313o = 0L;
                W.a.B0("compose:lazy:prefetch:available_time_nanos", jA);
                if (j0Var.e()) {
                    j4 = 0;
                } else {
                    j4 = 0;
                    j0Var = this;
                    if (j0Var.i(j0Var.f1312n, c0148b.f1250a + c0148b.f1251b)) {
                        Trace.beginSection("compose:lazy:prefetch:compose");
                        try {
                            j0Var.g(objB, objC, c0148b);
                        } finally {
                        }
                    }
                    if (!j0Var.e()) {
                        return true;
                    }
                }
                if (j0Var.f1305f != null) {
                    if (!j0Var.i(j0Var.f1312n, c0148b.f1252c)) {
                        return true;
                    }
                    Trace.beginSection("compose:lazy:prefetch:apply");
                    try {
                        y0 y0Var = j0Var.f1305f;
                        if (y0Var == null) {
                            throw new IllegalArgumentException("Nothing to apply!");
                        }
                        j0Var.f1304e = y0Var.apply();
                        j0Var.f1305f = null;
                        j0Var.f1308i = true;
                        Trace.endSection();
                        j0Var.j();
                        c0148b.f1252c = C0148b.a(j0Var.f1313o, c0148b.f1252c);
                    } finally {
                    }
                }
                if (!j0Var.f1310k) {
                    if (j0Var.f1312n <= j4) {
                        return true;
                    }
                    Trace.beginSection("compose:lazy:prefetch:resolve-nested");
                    try {
                        j0Var.l = j0Var.h();
                        j0Var.f1310k = true;
                    } finally {
                    }
                }
                i0 i0Var = j0Var.l;
                if (i0Var != null) {
                    int i8 = c0148b.f1254e;
                    boolean z7 = j0Var.f1311m;
                    List[] listArr = i0Var.f1294b;
                    int i9 = i0Var.f1295c;
                    List list2 = i0Var.f1293a;
                    if (i9 < list2.size()) {
                        if (i0Var.f1298f.f1307h) {
                            A.b.c("Should not execute nested prefetch on canceled request");
                        }
                        Trace.beginSection("compose:lazy:prefetch:update_nested_prefetch_count");
                        try {
                            int size = list2.size();
                            for (int i10 = 0; i10 < size; i10++) {
                                ((Q) list2.get(i10)).f1222d = i8;
                            }
                            Trace.endSection();
                            Trace.beginSection("compose:lazy:prefetch:nested");
                            while (i0Var.f1295c < list2.size()) {
                                try {
                                    if (listArr[i0Var.f1295c] == null) {
                                        if (pVar.a() <= j4) {
                                            Trace.endSection();
                                            return true;
                                        }
                                        int i11 = i0Var.f1295c;
                                        Q q6 = (Q) list2.get(i11);
                                        J4.c cVar = q6.f1219a;
                                        if (cVar == null) {
                                            list = x4.y.f26065a;
                                        } else {
                                            O o6 = new O(q6, q6.f1222d);
                                            cVar.invoke(o6);
                                            ArrayList arrayList = o6.f1217b;
                                            q6.f1224f = arrayList.size();
                                            list = arrayList;
                                        }
                                        listArr[i11] = list;
                                    }
                                    List list3 = listArr[i0Var.f1295c];
                                    kotlin.jvm.internal.o.e(list3);
                                    while (i0Var.f1296d < list3.size()) {
                                        j0 j0Var2 = (j0) list3.get(i0Var.f1296d);
                                        if (z7) {
                                            j0 j0Var3 = j0Var2 != null ? j0Var2 : null;
                                            if (j0Var3 != null) {
                                                z6 = true;
                                                j0Var3.f1311m = true;
                                            }
                                        } else {
                                            z6 = true;
                                        }
                                        i0Var.f1297e = z6;
                                        if (j0Var2.c(pVar)) {
                                            return z6;
                                        }
                                        i0Var.f1296d++;
                                    }
                                    i0Var.f1296d = 0;
                                    i0Var.f1295c++;
                                } catch (Throwable th) {
                                    throw th;
                                }
                            }
                        } finally {
                        }
                    }
                }
                i0 i0Var2 = j0Var.l;
                if (i0Var2 != null && i0Var2.f1297e) {
                    j0Var.j();
                    W.a.B0("compose:lazy:prefetch:execute:item", j6);
                    i0 i0Var3 = j0Var.l;
                    if (i0Var3 != null) {
                        i0Var3.f1297e = false;
                    }
                }
                if (!j0Var.f1306g && (c2069a = j0Var.f1303d) != null) {
                    if (!j0Var.i(j0Var.f1312n, c0148b.f1253d)) {
                        return true;
                    }
                    Trace.beginSection("compose:lazy:prefetch:measure");
                    try {
                        j0Var.f(c2069a.f16612a);
                        Trace.endSection();
                        j0Var.j();
                        c0148b.f1253d = C0148b.a(j0Var.f1313o, c0148b.f1253d);
                        J4.c cVar2 = j0Var.f1302c;
                        if (cVar2 != null) {
                            cVar2.invoke(j0Var);
                        }
                    } finally {
                    }
                }
                i0 i0Var4 = j0Var.l;
                if (j0Var.f1306g && j0Var.f1310k && i0Var4 != null) {
                    List list4 = i0Var4.f1293a;
                    int size2 = list4.size();
                    int iMin = Integer.MAX_VALUE;
                    for (int i12 = 0; i12 < size2; i12++) {
                        iMin = Math.min(iMin, ((Q) list4.get(i12)).f1223e);
                    }
                    if (iMin == Integer.MAX_VALUE) {
                        iMin = 0;
                    }
                    int i13 = c0148b.f1254e;
                    c0148b.f1254e = i13 == -1 ? iMin : ((i13 * 3) + iMin) / 4;
                    int size3 = list4.size();
                    int iMin2 = Integer.MAX_VALUE;
                    for (int i14 = 0; i14 < size3; i14++) {
                        iMin2 = Math.min(iMin2, ((Q) list4.get(i14)).f1224f);
                    }
                    if (iMin2 == Integer.MAX_VALUE) {
                        iMin2 = 0;
                    }
                    if (iMin2 < iMin) {
                        c0148b.f1253d = j4;
                    }
                }
                return false;
            }
        }
        j0Var.b();
        return false;
    }

    public final boolean e() {
        y0 y0Var;
        return this.f1308i || ((y0Var = this.f1305f) != null && y0Var.isComplete());
    }

    public final void f(long j4) {
        if (this.f1307h) {
            A.b.a("Callers should check whether the request is still valid before calling performMeasure()");
        }
        if (this.f1306g) {
            A.b.a("Request was already measured!");
        }
        this.f1306g = true;
        z0 z0Var = this.f1304e;
        if (z0Var == null) {
            A.b.b("performComposition() must be called before performMeasure()");
            throw new E3.v();
        }
        int iA = z0Var.a();
        for (int i6 = 0; i6 < iA; i6++) {
            z0Var.c(i6, j4);
        }
    }

    public final void g(Object obj, Object obj2, C0148b c0148b) {
        y0 u6;
        y0 y0Var = this.f1305f;
        if (y0Var == null) {
            E3.j jVar = this.f1316r;
            J4.e eVarA = ((C0170y) jVar.f1152c).a(this.f1300a, obj, obj2);
            G0.W wA = ((B0) jVar.f1153d).a();
            if (wA.f1936a.F()) {
                wA.i(obj, eVarA, true);
                u6 = new G0.U(wA, obj);
            } else {
                u6 = new T.p(7, wA, obj);
            }
            y0Var = u6;
            this.f1305f = y0Var;
            this.f1309j = obj;
        }
        this.f1315q = false;
        while (!y0Var.isComplete() && !this.f1315q) {
            y0Var.h(new h0(0, this, c0148b));
        }
        j();
        if (this.f1315q) {
            c0148b.f1251b = C0148b.a(this.f1313o, c0148b.f1251b);
        } else {
            c0148b.f1250a = C0148b.a(this.f1313o, c0148b.f1250a);
        }
    }

    public final i0 h() {
        z0 z0Var = this.f1304e;
        if (z0Var == null) {
            A.b.b("Should precompose before resolving nested prefetch states");
            throw new E3.v();
        }
        kotlin.jvm.internal.C c6 = new kotlin.jvm.internal.C();
        z0Var.d(new g0(c6, 0));
        List list = (List) c6.f20115a;
        if (list != null) {
            return new i0(this, list);
        }
        return null;
    }

    public final boolean i(long j4, long j6) {
        if (this.f1311m) {
            j6 = 0;
        }
        return j4 > j6;
    }

    public final void j() {
        int i6 = T4.e.f6723b;
        long jNanoTime = System.nanoTime() - T4.e.f6722a;
        long j4 = this.f1314p;
        T4.d unit = T4.d.f6714b;
        kotlin.jvm.internal.o.h(unit, "unit");
        long jJ0 = 0;
        if (((j4 - 1) | 1) == Long.MAX_VALUE) {
            if (jNanoTime == j4) {
                T4.a aVar = T4.b.f6709b;
            } else {
                jJ0 = T4.b.i(j4 < 0 ? T4.b.f6711d : T4.b.f6710c);
            }
        } else if (((jNanoTime - 1) | 1) == Long.MAX_VALUE) {
            jJ0 = jNanoTime < 0 ? T4.b.f6711d : T4.b.f6710c;
        } else {
            long j6 = jNanoTime - j4;
            if (((~(j6 ^ j4)) & (j6 ^ jNanoTime)) < 0) {
                T4.d dVar = T4.d.f6715c;
                if (unit.compareTo(dVar) < 0) {
                    long jX = A4.g.x(1L, dVar, unit);
                    long j7 = (jNanoTime / jX) - (j4 / jX);
                    long j8 = (jNanoTime % jX) - (j4 % jX);
                    T4.a aVar2 = T4.b.f6709b;
                    jJ0 = T4.b.g(e4.C.j0(j7, dVar), e4.C.j0(j8, unit));
                } else {
                    jJ0 = T4.b.i(j6 < 0 ? T4.b.f6711d : T4.b.f6710c);
                }
            } else {
                jJ0 = e4.C.j0(j6, unit);
            }
        }
        long j9 = jJ0 >> 1;
        T4.a aVar3 = T4.b.f6709b;
        long j10 = (1 & ((int) jJ0)) == 0 ? j9 : j9 > 9223372036854L ? Long.MAX_VALUE : j9 < -9223372036854L ? Long.MIN_VALUE : j9 * ((long) PlaybackException.CUSTOM_ERROR_CODE_BASE);
        this.f1313o = j10;
        long j11 = this.f1312n - j10;
        this.f1312n = j11;
        this.f1314p = jNanoTime;
        W.a.B0("compose:lazy:prefetch:available_time_nanos", j11);
    }

    public final String toString() {
        return "HandleAndRequestImpl { index = " + this.f1300a + ", constraints = " + this.f1303d + ", isComposed = " + e() + ", isMeasured = " + this.f1306g + ", isCanceled = " + this.f1307h + " }";
    }
}
