package androidx.compose.foundation.lazy.layout;

import android.os.Trace;
import androidx.compose.foundation.lazy.layout.d;
import dn.l;
import g0.m0;
import g0.n0;
import g0.o0;
import g0.p0;
import g0.q;
import g0.s;
import h2.z0;
import j2.w1;
import j2.x1;
import java.util.List;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.t;
import kotlin.jvm.internal.v;
import sm.u;

/* loaded from: classes.dex */
public final class h {

    /* renamed from: a, reason: collision with root package name */
    private final q f1938a;

    /* renamed from: b, reason: collision with root package name */
    private final z0 f1939b;

    /* renamed from: c, reason: collision with root package name */
    private final p0 f1940c;

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public final class a implements d.b, n0 {

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

        /* renamed from: b, reason: collision with root package name */
        private final long f1942b;

        /* renamed from: c, reason: collision with root package name */
        private final m0 f1943c;

        /* renamed from: d, reason: collision with root package name */
        private z0.a f1944d;

        /* renamed from: e, reason: collision with root package name */
        private boolean f1945e;

        /* renamed from: f, reason: collision with root package name */
        private boolean f1946f;

        /* renamed from: g, reason: collision with root package name */
        private boolean f1947g;

        /* renamed from: h, reason: collision with root package name */
        private C0051a f1948h;

        /* renamed from: i, reason: collision with root package name */
        private boolean f1949i;

        /* JADX INFO: Access modifiers changed from: private */
        /* renamed from: androidx.compose.foundation.lazy.layout.h$a$a, reason: collision with other inner class name */
        /* loaded from: classes.dex */
        public final class C0051a {

            /* renamed from: a, reason: collision with root package name */
            private final List f1951a;

            /* renamed from: b, reason: collision with root package name */
            private final List[] f1952b;

            /* renamed from: c, reason: collision with root package name */
            private int f1953c;

            /* renamed from: d, reason: collision with root package name */
            private int f1954d;

            public C0051a(List list) {
                this.f1951a = list;
                this.f1952b = new List[list.size()];
                if (!(!list.isEmpty())) {
                    throw new IllegalArgumentException("NestedPrefetchController shouldn't be created with no states".toString());
                }
            }

            public final boolean a(o0 o0Var) {
                if (this.f1953c >= this.f1951a.size()) {
                    return false;
                }
                if (!(!a.this.f1946f)) {
                    throw new IllegalStateException("Should not execute nested prefetch on canceled request".toString());
                }
                Trace.beginSection("compose:lazy:prefetch:nested");
                while (this.f1953c < this.f1951a.size()) {
                    try {
                        if (this.f1952b[this.f1953c] == null) {
                            if (o0Var.a() <= 0) {
                                return true;
                            }
                            List[] listArr = this.f1952b;
                            int i10 = this.f1953c;
                            listArr[i10] = ((d) this.f1951a.get(i10)).b();
                        }
                        List list = this.f1952b[this.f1953c];
                        t.d(list);
                        while (this.f1954d < list.size()) {
                            if (((n0) list.get(this.f1954d)).a(o0Var)) {
                                return true;
                            }
                            this.f1954d++;
                        }
                        this.f1954d = 0;
                        this.f1953c++;
                    } finally {
                        Trace.endSection();
                    }
                }
                rm.n0 n0Var = rm.n0.f34904a;
                return false;
            }
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        /* loaded from: classes.dex */
        public static final class b extends v implements l {

            /* renamed from: p, reason: collision with root package name */
            final /* synthetic */ kotlin.jvm.internal.o0 f1956p;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            b(kotlin.jvm.internal.o0 o0Var) {
                super(1);
                this.f1956p = o0Var;
            }

            @Override // dn.l
            /* renamed from: a, reason: merged with bridge method [inline-methods] */
            public final w1 invoke(x1 x1Var) {
                t.e(x1Var, "null cannot be cast to non-null type androidx.compose.foundation.lazy.layout.TraversablePrefetchStateNode");
                d h22 = ((i) x1Var).h2();
                kotlin.jvm.internal.o0 o0Var = this.f1956p;
                List list = (List) o0Var.f26199p;
                if (list != null) {
                    list.add(h22);
                } else {
                    list = u.q(h22);
                }
                o0Var.f26199p = list;
                return w1.SkipSubtreeAndContinueTraversal;
            }
        }

        public /* synthetic */ a(h hVar, int i10, long j10, m0 m0Var, k kVar) {
            this(i10, j10, m0Var);
        }

        private final boolean d() {
            return this.f1944d != null;
        }

        private final boolean e() {
            if (this.f1946f) {
                return false;
            }
            int a10 = ((s) h.this.f1938a.d().invoke()).a();
            int i10 = this.f1941a;
            return i10 >= 0 && i10 < a10;
        }

        private final void f() {
            if (!e()) {
                throw new IllegalArgumentException("Callers should check whether the request is still valid before calling performComposition()".toString());
            }
            if (!(this.f1944d == null)) {
                throw new IllegalArgumentException("Request was already composed!".toString());
            }
            s sVar = (s) h.this.f1938a.d().invoke();
            Object b10 = sVar.b(this.f1941a);
            this.f1944d = h.this.f1939b.i(b10, h.this.f1938a.b(this.f1941a, b10, sVar.d(this.f1941a)));
        }

        private final void g(long j10) {
            if (!(!this.f1946f)) {
                throw new IllegalArgumentException("Callers should check whether the request is still valid before calling performMeasure()".toString());
            }
            if (!(!this.f1945e)) {
                throw new IllegalArgumentException("Request was already measured!".toString());
            }
            this.f1945e = true;
            z0.a aVar = this.f1944d;
            if (aVar == null) {
                throw new IllegalArgumentException("performComposition() must be called before performMeasure()".toString());
            }
            int c10 = aVar.c();
            for (int i10 = 0; i10 < c10; i10++) {
                aVar.d(i10, j10);
            }
        }

        private final C0051a h() {
            z0.a aVar = this.f1944d;
            if (aVar == null) {
                throw new IllegalArgumentException("Should precompose before resolving nested prefetch states".toString());
            }
            kotlin.jvm.internal.o0 o0Var = new kotlin.jvm.internal.o0();
            aVar.b("androidx.compose.foundation.lazy.layout.TraversablePrefetchStateNode", new b(o0Var));
            List list = (List) o0Var.f26199p;
            if (list != null) {
                return new C0051a(list);
            }
            return null;
        }

        private final boolean i(o0 o0Var, long j10) {
            long a10 = o0Var.a();
            return (this.f1949i && a10 > 0) || j10 < a10;
        }

        @Override // g0.n0
        public boolean a(o0 o0Var) {
            if (!e()) {
                return false;
            }
            Object d10 = ((s) h.this.f1938a.d().invoke()).d(this.f1941a);
            if (!d()) {
                if (!i(o0Var, (d10 == null || !this.f1943c.f().a(d10)) ? this.f1943c.e() : this.f1943c.f().c(d10))) {
                    return true;
                }
                m0 m0Var = this.f1943c;
                long nanoTime = System.nanoTime();
                Trace.beginSection("compose:lazy:prefetch:compose");
                try {
                    f();
                    rm.n0 n0Var = rm.n0.f34904a;
                    Trace.endSection();
                    long nanoTime2 = System.nanoTime() - nanoTime;
                    if (d10 != null) {
                        m0Var.f().p(d10, m0.a(m0Var, nanoTime2, m0Var.f().e(d10, 0L)));
                    }
                    m0.b(m0Var, m0.a(m0Var, nanoTime2, m0Var.e()));
                } finally {
                }
            }
            if (!this.f1949i) {
                if (!this.f1947g) {
                    if (o0Var.a() <= 0) {
                        return true;
                    }
                    Trace.beginSection("compose:lazy:prefetch:resolve-nested");
                    try {
                        this.f1948h = h();
                        this.f1947g = true;
                        rm.n0 n0Var2 = rm.n0.f34904a;
                    } finally {
                    }
                }
                C0051a c0051a = this.f1948h;
                if (c0051a != null ? c0051a.a(o0Var) : false) {
                    return true;
                }
            }
            if (!this.f1945e && !c3.b.p(this.f1942b)) {
                if (!i(o0Var, (d10 == null || !this.f1943c.h().a(d10)) ? this.f1943c.g() : this.f1943c.h().c(d10))) {
                    return true;
                }
                m0 m0Var2 = this.f1943c;
                long nanoTime3 = System.nanoTime();
                Trace.beginSection("compose:lazy:prefetch:measure");
                try {
                    g(this.f1942b);
                    rm.n0 n0Var3 = rm.n0.f34904a;
                    Trace.endSection();
                    long nanoTime4 = System.nanoTime() - nanoTime3;
                    if (d10 != null) {
                        m0Var2.h().p(d10, m0.a(m0Var2, nanoTime4, m0Var2.h().e(d10, 0L)));
                    }
                    m0.c(m0Var2, m0.a(m0Var2, nanoTime4, m0Var2.g()));
                } finally {
                }
            }
            return false;
        }

        @Override // androidx.compose.foundation.lazy.layout.d.b
        public void b() {
            this.f1949i = true;
        }

        @Override // androidx.compose.foundation.lazy.layout.d.b
        public void cancel() {
            if (this.f1946f) {
                return;
            }
            this.f1946f = true;
            z0.a aVar = this.f1944d;
            if (aVar != null) {
                aVar.a();
            }
            this.f1944d = null;
        }

        public String toString() {
            return "HandleAndRequestImpl { index = " + this.f1941a + ", constraints = " + ((Object) c3.b.q(this.f1942b)) + ", isComposed = " + d() + ", isMeasured = " + this.f1945e + ", isCanceled = " + this.f1946f + " }";
        }

        private a(int i10, long j10, m0 m0Var) {
            this.f1941a = i10;
            this.f1942b = j10;
            this.f1943c = m0Var;
        }
    }

    public h(q qVar, z0 z0Var, p0 p0Var) {
        this.f1938a = qVar;
        this.f1939b = z0Var;
        this.f1940c = p0Var;
    }

    public final n0 c(int i10, long j10, m0 m0Var) {
        return new a(this, i10, j10, m0Var, null);
    }

    public final d.b d(int i10, long j10, m0 m0Var) {
        a aVar = new a(this, i10, j10, m0Var, null);
        this.f1940c.a(aVar);
        return aVar;
    }
}
