package androidx.leanback.widget;

import N1.C0224z;

/* JADX INFO: loaded from: classes.dex */
public final class D0 extends r {

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final W2.j f8363j = new W2.j(0);

    public D0() {
        n(1);
    }

    @Override // androidx.leanback.widget.r
    public final boolean b(int i, boolean z8) {
        int iMin;
        int iT;
        if (this.f8702b.r() == 0 || (!z8 && c(i))) {
            return false;
        }
        int i5 = this.f8707g;
        if (i5 >= 0) {
            iMin = i5 + 1;
        } else {
            int i8 = this.i;
            iMin = i8 != -1 ? Math.min(i8, this.f8702b.r() - 1) : 0;
        }
        int i9 = iMin;
        boolean z9 = false;
        while (i9 < this.f8702b.r()) {
            P.g gVar = this.f8702b;
            Object[] objArr = this.f8701a;
            int iQ = gVar.q(i9, true, objArr, false);
            if (this.f8706f < 0 || this.f8707g < 0) {
                iT = this.f8703c ? Integer.MAX_VALUE : Integer.MIN_VALUE;
                this.f8706f = i9;
                this.f8707g = i9;
            } else {
                if (this.f8703c) {
                    int i10 = i9 - 1;
                    iT = (this.f8702b.s(i10) - this.f8702b.t(i10)) - this.f8704d;
                } else {
                    int i11 = i9 - 1;
                    iT = this.f8704d + this.f8702b.t(i11) + this.f8702b.s(i11);
                }
                this.f8707g = i9;
            }
            this.f8702b.p(objArr[0], i9, iQ, 0, iT);
            if (z8 || c(i)) {
                return true;
            }
            i9++;
            z9 = true;
        }
        return z9;
    }

    @Override // androidx.leanback.widget.r
    public final void e(int i, int i5, C0224z c0224z) {
        int iO;
        int iS;
        if (!this.f8703c ? i5 < 0 : i5 > 0) {
            if (this.f8707g == this.f8702b.r() - 1) {
                return;
            }
            int i8 = this.f8707g;
            if (i8 >= 0) {
                iO = i8 + 1;
            } else {
                int i9 = this.i;
                iO = i9 != -1 ? Math.min(i9, this.f8702b.r() - 1) : 0;
            }
            int iT = this.f8702b.t(this.f8707g) + this.f8704d;
            int iS2 = this.f8702b.s(this.f8707g);
            if (this.f8703c) {
                iT = -iT;
            }
            iS = iT + iS2;
        } else {
            if (this.f8706f == 0) {
                return;
            }
            iO = o();
            iS = this.f8702b.s(this.f8706f) + (this.f8703c ? this.f8704d : -this.f8704d);
        }
        c0224z.b(iO, Math.abs(iS - i));
    }

    @Override // androidx.leanback.widget.r
    public final int f(int i, boolean z8, int[] iArr) {
        if (iArr != null) {
            iArr[0] = 0;
            iArr[1] = i;
        }
        if (this.f8703c) {
            return this.f8702b.s(i);
        }
        return this.f8702b.t(i) + this.f8702b.s(i);
    }

    @Override // androidx.leanback.widget.r
    public final int h(int i, boolean z8, int[] iArr) {
        if (iArr != null) {
            iArr[0] = 0;
            iArr[1] = i;
        }
        return this.f8703c ? this.f8702b.s(i) - this.f8702b.t(i) : this.f8702b.s(i);
    }

    @Override // androidx.leanback.widget.r
    public final C0224z[] j(int i, int i5) {
        C0224z c0224z = this.f8708h[0];
        c0224z.f4727c = c0224z.f4726b;
        c0224z.a(i);
        this.f8708h[0].a(i5);
        return this.f8708h;
    }

    @Override // androidx.leanback.widget.r
    public final W2.j k(int i) {
        return this.f8363j;
    }

    @Override // androidx.leanback.widget.r
    public final boolean m(int i, boolean z8) {
        int iS;
        if (this.f8702b.r() == 0 || (!z8 && d(i))) {
            return false;
        }
        int i5 = ((GridLayoutManager) this.f8702b.f5246r).f8391M;
        boolean z9 = false;
        for (int iO = o(); iO >= i5; iO--) {
            P.g gVar = this.f8702b;
            Object[] objArr = this.f8701a;
            int iQ = gVar.q(iO, false, objArr, false);
            if (this.f8706f < 0 || this.f8707g < 0) {
                iS = this.f8703c ? Integer.MIN_VALUE : Integer.MAX_VALUE;
                this.f8706f = iO;
                this.f8707g = iO;
            } else {
                iS = this.f8703c ? this.f8702b.s(iO + 1) + this.f8704d + iQ : (this.f8702b.s(iO + 1) - this.f8704d) - iQ;
                this.f8706f = iO;
            }
            this.f8702b.p(objArr[0], iO, iQ, 0, iS);
            z9 = true;
            if (z8 || d(i)) {
                break;
            }
        }
        return z9;
    }

    public final int o() {
        int i = this.f8706f;
        if (i >= 0) {
            return i - 1;
        }
        int i5 = this.i;
        return i5 != -1 ? Math.min(i5, this.f8702b.r() - 1) : this.f8702b.r() - 1;
    }
}
