package androidx.recyclerview.widget;

import android.content.Context;
import android.graphics.PointF;
import android.graphics.Rect;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;

/* JADX INFO: loaded from: classes.dex */
public class StaggeredGridLayoutManager extends q1 implements d2 {
    static final boolean DEBUG = false;

    @Deprecated
    public static final int GAP_HANDLING_LAZY = 1;
    public static final int GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS = 2;
    public static final int GAP_HANDLING_NONE = 0;
    public static final int HORIZONTAL = 0;
    static final int INVALID_OFFSET = Integer.MIN_VALUE;
    private static final float MAX_SCROLL_FACTOR = 0.33333334f;
    private static final String TAG = "StaggeredGridLManager";
    public static final int VERTICAL = 1;
    private int mFullSizeSpec;
    private boolean mLastLayoutFromEnd;
    private boolean mLastLayoutRTL;

    @NonNull
    private final o0 mLayoutState;
    private int mOrientation;
    private t2 mPendingSavedState;
    private int[] mPrefetchDistances;

    @NonNull
    x0 mPrimaryOrientation;
    private BitSet mRemainingSpans;

    @NonNull
    x0 mSecondaryOrientation;
    private int mSizePerSpan;
    u2[] mSpans;
    private int mSpanCount = -1;
    boolean mReverseLayout = false;
    boolean mShouldReverseLayout = false;
    int mPendingScrollPosition = -1;
    int mPendingScrollPositionOffset = Integer.MIN_VALUE;
    s2 mLazySpanLookup = new s2();
    private int mGapStrategy = 2;
    private final Rect mTmpRect = new Rect();
    private final p2 mAnchorInfo = new p2(this);
    private boolean mLaidOutInvalidFullSpan = false;
    private boolean mSmoothScrollbarEnabled = true;
    private final Runnable mCheckForGapsRunnable = new s(this, 1);

    public StaggeredGridLayoutManager(Context context, AttributeSet attributeSet, int i10, int i11) {
        p1 properties = q1.getProperties(context, attributeSet, i10, i11);
        setOrientation(properties.f3851a);
        setSpanCount(properties.f3852b);
        setReverseLayout(properties.f3853c);
        this.mLayoutState = new o0();
        this.mPrimaryOrientation = x0.b(this, this.mOrientation);
        this.mSecondaryOrientation = x0.b(this, 1 - this.mOrientation);
    }

    public static int H(int i10, int i11, int i12) {
        if (i11 == 0 && i12 == 0) {
            return i10;
        }
        int mode = View.MeasureSpec.getMode(i10);
        return (mode == Integer.MIN_VALUE || mode == 1073741824) ? View.MeasureSpec.makeMeasureSpec(Math.max(0, (View.MeasureSpec.getSize(i10) - i11) - i12), mode) : i10;
    }

    public final void A(y1 y1Var, o0 o0Var) {
        if (!o0Var.f3825a || o0Var.f3833i) {
            return;
        }
        if (o0Var.f3826b == 0) {
            if (o0Var.f3829e == -1) {
                B(o0Var.f3831g, y1Var);
                return;
            } else {
                C(o0Var.f3830f, y1Var);
                return;
            }
        }
        int i10 = 1;
        if (o0Var.f3829e == -1) {
            int i11 = o0Var.f3830f;
            int iH = this.mSpans[0].h(i11);
            while (i10 < this.mSpanCount) {
                int iH2 = this.mSpans[i10].h(i11);
                if (iH2 > iH) {
                    iH = iH2;
                }
                i10++;
            }
            int i12 = i11 - iH;
            B(i12 < 0 ? o0Var.f3831g : o0Var.f3831g - Math.min(i12, o0Var.f3826b), y1Var);
            return;
        }
        int i13 = o0Var.f3831g;
        int iF = this.mSpans[0].f(i13);
        while (i10 < this.mSpanCount) {
            int iF2 = this.mSpans[i10].f(i13);
            if (iF2 < iF) {
                iF = iF2;
            }
            i10++;
        }
        int i14 = iF - o0Var.f3831g;
        C(i14 < 0 ? o0Var.f3830f : Math.min(i14, o0Var.f3826b) + o0Var.f3830f, y1Var);
    }

    public final void B(int i10, y1 y1Var) {
        for (int childCount = getChildCount() - 1; childCount >= 0; childCount--) {
            View childAt = getChildAt(childCount);
            if (this.mPrimaryOrientation.g(childAt) < i10 || this.mPrimaryOrientation.o(childAt) < i10) {
                return;
            }
            q2 q2Var = (q2) childAt.getLayoutParams();
            q2Var.getClass();
            if (q2Var.f3866a.f3909a.size() == 1) {
                return;
            }
            u2 u2Var = q2Var.f3866a;
            ArrayList arrayList = u2Var.f3909a;
            int size = arrayList.size();
            View view = (View) arrayList.remove(size - 1);
            q2 q2Var2 = (q2) view.getLayoutParams();
            q2Var2.f3866a = null;
            if (q2Var2.isItemRemoved() || q2Var2.isItemChanged()) {
                u2Var.f3912d -= u2Var.f3914f.mPrimaryOrientation.e(view);
            }
            if (size == 1) {
                u2Var.f3910b = Integer.MIN_VALUE;
            }
            u2Var.f3911c = Integer.MIN_VALUE;
            removeAndRecycleView(childAt, y1Var);
        }
    }

    public final void C(int i10, y1 y1Var) {
        while (getChildCount() > 0) {
            View childAt = getChildAt(0);
            if (this.mPrimaryOrientation.d(childAt) > i10 || this.mPrimaryOrientation.n(childAt) > i10) {
                return;
            }
            q2 q2Var = (q2) childAt.getLayoutParams();
            q2Var.getClass();
            if (q2Var.f3866a.f3909a.size() == 1) {
                return;
            }
            u2 u2Var = q2Var.f3866a;
            ArrayList arrayList = u2Var.f3909a;
            View view = (View) arrayList.remove(0);
            q2 q2Var2 = (q2) view.getLayoutParams();
            q2Var2.f3866a = null;
            if (arrayList.size() == 0) {
                u2Var.f3911c = Integer.MIN_VALUE;
            }
            if (q2Var2.isItemRemoved() || q2Var2.isItemChanged()) {
                u2Var.f3912d -= u2Var.f3914f.mPrimaryOrientation.e(view);
            }
            u2Var.f3910b = Integer.MIN_VALUE;
            removeAndRecycleView(childAt, y1Var);
        }
    }

    public final void D() {
        if (this.mOrientation == 1 || !isLayoutRTL()) {
            this.mShouldReverseLayout = this.mReverseLayout;
        } else {
            this.mShouldReverseLayout = !this.mReverseLayout;
        }
    }

    public final void E(int i10) {
        o0 o0Var = this.mLayoutState;
        o0Var.f3829e = i10;
        o0Var.f3828d = this.mShouldReverseLayout != (i10 == -1) ? -1 : 1;
    }

    public final void F(int i10, f2 f2Var) {
        int iL;
        int iL2;
        int i11;
        o0 o0Var = this.mLayoutState;
        boolean z10 = false;
        o0Var.f3826b = 0;
        o0Var.f3827c = i10;
        if (!isSmoothScrolling() || (i11 = f2Var.f3700a) == -1) {
            iL = 0;
            iL2 = 0;
        } else {
            if (this.mShouldReverseLayout == (i11 < i10)) {
                iL = this.mPrimaryOrientation.l();
                iL2 = 0;
            } else {
                iL2 = this.mPrimaryOrientation.l();
                iL = 0;
            }
        }
        if (getClipToPadding()) {
            this.mLayoutState.f3830f = this.mPrimaryOrientation.k() - iL2;
            this.mLayoutState.f3831g = this.mPrimaryOrientation.i() + iL;
        } else {
            this.mLayoutState.f3831g = this.mPrimaryOrientation.h() + iL;
            this.mLayoutState.f3830f = -iL2;
        }
        o0 o0Var2 = this.mLayoutState;
        o0Var2.f3832h = false;
        o0Var2.f3825a = true;
        if (this.mPrimaryOrientation.j() == 0 && this.mPrimaryOrientation.h() == 0) {
            z10 = true;
        }
        o0Var2.f3833i = z10;
    }

    public final void G(u2 u2Var, int i10, int i11) {
        int i12 = u2Var.f3912d;
        int i13 = u2Var.f3913e;
        if (i10 != -1) {
            int i14 = u2Var.f3911c;
            if (i14 == Integer.MIN_VALUE) {
                u2Var.a();
                i14 = u2Var.f3911c;
            }
            if (i14 - i12 >= i11) {
                this.mRemainingSpans.set(i13, false);
                return;
            }
            return;
        }
        int i15 = u2Var.f3910b;
        if (i15 == Integer.MIN_VALUE) {
            View view = (View) u2Var.f3909a.get(0);
            q2 q2Var = (q2) view.getLayoutParams();
            u2Var.f3910b = u2Var.f3914f.mPrimaryOrientation.g(view);
            q2Var.getClass();
            i15 = u2Var.f3910b;
        }
        if (i15 + i12 <= i11) {
            this.mRemainingSpans.set(i13, false);
        }
    }

    public boolean areAllEndsEqual() {
        int iF = this.mSpans[0].f(Integer.MIN_VALUE);
        for (int i10 = 1; i10 < this.mSpanCount; i10++) {
            if (this.mSpans[i10].f(Integer.MIN_VALUE) != iF) {
                return false;
            }
        }
        return true;
    }

    public boolean areAllStartsEqual() {
        int iH = this.mSpans[0].h(Integer.MIN_VALUE);
        for (int i10 = 1; i10 < this.mSpanCount; i10++) {
            if (this.mSpans[i10].h(Integer.MIN_VALUE) != iH) {
                return false;
            }
        }
        return true;
    }

    @Override // androidx.recyclerview.widget.q1
    public void assertNotInLayoutOrScroll(String str) {
        if (this.mPendingSavedState == null) {
            super.assertNotInLayoutOrScroll(str);
        }
    }

    @Override // androidx.recyclerview.widget.q1
    public boolean canScrollHorizontally() {
        return this.mOrientation == 0;
    }

    @Override // androidx.recyclerview.widget.q1
    public boolean canScrollVertically() {
        return this.mOrientation == 1;
    }

    public boolean checkForGaps() {
        int firstChildPosition;
        int lastChildPosition;
        if (getChildCount() == 0 || this.mGapStrategy == 0 || !isAttachedToWindow()) {
            return false;
        }
        if (this.mShouldReverseLayout) {
            firstChildPosition = getLastChildPosition();
            lastChildPosition = getFirstChildPosition();
        } else {
            firstChildPosition = getFirstChildPosition();
            lastChildPosition = getLastChildPosition();
        }
        if (firstChildPosition == 0 && hasGapsToFix() != null) {
            this.mLazySpanLookup.a();
            requestSimpleAnimationsInNextLayout();
            requestLayout();
            return true;
        }
        if (!this.mLaidOutInvalidFullSpan) {
            return false;
        }
        int i10 = this.mShouldReverseLayout ? -1 : 1;
        int i11 = lastChildPosition + 1;
        r2 r2VarD = this.mLazySpanLookup.d(firstChildPosition, i11, i10);
        if (r2VarD == null) {
            this.mLaidOutInvalidFullSpan = false;
            this.mLazySpanLookup.c(i11);
            return false;
        }
        r2 r2VarD2 = this.mLazySpanLookup.d(firstChildPosition, r2VarD.f3883a, i10 * (-1));
        if (r2VarD2 == null) {
            this.mLazySpanLookup.c(r2VarD.f3883a);
        } else {
            this.mLazySpanLookup.c(r2VarD2.f3883a + 1);
        }
        requestSimpleAnimationsInNextLayout();
        requestLayout();
        return true;
    }

    @Override // androidx.recyclerview.widget.q1
    public boolean checkLayoutParams(r1 r1Var) {
        return r1Var instanceof q2;
    }

    @Override // androidx.recyclerview.widget.q1
    @RestrictTo({RestrictTo.Scope.f555a})
    public void collectAdjacentPrefetchPositions(int i10, int i11, f2 f2Var, o1 o1Var) {
        int iF;
        int iH;
        if (this.mOrientation != 0) {
            i10 = i11;
        }
        if (getChildCount() == 0 || i10 == 0) {
            return;
        }
        prepareLayoutStateForDelta(i10, f2Var);
        int[] iArr = this.mPrefetchDistances;
        if (iArr == null || iArr.length < this.mSpanCount) {
            this.mPrefetchDistances = new int[this.mSpanCount];
        }
        int i12 = 0;
        for (int i13 = 0; i13 < this.mSpanCount; i13++) {
            o0 o0Var = this.mLayoutState;
            if (o0Var.f3828d == -1) {
                iF = o0Var.f3830f;
                iH = this.mSpans[i13].h(iF);
            } else {
                iF = this.mSpans[i13].f(o0Var.f3831g);
                iH = this.mLayoutState.f3831g;
            }
            int i14 = iF - iH;
            if (i14 >= 0) {
                this.mPrefetchDistances[i12] = i14;
                i12++;
            }
        }
        Arrays.sort(this.mPrefetchDistances, 0, i12);
        for (int i15 = 0; i15 < i12; i15++) {
            int i16 = this.mLayoutState.f3827c;
            if (i16 < 0 || i16 >= f2Var.b()) {
                return;
            }
            ((x) o1Var).a(this.mLayoutState.f3827c, this.mPrefetchDistances[i15]);
            o0 o0Var2 = this.mLayoutState;
            o0Var2.f3827c += o0Var2.f3828d;
        }
    }

    @Override // androidx.recyclerview.widget.q1
    public int computeHorizontalScrollExtent(f2 f2Var) {
        return o(f2Var);
    }

    @Override // androidx.recyclerview.widget.q1
    public int computeHorizontalScrollOffset(f2 f2Var) {
        return p(f2Var);
    }

    @Override // androidx.recyclerview.widget.q1
    public int computeHorizontalScrollRange(f2 f2Var) {
        return q(f2Var);
    }

    @Override // androidx.recyclerview.widget.d2
    public PointF computeScrollVectorForPosition(int i10) {
        int iN = n(i10);
        PointF pointF = new PointF();
        if (iN == 0) {
            return null;
        }
        if (this.mOrientation == 0) {
            pointF.x = iN;
            pointF.y = 0.0f;
        } else {
            pointF.x = 0.0f;
            pointF.y = iN;
        }
        return pointF;
    }

    @Override // androidx.recyclerview.widget.q1
    public int computeVerticalScrollExtent(f2 f2Var) {
        return o(f2Var);
    }

    @Override // androidx.recyclerview.widget.q1
    public int computeVerticalScrollOffset(f2 f2Var) {
        return p(f2Var);
    }

    @Override // androidx.recyclerview.widget.q1
    public int computeVerticalScrollRange(f2 f2Var) {
        return q(f2Var);
    }

    public int[] findFirstCompletelyVisibleItemPositions(int[] iArr) {
        if (iArr == null) {
            iArr = new int[this.mSpanCount];
        } else if (iArr.length < this.mSpanCount) {
            throw new IllegalArgumentException("Provided int[]'s size must be more than or equal to span count. Expected:" + this.mSpanCount + ", array size:" + iArr.length);
        }
        for (int i10 = 0; i10 < this.mSpanCount; i10++) {
            u2 u2Var = this.mSpans[i10];
            iArr[i10] = u2Var.f3914f.mReverseLayout ? u2Var.e(true, r3.size() - 1, -1, true, false) : u2Var.e(true, 0, u2Var.f3909a.size(), true, false);
        }
        return iArr;
    }

    public View findFirstVisibleItemClosestToEnd(boolean z10) {
        int iK = this.mPrimaryOrientation.k();
        int i10 = this.mPrimaryOrientation.i();
        View view = null;
        for (int childCount = getChildCount() - 1; childCount >= 0; childCount--) {
            View childAt = getChildAt(childCount);
            int iG = this.mPrimaryOrientation.g(childAt);
            int iD = this.mPrimaryOrientation.d(childAt);
            if (iD > iK && iG < i10) {
                if (iD <= i10 || !z10) {
                    return childAt;
                }
                if (view == null) {
                    view = childAt;
                }
            }
        }
        return view;
    }

    public View findFirstVisibleItemClosestToStart(boolean z10) {
        int iK = this.mPrimaryOrientation.k();
        int i10 = this.mPrimaryOrientation.i();
        int childCount = getChildCount();
        View view = null;
        for (int i11 = 0; i11 < childCount; i11++) {
            View childAt = getChildAt(i11);
            int iG = this.mPrimaryOrientation.g(childAt);
            if (this.mPrimaryOrientation.d(childAt) > iK && iG < i10) {
                if (iG >= iK || !z10) {
                    return childAt;
                }
                if (view == null) {
                    view = childAt;
                }
            }
        }
        return view;
    }

    public int findFirstVisibleItemPositionInt() {
        View viewFindFirstVisibleItemClosestToEnd = this.mShouldReverseLayout ? findFirstVisibleItemClosestToEnd(true) : findFirstVisibleItemClosestToStart(true);
        if (viewFindFirstVisibleItemClosestToEnd == null) {
            return -1;
        }
        return getPosition(viewFindFirstVisibleItemClosestToEnd);
    }

    public int[] findFirstVisibleItemPositions(int[] iArr) {
        if (iArr == null) {
            iArr = new int[this.mSpanCount];
        } else if (iArr.length < this.mSpanCount) {
            throw new IllegalArgumentException("Provided int[]'s size must be more than or equal to span count. Expected:" + this.mSpanCount + ", array size:" + iArr.length);
        }
        for (int i10 = 0; i10 < this.mSpanCount; i10++) {
            u2 u2Var = this.mSpans[i10];
            iArr[i10] = u2Var.f3914f.mReverseLayout ? u2Var.e(false, r3.size() - 1, -1, true, false) : u2Var.e(false, 0, u2Var.f3909a.size(), true, false);
        }
        return iArr;
    }

    public int[] findLastCompletelyVisibleItemPositions(int[] iArr) {
        if (iArr == null) {
            iArr = new int[this.mSpanCount];
        } else if (iArr.length < this.mSpanCount) {
            throw new IllegalArgumentException("Provided int[]'s size must be more than or equal to span count. Expected:" + this.mSpanCount + ", array size:" + iArr.length);
        }
        for (int i10 = 0; i10 < this.mSpanCount; i10++) {
            u2 u2Var = this.mSpans[i10];
            iArr[i10] = u2Var.f3914f.mReverseLayout ? u2Var.e(true, 0, u2Var.f3909a.size(), true, false) : u2Var.e(true, r3.size() - 1, -1, true, false);
        }
        return iArr;
    }

    public int[] findLastVisibleItemPositions(int[] iArr) {
        if (iArr == null) {
            iArr = new int[this.mSpanCount];
        } else if (iArr.length < this.mSpanCount) {
            throw new IllegalArgumentException("Provided int[]'s size must be more than or equal to span count. Expected:" + this.mSpanCount + ", array size:" + iArr.length);
        }
        for (int i10 = 0; i10 < this.mSpanCount; i10++) {
            u2 u2Var = this.mSpans[i10];
            iArr[i10] = u2Var.f3914f.mReverseLayout ? u2Var.e(false, 0, u2Var.f3909a.size(), true, false) : u2Var.e(false, r3.size() - 1, -1, true, false);
        }
        return iArr;
    }

    @Override // androidx.recyclerview.widget.q1
    public r1 generateDefaultLayoutParams() {
        return this.mOrientation == 0 ? new q2(-2, -1) : new q2(-1, -2);
    }

    @Override // androidx.recyclerview.widget.q1
    public r1 generateLayoutParams(Context context, AttributeSet attributeSet) {
        return new q2(context, attributeSet);
    }

    public int getFirstChildPosition() {
        if (getChildCount() == 0) {
            return 0;
        }
        return getPosition(getChildAt(0));
    }

    public int getGapStrategy() {
        return this.mGapStrategy;
    }

    public int getLastChildPosition() {
        int childCount = getChildCount();
        if (childCount == 0) {
            return 0;
        }
        return getPosition(getChildAt(childCount - 1));
    }

    public int getOrientation() {
        return this.mOrientation;
    }

    public boolean getReverseLayout() {
        return this.mReverseLayout;
    }

    public int getSpanCount() {
        return this.mSpanCount;
    }

    /* JADX WARN: Removed duplicated region for block: B:50:0x00f0  */
    /* JADX WARN: Removed duplicated region for block: B:51:0x00f2  */
    /* JADX WARN: Removed duplicated region for block: B:53:0x00f5  */
    /* JADX WARN: Removed duplicated region for block: B:54:0x00f7  */
    /* JADX WARN: Removed duplicated region for block: B:67:0x00fa A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:73:0x002c A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public View hasGapsToFix() {
        int childCount = getChildCount();
        int i10 = childCount - 1;
        BitSet bitSet = new BitSet(this.mSpanCount);
        bitSet.set(0, this.mSpanCount, true);
        byte b10 = (this.mOrientation == 1 && isLayoutRTL()) ? (byte) 1 : (byte) -1;
        if (this.mShouldReverseLayout) {
            childCount = -1;
        } else {
            i10 = 0;
        }
        int i11 = i10 < childCount ? 1 : -1;
        while (i10 != childCount) {
            View childAt = getChildAt(i10);
            q2 q2Var = (q2) childAt.getLayoutParams();
            if (bitSet.get(q2Var.f3866a.f3913e)) {
                u2 u2Var = q2Var.f3866a;
                if (this.mShouldReverseLayout) {
                    int i12 = u2Var.f3911c;
                    if (i12 == Integer.MIN_VALUE) {
                        u2Var.a();
                        i12 = u2Var.f3911c;
                    }
                    if (i12 < this.mPrimaryOrientation.i()) {
                        ((q2) ((View) com.google.android.gms.measurement.internal.u1.e(u2Var.f3909a, 1)).getLayoutParams()).getClass();
                        return childAt;
                    }
                    bitSet.clear(q2Var.f3866a.f3913e);
                } else {
                    int i13 = u2Var.f3910b;
                    if (i13 == Integer.MIN_VALUE) {
                        View view = (View) u2Var.f3909a.get(0);
                        q2 q2Var2 = (q2) view.getLayoutParams();
                        u2Var.f3910b = u2Var.f3914f.mPrimaryOrientation.g(view);
                        q2Var2.getClass();
                        i13 = u2Var.f3910b;
                    }
                    if (i13 > this.mPrimaryOrientation.k()) {
                        ((q2) ((View) u2Var.f3909a.get(0)).getLayoutParams()).getClass();
                        return childAt;
                    }
                    bitSet.clear(q2Var.f3866a.f3913e);
                }
            }
            i10 += i11;
            if (i10 != childCount) {
                View childAt2 = getChildAt(i10);
                if (this.mShouldReverseLayout) {
                    int iD = this.mPrimaryOrientation.d(childAt);
                    int iD2 = this.mPrimaryOrientation.d(childAt2);
                    if (iD < iD2) {
                        return childAt;
                    }
                    if (iD == iD2) {
                        if ((q2Var.f3866a.f3913e - ((q2) childAt2.getLayoutParams()).f3866a.f3913e >= 0) == (b10 >= 0)) {
                            return childAt;
                        }
                    } else {
                        continue;
                    }
                } else {
                    int iG = this.mPrimaryOrientation.g(childAt);
                    int iG2 = this.mPrimaryOrientation.g(childAt2);
                    if (iG > iG2) {
                        return childAt;
                    }
                    if (iG == iG2) {
                        if ((q2Var.f3866a.f3913e - ((q2) childAt2.getLayoutParams()).f3866a.f3913e >= 0) == (b10 >= 0)) {
                        }
                    } else {
                        continue;
                    }
                }
            }
        }
        return null;
    }

    public void invalidateSpanAssignments() {
        this.mLazySpanLookup.a();
        requestLayout();
    }

    @Override // androidx.recyclerview.widget.q1
    public boolean isAutoMeasureEnabled() {
        return this.mGapStrategy != 0;
    }

    public boolean isLayoutRTL() {
        return getLayoutDirection() == 1;
    }

    public final int n(int i10) {
        if (getChildCount() == 0) {
            return this.mShouldReverseLayout ? 1 : -1;
        }
        return (i10 < getFirstChildPosition()) != this.mShouldReverseLayout ? -1 : 1;
    }

    public final int o(f2 f2Var) {
        if (getChildCount() == 0) {
            return 0;
        }
        return nb.g.k(f2Var, this.mPrimaryOrientation, findFirstVisibleItemClosestToStart(!this.mSmoothScrollbarEnabled), findFirstVisibleItemClosestToEnd(!this.mSmoothScrollbarEnabled), this, this.mSmoothScrollbarEnabled);
    }

    @Override // androidx.recyclerview.widget.q1
    public void offsetChildrenHorizontal(int i10) {
        super.offsetChildrenHorizontal(i10);
        for (int i11 = 0; i11 < this.mSpanCount; i11++) {
            u2 u2Var = this.mSpans[i11];
            int i12 = u2Var.f3910b;
            if (i12 != Integer.MIN_VALUE) {
                u2Var.f3910b = i12 + i10;
            }
            int i13 = u2Var.f3911c;
            if (i13 != Integer.MIN_VALUE) {
                u2Var.f3911c = i13 + i10;
            }
        }
    }

    @Override // androidx.recyclerview.widget.q1
    public void offsetChildrenVertical(int i10) {
        super.offsetChildrenVertical(i10);
        for (int i11 = 0; i11 < this.mSpanCount; i11++) {
            u2 u2Var = this.mSpans[i11];
            int i12 = u2Var.f3910b;
            if (i12 != Integer.MIN_VALUE) {
                u2Var.f3910b = i12 + i10;
            }
            int i13 = u2Var.f3911c;
            if (i13 != Integer.MIN_VALUE) {
                u2Var.f3911c = i13 + i10;
            }
        }
    }

    @Override // androidx.recyclerview.widget.q1
    public void onAdapterChanged(@Nullable d1 d1Var, @Nullable d1 d1Var2) {
        this.mLazySpanLookup.a();
        for (int i10 = 0; i10 < this.mSpanCount; i10++) {
            this.mSpans[i10].b();
        }
    }

    @Override // androidx.recyclerview.widget.q1
    public void onDetachedFromWindow(RecyclerView recyclerView, y1 y1Var) {
        onDetachedFromWindow(recyclerView);
        removeCallbacks(this.mCheckForGapsRunnable);
        for (int i10 = 0; i10 < this.mSpanCount; i10++) {
            this.mSpans[i10].b();
        }
        recyclerView.requestLayout();
    }

    /* JADX WARN: Removed duplicated region for block: B:35:0x0045  */
    /* JADX WARN: Removed duplicated region for block: B:38:0x004d  */
    @Override // androidx.recyclerview.widget.q1
    @Nullable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public View onFocusSearchFailed(View view, int i10, y1 y1Var, f2 f2Var) {
        View viewFindContainingItemView;
        int i11;
        if (getChildCount() == 0 || (viewFindContainingItemView = findContainingItemView(view)) == null) {
            return null;
        }
        D();
        if (i10 != 1) {
            if (i10 != 2) {
                if (i10 != 17) {
                    if (i10 != 33) {
                        if (i10 == 66 ? this.mOrientation == 0 : !(i10 != 130 || this.mOrientation != 1)) {
                            i11 = 1;
                        }
                    } else if (this.mOrientation == 1) {
                        i11 = -1;
                    }
                    i11 = Integer.MIN_VALUE;
                } else if (this.mOrientation != 0) {
                    i11 = Integer.MIN_VALUE;
                }
            } else if (this.mOrientation != 1 && isLayoutRTL()) {
            }
        } else if (this.mOrientation != 1 && isLayoutRTL()) {
        }
        if (i11 == Integer.MIN_VALUE) {
            return null;
        }
        q2 q2Var = (q2) viewFindContainingItemView.getLayoutParams();
        q2Var.getClass();
        u2 u2Var = q2Var.f3866a;
        int lastChildPosition = i11 == 1 ? getLastChildPosition() : getFirstChildPosition();
        F(lastChildPosition, f2Var);
        E(i11);
        o0 o0Var = this.mLayoutState;
        o0Var.f3827c = o0Var.f3828d + lastChildPosition;
        o0Var.f3826b = (int) (this.mPrimaryOrientation.l() * MAX_SCROLL_FACTOR);
        o0 o0Var2 = this.mLayoutState;
        o0Var2.f3832h = true;
        o0Var2.f3825a = false;
        r(y1Var, o0Var2, f2Var);
        this.mLastLayoutFromEnd = this.mShouldReverseLayout;
        View viewG = u2Var.g(lastChildPosition, i11);
        if (viewG != null && viewG != viewFindContainingItemView) {
            return viewG;
        }
        if (z(i11)) {
            for (int i12 = this.mSpanCount - 1; i12 >= 0; i12--) {
                View viewG2 = this.mSpans[i12].g(lastChildPosition, i11);
                if (viewG2 != null && viewG2 != viewFindContainingItemView) {
                    return viewG2;
                }
            }
        } else {
            for (int i13 = 0; i13 < this.mSpanCount; i13++) {
                View viewG3 = this.mSpans[i13].g(lastChildPosition, i11);
                if (viewG3 != null && viewG3 != viewFindContainingItemView) {
                    return viewG3;
                }
            }
        }
        boolean z10 = (this.mReverseLayout ^ true) == (i11 == -1);
        View viewFindViewByPosition = findViewByPosition(z10 ? u2Var.c() : u2Var.d());
        if (viewFindViewByPosition != null && viewFindViewByPosition != viewFindContainingItemView) {
            return viewFindViewByPosition;
        }
        if (z(i11)) {
            for (int i14 = this.mSpanCount - 1; i14 >= 0; i14--) {
                if (i14 != u2Var.f3913e) {
                    View viewFindViewByPosition2 = findViewByPosition(z10 ? this.mSpans[i14].c() : this.mSpans[i14].d());
                    if (viewFindViewByPosition2 != null && viewFindViewByPosition2 != viewFindContainingItemView) {
                        return viewFindViewByPosition2;
                    }
                }
            }
        } else {
            for (int i15 = 0; i15 < this.mSpanCount; i15++) {
                View viewFindViewByPosition3 = findViewByPosition(z10 ? this.mSpans[i15].c() : this.mSpans[i15].d());
                if (viewFindViewByPosition3 != null && viewFindViewByPosition3 != viewFindContainingItemView) {
                    return viewFindViewByPosition3;
                }
            }
        }
        return null;
    }

    @Override // androidx.recyclerview.widget.q1
    public void onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        super.onInitializeAccessibilityEvent(accessibilityEvent);
        if (getChildCount() > 0) {
            View viewFindFirstVisibleItemClosestToStart = findFirstVisibleItemClosestToStart(false);
            View viewFindFirstVisibleItemClosestToEnd = findFirstVisibleItemClosestToEnd(false);
            if (viewFindFirstVisibleItemClosestToStart == null || viewFindFirstVisibleItemClosestToEnd == null) {
                return;
            }
            int position = getPosition(viewFindFirstVisibleItemClosestToStart);
            int position2 = getPosition(viewFindFirstVisibleItemClosestToEnd);
            if (position < position2) {
                accessibilityEvent.setFromIndex(position);
                accessibilityEvent.setToIndex(position2);
            } else {
                accessibilityEvent.setFromIndex(position2);
                accessibilityEvent.setToIndex(position);
            }
        }
    }

    @Override // androidx.recyclerview.widget.q1
    public void onItemsAdded(RecyclerView recyclerView, int i10, int i11) {
        w(i10, i11, 1);
    }

    @Override // androidx.recyclerview.widget.q1
    public void onItemsChanged(RecyclerView recyclerView) {
        this.mLazySpanLookup.a();
        requestLayout();
    }

    @Override // androidx.recyclerview.widget.q1
    public void onItemsMoved(RecyclerView recyclerView, int i10, int i11, int i12) {
        w(i10, i11, 8);
    }

    @Override // androidx.recyclerview.widget.q1
    public void onItemsRemoved(RecyclerView recyclerView, int i10, int i11) {
        w(i10, i11, 2);
    }

    @Override // androidx.recyclerview.widget.q1
    public void onItemsUpdated(RecyclerView recyclerView, int i10, int i11, Object obj) {
        w(i10, i11, 4);
    }

    @Override // androidx.recyclerview.widget.q1
    public void onLayoutChildren(y1 y1Var, f2 f2Var) {
        y(y1Var, f2Var, true);
    }

    @Override // androidx.recyclerview.widget.q1
    public void onLayoutCompleted(f2 f2Var) {
        super.onLayoutCompleted(f2Var);
        this.mPendingScrollPosition = -1;
        this.mPendingScrollPositionOffset = Integer.MIN_VALUE;
        this.mPendingSavedState = null;
        this.mAnchorInfo.a();
    }

    @Override // androidx.recyclerview.widget.q1
    public void onRestoreInstanceState(Parcelable parcelable) {
        if (parcelable instanceof t2) {
            t2 t2Var = (t2) parcelable;
            this.mPendingSavedState = t2Var;
            if (this.mPendingScrollPosition != -1) {
                t2Var.f3899d = null;
                t2Var.f3898c = 0;
                t2Var.f3896a = -1;
                t2Var.f3897b = -1;
                t2Var.f3899d = null;
                t2Var.f3898c = 0;
                t2Var.f3900e = 0;
                t2Var.f3901f = null;
                t2Var.f3902g = null;
            }
            requestLayout();
        }
    }

    @Override // androidx.recyclerview.widget.q1
    public Parcelable onSaveInstanceState() {
        int iH;
        int iK;
        int[] iArr;
        t2 t2Var = this.mPendingSavedState;
        if (t2Var != null) {
            t2 t2Var2 = new t2();
            t2Var2.f3898c = t2Var.f3898c;
            t2Var2.f3896a = t2Var.f3896a;
            t2Var2.f3897b = t2Var.f3897b;
            t2Var2.f3899d = t2Var.f3899d;
            t2Var2.f3900e = t2Var.f3900e;
            t2Var2.f3901f = t2Var.f3901f;
            t2Var2.f3903h = t2Var.f3903h;
            t2Var2.f3904i = t2Var.f3904i;
            t2Var2.f3905j = t2Var.f3905j;
            t2Var2.f3902g = t2Var.f3902g;
            return t2Var2;
        }
        t2 t2Var3 = new t2();
        t2Var3.f3903h = this.mReverseLayout;
        t2Var3.f3904i = this.mLastLayoutFromEnd;
        t2Var3.f3905j = this.mLastLayoutRTL;
        s2 s2Var = this.mLazySpanLookup;
        if (s2Var == null || (iArr = s2Var.f3892a) == null) {
            t2Var3.f3900e = 0;
        } else {
            t2Var3.f3901f = iArr;
            t2Var3.f3900e = iArr.length;
            t2Var3.f3902g = s2Var.f3893b;
        }
        if (getChildCount() > 0) {
            t2Var3.f3896a = this.mLastLayoutFromEnd ? getLastChildPosition() : getFirstChildPosition();
            t2Var3.f3897b = findFirstVisibleItemPositionInt();
            int i10 = this.mSpanCount;
            t2Var3.f3898c = i10;
            t2Var3.f3899d = new int[i10];
            for (int i11 = 0; i11 < this.mSpanCount; i11++) {
                if (this.mLastLayoutFromEnd) {
                    iH = this.mSpans[i11].f(Integer.MIN_VALUE);
                    if (iH != Integer.MIN_VALUE) {
                        iK = this.mPrimaryOrientation.i();
                        iH -= iK;
                    }
                } else {
                    iH = this.mSpans[i11].h(Integer.MIN_VALUE);
                    if (iH != Integer.MIN_VALUE) {
                        iK = this.mPrimaryOrientation.k();
                        iH -= iK;
                    }
                }
                t2Var3.f3899d[i11] = iH;
            }
        } else {
            t2Var3.f3896a = -1;
            t2Var3.f3897b = -1;
            t2Var3.f3898c = 0;
        }
        return t2Var3;
    }

    @Override // androidx.recyclerview.widget.q1
    public void onScrollStateChanged(int i10) {
        if (i10 == 0) {
            checkForGaps();
        }
    }

    public final int p(f2 f2Var) {
        if (getChildCount() == 0) {
            return 0;
        }
        return nb.g.l(f2Var, this.mPrimaryOrientation, findFirstVisibleItemClosestToStart(!this.mSmoothScrollbarEnabled), findFirstVisibleItemClosestToEnd(!this.mSmoothScrollbarEnabled), this, this.mSmoothScrollbarEnabled, this.mShouldReverseLayout);
    }

    public void prepareLayoutStateForDelta(int i10, f2 f2Var) {
        int firstChildPosition;
        int i11;
        if (i10 > 0) {
            firstChildPosition = getLastChildPosition();
            i11 = 1;
        } else {
            firstChildPosition = getFirstChildPosition();
            i11 = -1;
        }
        this.mLayoutState.f3825a = true;
        F(firstChildPosition, f2Var);
        E(i11);
        o0 o0Var = this.mLayoutState;
        o0Var.f3827c = firstChildPosition + o0Var.f3828d;
        o0Var.f3826b = Math.abs(i10);
    }

    public final int q(f2 f2Var) {
        if (getChildCount() == 0) {
            return 0;
        }
        return nb.g.m(f2Var, this.mPrimaryOrientation, findFirstVisibleItemClosestToStart(!this.mSmoothScrollbarEnabled), findFirstVisibleItemClosestToEnd(!this.mSmoothScrollbarEnabled), this, this.mSmoothScrollbarEnabled);
    }

    /* JADX WARN: Type inference failed for: r1v21 */
    /* JADX WARN: Type inference failed for: r1v22, types: [boolean, int] */
    /* JADX WARN: Type inference failed for: r1v54 */
    public final int r(y1 y1Var, o0 o0Var, f2 f2Var) {
        u2 u2Var;
        ?? r12;
        int iE;
        int iE2;
        int iK;
        int iE3;
        int i10;
        int i11;
        int i12;
        int i13 = 0;
        this.mRemainingSpans.set(0, this.mSpanCount, true);
        int i14 = this.mLayoutState.f3833i ? o0Var.f3829e == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE : o0Var.f3829e == 1 ? o0Var.f3831g + o0Var.f3826b : o0Var.f3830f - o0Var.f3826b;
        int i15 = o0Var.f3829e;
        for (int i16 = 0; i16 < this.mSpanCount; i16++) {
            if (!this.mSpans[i16].f3909a.isEmpty()) {
                G(this.mSpans[i16], i15, i14);
            }
        }
        int i17 = this.mShouldReverseLayout ? this.mPrimaryOrientation.i() : this.mPrimaryOrientation.k();
        boolean z10 = false;
        while (true) {
            int i18 = o0Var.f3827c;
            int i19 = -1;
            if (((i18 < 0 || i18 >= f2Var.b()) ? i13 : 1) == 0 || (!this.mLayoutState.f3833i && this.mRemainingSpans.isEmpty())) {
                break;
            }
            View viewD = y1Var.d(o0Var.f3827c);
            o0Var.f3827c += o0Var.f3828d;
            q2 q2Var = (q2) viewD.getLayoutParams();
            int viewLayoutPosition = q2Var.getViewLayoutPosition();
            int[] iArr = this.mLazySpanLookup.f3892a;
            int i20 = (iArr == null || viewLayoutPosition >= iArr.length) ? -1 : iArr[viewLayoutPosition];
            if (i20 == -1) {
                if (z(o0Var.f3829e)) {
                    i11 = this.mSpanCount - 1;
                    i12 = -1;
                } else {
                    i19 = this.mSpanCount;
                    i11 = i13;
                    i12 = 1;
                }
                u2 u2Var2 = null;
                if (o0Var.f3829e == 1) {
                    int iK2 = this.mPrimaryOrientation.k();
                    int i21 = Integer.MAX_VALUE;
                    while (i11 != i19) {
                        u2 u2Var3 = this.mSpans[i11];
                        int iF = u2Var3.f(iK2);
                        if (iF < i21) {
                            i21 = iF;
                            u2Var2 = u2Var3;
                        }
                        i11 += i12;
                    }
                } else {
                    int i22 = this.mPrimaryOrientation.i();
                    int i23 = Integer.MIN_VALUE;
                    while (i11 != i19) {
                        u2 u2Var4 = this.mSpans[i11];
                        int iH = u2Var4.h(i22);
                        if (iH > i23) {
                            u2Var2 = u2Var4;
                            i23 = iH;
                        }
                        i11 += i12;
                    }
                }
                u2Var = u2Var2;
                s2 s2Var = this.mLazySpanLookup;
                s2Var.b(viewLayoutPosition);
                s2Var.f3892a[viewLayoutPosition] = u2Var.f3913e;
            } else {
                u2Var = this.mSpans[i20];
            }
            u2 u2Var5 = u2Var;
            q2Var.f3866a = u2Var5;
            if (o0Var.f3829e == 1) {
                addView(viewD);
                r12 = 0;
            } else {
                r12 = 0;
                addView(viewD, 0);
            }
            if (this.mOrientation == 1) {
                x(viewD, q1.getChildMeasureSpec(this.mSizePerSpan, getWidthMode(), r12, ((ViewGroup.MarginLayoutParams) q2Var).width, r12), q1.getChildMeasureSpec(getHeight(), getHeightMode(), getPaddingBottom() + getPaddingTop(), ((ViewGroup.MarginLayoutParams) q2Var).height, true));
            } else {
                x(viewD, q1.getChildMeasureSpec(getWidth(), getWidthMode(), getPaddingRight() + getPaddingLeft(), ((ViewGroup.MarginLayoutParams) q2Var).width, true), q1.getChildMeasureSpec(this.mSizePerSpan, getHeightMode(), 0, ((ViewGroup.MarginLayoutParams) q2Var).height, false));
            }
            if (o0Var.f3829e == 1) {
                int iF2 = u2Var5.f(i17);
                iE2 = iF2;
                iE = this.mPrimaryOrientation.e(viewD) + iF2;
            } else {
                int iH2 = u2Var5.h(i17);
                iE = iH2;
                iE2 = iH2 - this.mPrimaryOrientation.e(viewD);
            }
            if (o0Var.f3829e == 1) {
                u2 u2Var6 = q2Var.f3866a;
                u2Var6.getClass();
                q2 q2Var2 = (q2) viewD.getLayoutParams();
                q2Var2.f3866a = u2Var6;
                ArrayList arrayList = u2Var6.f3909a;
                arrayList.add(viewD);
                u2Var6.f3911c = Integer.MIN_VALUE;
                if (arrayList.size() == 1) {
                    u2Var6.f3910b = Integer.MIN_VALUE;
                }
                if (q2Var2.isItemRemoved() || q2Var2.isItemChanged()) {
                    u2Var6.f3912d = u2Var6.f3914f.mPrimaryOrientation.e(viewD) + u2Var6.f3912d;
                }
            } else {
                u2 u2Var7 = q2Var.f3866a;
                u2Var7.getClass();
                q2 q2Var3 = (q2) viewD.getLayoutParams();
                q2Var3.f3866a = u2Var7;
                ArrayList arrayList2 = u2Var7.f3909a;
                arrayList2.add(0, viewD);
                u2Var7.f3910b = Integer.MIN_VALUE;
                if (arrayList2.size() == 1) {
                    u2Var7.f3911c = Integer.MIN_VALUE;
                }
                if (q2Var3.isItemRemoved() || q2Var3.isItemChanged()) {
                    u2Var7.f3912d = u2Var7.f3914f.mPrimaryOrientation.e(viewD) + u2Var7.f3912d;
                }
            }
            if (isLayoutRTL() && this.mOrientation == 1) {
                iE3 = this.mSecondaryOrientation.i() - (((this.mSpanCount - 1) - u2Var5.f3913e) * this.mSizePerSpan);
                iK = iE3 - this.mSecondaryOrientation.e(viewD);
            } else {
                iK = this.mSecondaryOrientation.k() + (u2Var5.f3913e * this.mSizePerSpan);
                iE3 = this.mSecondaryOrientation.e(viewD) + iK;
            }
            int i24 = iE3;
            int i25 = iK;
            if (this.mOrientation == 1) {
                layoutDecoratedWithMargins(viewD, i25, iE2, i24, iE);
            } else {
                layoutDecoratedWithMargins(viewD, iE2, i25, iE, i24);
            }
            G(u2Var5, this.mLayoutState.f3829e, i14);
            A(y1Var, this.mLayoutState);
            if (this.mLayoutState.f3832h && viewD.hasFocusable()) {
                i10 = 0;
                this.mRemainingSpans.set(u2Var5.f3913e, false);
            } else {
                i10 = 0;
            }
            i13 = i10;
            z10 = true;
        }
        int i26 = i13;
        if (!z10) {
            A(y1Var, this.mLayoutState);
        }
        int iK3 = this.mLayoutState.f3829e == -1 ? this.mPrimaryOrientation.k() - v(this.mPrimaryOrientation.k()) : u(this.mPrimaryOrientation.i()) - this.mPrimaryOrientation.i();
        return iK3 > 0 ? Math.min(o0Var.f3826b, iK3) : i26;
    }

    public final void s(y1 y1Var, f2 f2Var, boolean z10) {
        int i10;
        int iU = u(Integer.MIN_VALUE);
        if (iU != Integer.MIN_VALUE && (i10 = this.mPrimaryOrientation.i() - iU) > 0) {
            int i11 = i10 - (-scrollBy(-i10, y1Var, f2Var));
            if (!z10 || i11 <= 0) {
                return;
            }
            this.mPrimaryOrientation.p(i11);
        }
    }

    public int scrollBy(int i10, y1 y1Var, f2 f2Var) {
        if (getChildCount() == 0 || i10 == 0) {
            return 0;
        }
        prepareLayoutStateForDelta(i10, f2Var);
        int iR = r(y1Var, this.mLayoutState, f2Var);
        if (this.mLayoutState.f3826b >= iR) {
            i10 = i10 < 0 ? -iR : iR;
        }
        this.mPrimaryOrientation.p(-i10);
        this.mLastLayoutFromEnd = this.mShouldReverseLayout;
        o0 o0Var = this.mLayoutState;
        o0Var.f3826b = 0;
        A(y1Var, o0Var);
        return i10;
    }

    @Override // androidx.recyclerview.widget.q1
    public int scrollHorizontallyBy(int i10, y1 y1Var, f2 f2Var) {
        return scrollBy(i10, y1Var, f2Var);
    }

    @Override // androidx.recyclerview.widget.q1
    public void scrollToPosition(int i10) {
        t2 t2Var = this.mPendingSavedState;
        if (t2Var != null && t2Var.f3896a != i10) {
            t2Var.f3899d = null;
            t2Var.f3898c = 0;
            t2Var.f3896a = -1;
            t2Var.f3897b = -1;
        }
        this.mPendingScrollPosition = i10;
        this.mPendingScrollPositionOffset = Integer.MIN_VALUE;
        requestLayout();
    }

    public void scrollToPositionWithOffset(int i10, int i11) {
        t2 t2Var = this.mPendingSavedState;
        if (t2Var != null) {
            t2Var.f3899d = null;
            t2Var.f3898c = 0;
            t2Var.f3896a = -1;
            t2Var.f3897b = -1;
        }
        this.mPendingScrollPosition = i10;
        this.mPendingScrollPositionOffset = i11;
        requestLayout();
    }

    @Override // androidx.recyclerview.widget.q1
    public int scrollVerticallyBy(int i10, y1 y1Var, f2 f2Var) {
        return scrollBy(i10, y1Var, f2Var);
    }

    public void setGapStrategy(int i10) {
        assertNotInLayoutOrScroll(null);
        if (i10 == this.mGapStrategy) {
            return;
        }
        if (i10 != 0 && i10 != 2) {
            throw new IllegalArgumentException("invalid gap strategy. Must be GAP_HANDLING_NONE or GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS");
        }
        this.mGapStrategy = i10;
        requestLayout();
    }

    @Override // androidx.recyclerview.widget.q1
    public void setMeasuredDimension(Rect rect, int i10, int i11) {
        int iChooseSize;
        int iChooseSize2;
        int paddingRight = getPaddingRight() + getPaddingLeft();
        int paddingBottom = getPaddingBottom() + getPaddingTop();
        if (this.mOrientation == 1) {
            iChooseSize2 = q1.chooseSize(i11, rect.height() + paddingBottom, getMinimumHeight());
            iChooseSize = q1.chooseSize(i10, (this.mSizePerSpan * this.mSpanCount) + paddingRight, getMinimumWidth());
        } else {
            iChooseSize = q1.chooseSize(i10, rect.width() + paddingRight, getMinimumWidth());
            iChooseSize2 = q1.chooseSize(i11, (this.mSizePerSpan * this.mSpanCount) + paddingBottom, getMinimumHeight());
        }
        setMeasuredDimension(iChooseSize, iChooseSize2);
    }

    public void setOrientation(int i10) {
        if (i10 != 0 && i10 != 1) {
            throw new IllegalArgumentException("invalid orientation.");
        }
        assertNotInLayoutOrScroll(null);
        if (i10 == this.mOrientation) {
            return;
        }
        this.mOrientation = i10;
        x0 x0Var = this.mPrimaryOrientation;
        this.mPrimaryOrientation = this.mSecondaryOrientation;
        this.mSecondaryOrientation = x0Var;
        requestLayout();
    }

    public void setReverseLayout(boolean z10) {
        assertNotInLayoutOrScroll(null);
        t2 t2Var = this.mPendingSavedState;
        if (t2Var != null && t2Var.f3903h != z10) {
            t2Var.f3903h = z10;
        }
        this.mReverseLayout = z10;
        requestLayout();
    }

    public void setSpanCount(int i10) {
        assertNotInLayoutOrScroll(null);
        if (i10 != this.mSpanCount) {
            invalidateSpanAssignments();
            this.mSpanCount = i10;
            this.mRemainingSpans = new BitSet(this.mSpanCount);
            this.mSpans = new u2[this.mSpanCount];
            for (int i11 = 0; i11 < this.mSpanCount; i11++) {
                this.mSpans[i11] = new u2(this, i11);
            }
            requestLayout();
        }
    }

    @Override // androidx.recyclerview.widget.q1
    public void smoothScrollToPosition(RecyclerView recyclerView, f2 f2Var, int i10) {
        t0 t0Var = new t0(recyclerView.getContext());
        t0Var.setTargetPosition(i10);
        startSmoothScroll(t0Var);
    }

    @Override // androidx.recyclerview.widget.q1
    public boolean supportsPredictiveItemAnimations() {
        return this.mPendingSavedState == null;
    }

    public final void t(y1 y1Var, f2 f2Var, boolean z10) {
        int iK;
        int iV = v(Integer.MAX_VALUE);
        if (iV != Integer.MAX_VALUE && (iK = iV - this.mPrimaryOrientation.k()) > 0) {
            int iScrollBy = iK - scrollBy(iK, y1Var, f2Var);
            if (!z10 || iScrollBy <= 0) {
                return;
            }
            this.mPrimaryOrientation.p(-iScrollBy);
        }
    }

    public final int u(int i10) {
        int iF = this.mSpans[0].f(i10);
        for (int i11 = 1; i11 < this.mSpanCount; i11++) {
            int iF2 = this.mSpans[i11].f(i10);
            if (iF2 > iF) {
                iF = iF2;
            }
        }
        return iF;
    }

    public boolean updateAnchorFromPendingData(f2 f2Var, p2 p2Var) {
        int i10;
        if (!f2Var.f3706g && (i10 = this.mPendingScrollPosition) != -1) {
            if (i10 >= 0 && i10 < f2Var.b()) {
                t2 t2Var = this.mPendingSavedState;
                if (t2Var == null || t2Var.f3896a == -1 || t2Var.f3898c < 1) {
                    View viewFindViewByPosition = findViewByPosition(this.mPendingScrollPosition);
                    if (viewFindViewByPosition != null) {
                        p2Var.f3855a = this.mShouldReverseLayout ? getLastChildPosition() : getFirstChildPosition();
                        if (this.mPendingScrollPositionOffset != Integer.MIN_VALUE) {
                            if (p2Var.f3857c) {
                                p2Var.f3856b = (this.mPrimaryOrientation.i() - this.mPendingScrollPositionOffset) - this.mPrimaryOrientation.d(viewFindViewByPosition);
                            } else {
                                p2Var.f3856b = (this.mPrimaryOrientation.k() + this.mPendingScrollPositionOffset) - this.mPrimaryOrientation.g(viewFindViewByPosition);
                            }
                            return true;
                        }
                        if (this.mPrimaryOrientation.e(viewFindViewByPosition) > this.mPrimaryOrientation.l()) {
                            p2Var.f3856b = p2Var.f3857c ? this.mPrimaryOrientation.i() : this.mPrimaryOrientation.k();
                            return true;
                        }
                        int iG = this.mPrimaryOrientation.g(viewFindViewByPosition) - this.mPrimaryOrientation.k();
                        if (iG < 0) {
                            p2Var.f3856b = -iG;
                            return true;
                        }
                        int i11 = this.mPrimaryOrientation.i() - this.mPrimaryOrientation.d(viewFindViewByPosition);
                        if (i11 < 0) {
                            p2Var.f3856b = i11;
                            return true;
                        }
                        p2Var.f3856b = Integer.MIN_VALUE;
                    } else {
                        int i12 = this.mPendingScrollPosition;
                        p2Var.f3855a = i12;
                        int i13 = this.mPendingScrollPositionOffset;
                        StaggeredGridLayoutManager staggeredGridLayoutManager = p2Var.f3861g;
                        if (i13 == Integer.MIN_VALUE) {
                            boolean z10 = n(i12) == 1;
                            p2Var.f3857c = z10;
                            p2Var.f3856b = z10 ? staggeredGridLayoutManager.mPrimaryOrientation.i() : staggeredGridLayoutManager.mPrimaryOrientation.k();
                        } else if (p2Var.f3857c) {
                            p2Var.f3856b = staggeredGridLayoutManager.mPrimaryOrientation.i() - i13;
                        } else {
                            p2Var.f3856b = staggeredGridLayoutManager.mPrimaryOrientation.k() + i13;
                        }
                        p2Var.f3858d = true;
                    }
                } else {
                    p2Var.f3856b = Integer.MIN_VALUE;
                    p2Var.f3855a = this.mPendingScrollPosition;
                }
                return true;
            }
            this.mPendingScrollPosition = -1;
            this.mPendingScrollPositionOffset = Integer.MIN_VALUE;
        }
        return false;
    }

    public void updateAnchorInfoForLayout(f2 f2Var, p2 p2Var) {
        if (updateAnchorFromPendingData(f2Var, p2Var)) {
            return;
        }
        int i10 = 0;
        if (!this.mLastLayoutFromEnd) {
            int iB = f2Var.b();
            int childCount = getChildCount();
            int i11 = 0;
            while (true) {
                if (i11 < childCount) {
                    int position = getPosition(getChildAt(i11));
                    if (position >= 0 && position < iB) {
                        i10 = position;
                        break;
                    }
                    i11++;
                } else {
                    break;
                }
            }
        } else {
            int iB2 = f2Var.b();
            int childCount2 = getChildCount() - 1;
            while (true) {
                if (childCount2 >= 0) {
                    int position2 = getPosition(getChildAt(childCount2));
                    if (position2 >= 0 && position2 < iB2) {
                        i10 = position2;
                        break;
                    }
                    childCount2--;
                } else {
                    break;
                }
            }
        }
        p2Var.f3855a = i10;
        p2Var.f3856b = Integer.MIN_VALUE;
    }

    public void updateMeasureSpecs(int i10) {
        this.mSizePerSpan = i10 / this.mSpanCount;
        this.mFullSizeSpec = View.MeasureSpec.makeMeasureSpec(i10, this.mSecondaryOrientation.j());
    }

    public final int v(int i10) {
        int iH = this.mSpans[0].h(i10);
        for (int i11 = 1; i11 < this.mSpanCount; i11++) {
            int iH2 = this.mSpans[i11].h(i10);
            if (iH2 < iH) {
                iH = iH2;
            }
        }
        return iH;
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x0026  */
    /* JADX WARN: Removed duplicated region for block: B:21:0x003d  */
    /* JADX WARN: Removed duplicated region for block: B:23:0x0044 A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:24:0x0045  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void w(int i10, int i11, int i12) {
        int i13;
        int i14;
        int lastChildPosition = this.mShouldReverseLayout ? getLastChildPosition() : getFirstChildPosition();
        if (i12 != 8) {
            i13 = i10 + i11;
        } else {
            if (i10 >= i11) {
                i13 = i10 + 1;
                i14 = i11;
                this.mLazySpanLookup.e(i14);
                if (i12 != 1) {
                    this.mLazySpanLookup.f(i10, i11);
                } else if (i12 == 2) {
                    this.mLazySpanLookup.g(i10, i11);
                } else if (i12 == 8) {
                    this.mLazySpanLookup.g(i10, 1);
                    this.mLazySpanLookup.f(i11, 1);
                }
                if (i13 > lastChildPosition) {
                    return;
                }
                if (i14 <= (this.mShouldReverseLayout ? getFirstChildPosition() : getLastChildPosition())) {
                    requestLayout();
                    return;
                }
                return;
            }
            i13 = i11 + 1;
        }
        i14 = i10;
        this.mLazySpanLookup.e(i14);
        if (i12 != 1) {
        }
        if (i13 > lastChildPosition) {
        }
    }

    public final void x(View view, int i10, int i11) {
        calculateItemDecorationsForChild(view, this.mTmpRect);
        q2 q2Var = (q2) view.getLayoutParams();
        int i12 = ((ViewGroup.MarginLayoutParams) q2Var).leftMargin;
        Rect rect = this.mTmpRect;
        int iH = H(i10, i12 + rect.left, ((ViewGroup.MarginLayoutParams) q2Var).rightMargin + rect.right);
        int i13 = ((ViewGroup.MarginLayoutParams) q2Var).topMargin;
        Rect rect2 = this.mTmpRect;
        int iH2 = H(i11, i13 + rect2.top, ((ViewGroup.MarginLayoutParams) q2Var).bottomMargin + rect2.bottom);
        if (shouldMeasureChild(view, iH, iH2, q2Var)) {
            view.measure(iH, iH2);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:162:0x02ae  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void y(y1 y1Var, f2 f2Var, boolean z10) {
        t2 t2Var;
        p2 p2Var = this.mAnchorInfo;
        if (!(this.mPendingSavedState == null && this.mPendingScrollPosition == -1) && f2Var.b() == 0) {
            removeAndRecycleAllViews(y1Var);
            p2Var.a();
            return;
        }
        boolean z11 = true;
        boolean z12 = (p2Var.f3859e && this.mPendingScrollPosition == -1 && this.mPendingSavedState == null) ? false : true;
        if (z12) {
            p2Var.a();
            t2 t2Var2 = this.mPendingSavedState;
            if (t2Var2 != null) {
                int i10 = t2Var2.f3898c;
                if (i10 > 0) {
                    if (i10 == this.mSpanCount) {
                        for (int i11 = 0; i11 < this.mSpanCount; i11++) {
                            this.mSpans[i11].b();
                            t2 t2Var3 = this.mPendingSavedState;
                            int i12 = t2Var3.f3899d[i11];
                            if (i12 != Integer.MIN_VALUE) {
                                i12 += t2Var3.f3904i ? this.mPrimaryOrientation.i() : this.mPrimaryOrientation.k();
                            }
                            u2 u2Var = this.mSpans[i11];
                            u2Var.f3910b = i12;
                            u2Var.f3911c = i12;
                        }
                    } else {
                        t2Var2.f3899d = null;
                        t2Var2.f3898c = 0;
                        t2Var2.f3900e = 0;
                        t2Var2.f3901f = null;
                        t2Var2.f3902g = null;
                        t2Var2.f3896a = t2Var2.f3897b;
                    }
                }
                t2 t2Var4 = this.mPendingSavedState;
                this.mLastLayoutRTL = t2Var4.f3905j;
                setReverseLayout(t2Var4.f3903h);
                D();
                t2 t2Var5 = this.mPendingSavedState;
                int i13 = t2Var5.f3896a;
                if (i13 != -1) {
                    this.mPendingScrollPosition = i13;
                    p2Var.f3857c = t2Var5.f3904i;
                } else {
                    p2Var.f3857c = this.mShouldReverseLayout;
                }
                if (t2Var5.f3900e > 1) {
                    s2 s2Var = this.mLazySpanLookup;
                    s2Var.f3892a = t2Var5.f3901f;
                    s2Var.f3893b = t2Var5.f3902g;
                }
            } else {
                D();
                p2Var.f3857c = this.mShouldReverseLayout;
            }
            updateAnchorInfoForLayout(f2Var, p2Var);
            p2Var.f3859e = true;
        }
        if (this.mPendingSavedState == null && this.mPendingScrollPosition == -1 && (p2Var.f3857c != this.mLastLayoutFromEnd || isLayoutRTL() != this.mLastLayoutRTL)) {
            this.mLazySpanLookup.a();
            p2Var.f3858d = true;
        }
        if (getChildCount() > 0 && ((t2Var = this.mPendingSavedState) == null || t2Var.f3898c < 1)) {
            if (p2Var.f3858d) {
                for (int i14 = 0; i14 < this.mSpanCount; i14++) {
                    this.mSpans[i14].b();
                    int i15 = p2Var.f3856b;
                    if (i15 != Integer.MIN_VALUE) {
                        u2 u2Var2 = this.mSpans[i14];
                        u2Var2.f3910b = i15;
                        u2Var2.f3911c = i15;
                    }
                }
            } else if (z12 || this.mAnchorInfo.f3860f == null) {
                for (int i16 = 0; i16 < this.mSpanCount; i16++) {
                    u2 u2Var3 = this.mSpans[i16];
                    boolean z13 = this.mShouldReverseLayout;
                    int i17 = p2Var.f3856b;
                    int iF = z13 ? u2Var3.f(Integer.MIN_VALUE) : u2Var3.h(Integer.MIN_VALUE);
                    u2Var3.b();
                    if (iF != Integer.MIN_VALUE) {
                        StaggeredGridLayoutManager staggeredGridLayoutManager = u2Var3.f3914f;
                        if ((!z13 || iF >= staggeredGridLayoutManager.mPrimaryOrientation.i()) && (z13 || iF <= staggeredGridLayoutManager.mPrimaryOrientation.k())) {
                            if (i17 != Integer.MIN_VALUE) {
                                iF += i17;
                            }
                            u2Var3.f3911c = iF;
                            u2Var3.f3910b = iF;
                        }
                    }
                }
                p2 p2Var2 = this.mAnchorInfo;
                u2[] u2VarArr = this.mSpans;
                p2Var2.getClass();
                int length = u2VarArr.length;
                int[] iArr = p2Var2.f3860f;
                if (iArr == null || iArr.length < length) {
                    p2Var2.f3860f = new int[p2Var2.f3861g.mSpans.length];
                }
                for (int i18 = 0; i18 < length; i18++) {
                    p2Var2.f3860f[i18] = u2VarArr[i18].h(Integer.MIN_VALUE);
                }
            } else {
                for (int i19 = 0; i19 < this.mSpanCount; i19++) {
                    u2 u2Var4 = this.mSpans[i19];
                    u2Var4.b();
                    int i20 = this.mAnchorInfo.f3860f[i19];
                    u2Var4.f3910b = i20;
                    u2Var4.f3911c = i20;
                }
            }
        }
        detachAndScrapAttachedViews(y1Var);
        this.mLayoutState.f3825a = false;
        this.mLaidOutInvalidFullSpan = false;
        updateMeasureSpecs(this.mSecondaryOrientation.l());
        F(p2Var.f3855a, f2Var);
        if (p2Var.f3857c) {
            E(-1);
            r(y1Var, this.mLayoutState, f2Var);
            E(1);
            o0 o0Var = this.mLayoutState;
            o0Var.f3827c = p2Var.f3855a + o0Var.f3828d;
            r(y1Var, o0Var, f2Var);
        } else {
            E(1);
            r(y1Var, this.mLayoutState, f2Var);
            E(-1);
            o0 o0Var2 = this.mLayoutState;
            o0Var2.f3827c = p2Var.f3855a + o0Var2.f3828d;
            r(y1Var, o0Var2, f2Var);
        }
        if (this.mSecondaryOrientation.j() != 1073741824) {
            int childCount = getChildCount();
            float fMax = 0.0f;
            for (int i21 = 0; i21 < childCount; i21++) {
                View childAt = getChildAt(i21);
                float fE = this.mSecondaryOrientation.e(childAt);
                if (fE >= fMax) {
                    ((q2) childAt.getLayoutParams()).getClass();
                    fMax = Math.max(fMax, fE);
                }
            }
            int i22 = this.mSizePerSpan;
            int iRound = Math.round(fMax * this.mSpanCount);
            if (this.mSecondaryOrientation.j() == Integer.MIN_VALUE) {
                iRound = Math.min(iRound, this.mSecondaryOrientation.l());
            }
            updateMeasureSpecs(iRound);
            if (this.mSizePerSpan != i22) {
                for (int i23 = 0; i23 < childCount; i23++) {
                    View childAt2 = getChildAt(i23);
                    q2 q2Var = (q2) childAt2.getLayoutParams();
                    q2Var.getClass();
                    if (isLayoutRTL() && this.mOrientation == 1) {
                        int i24 = -((this.mSpanCount - 1) - q2Var.f3866a.f3913e);
                        childAt2.offsetLeftAndRight((this.mSizePerSpan * i24) - (i24 * i22));
                    } else {
                        int i25 = q2Var.f3866a.f3913e;
                        int i26 = this.mSizePerSpan * i25;
                        int i27 = i25 * i22;
                        if (this.mOrientation == 1) {
                            childAt2.offsetLeftAndRight(i26 - i27);
                        } else {
                            childAt2.offsetTopAndBottom(i26 - i27);
                        }
                    }
                }
            }
        }
        if (getChildCount() > 0) {
            if (this.mShouldReverseLayout) {
                s(y1Var, f2Var, true);
                t(y1Var, f2Var, false);
            } else {
                t(y1Var, f2Var, true);
                s(y1Var, f2Var, false);
            }
        }
        if (!z10 || f2Var.f3706g || this.mGapStrategy == 0 || getChildCount() <= 0 || (!this.mLaidOutInvalidFullSpan && hasGapsToFix() == null)) {
            z11 = false;
        } else {
            removeCallbacks(this.mCheckForGapsRunnable);
            if (!checkForGaps()) {
            }
        }
        if (f2Var.f3706g) {
            this.mAnchorInfo.a();
        }
        this.mLastLayoutFromEnd = p2Var.f3857c;
        this.mLastLayoutRTL = isLayoutRTL();
        if (z11) {
            this.mAnchorInfo.a();
            y(y1Var, f2Var, false);
        }
    }

    public final boolean z(int i10) {
        if (this.mOrientation == 0) {
            return (i10 == -1) != this.mShouldReverseLayout;
        }
        return ((i10 == -1) == this.mShouldReverseLayout) == isLayoutRTL();
    }

    @Override // androidx.recyclerview.widget.q1
    public r1 generateLayoutParams(ViewGroup.LayoutParams layoutParams) {
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            return new q2((ViewGroup.MarginLayoutParams) layoutParams);
        }
        return new q2(layoutParams);
    }

    public StaggeredGridLayoutManager(int i10, int i11) {
        this.mOrientation = i11;
        setSpanCount(i10);
        this.mLayoutState = new o0();
        this.mPrimaryOrientation = x0.b(this, this.mOrientation);
        this.mSecondaryOrientation = x0.b(this, 1 - this.mOrientation);
    }
}
