package androidx.recyclerview.widget;

import android.view.View;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes.dex */
public final class u2 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ArrayList f3909a = new ArrayList();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f3910b = Integer.MIN_VALUE;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f3911c = Integer.MIN_VALUE;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public int f3912d = 0;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final /* synthetic */ StaggeredGridLayoutManager f3914f;

    public u2(StaggeredGridLayoutManager staggeredGridLayoutManager, int i10) {
        this.f3914f = staggeredGridLayoutManager;
        this.f3913e = i10;
    }

    public final void a() {
        View view = (View) com.google.android.gms.measurement.internal.u1.e(this.f3909a, 1);
        q2 q2Var = (q2) view.getLayoutParams();
        this.f3911c = this.f3914f.mPrimaryOrientation.d(view);
        q2Var.getClass();
    }

    public final void b() {
        this.f3909a.clear();
        this.f3910b = Integer.MIN_VALUE;
        this.f3911c = Integer.MIN_VALUE;
        this.f3912d = 0;
    }

    public final int c() {
        return this.f3914f.mReverseLayout ? e(false, r1.size() - 1, -1, false, true) : e(false, 0, this.f3909a.size(), false, true);
    }

    public final int d() {
        return this.f3914f.mReverseLayout ? e(false, 0, this.f3909a.size(), false, true) : e(false, r1.size() - 1, -1, false, true);
    }

    public final int e(boolean z10, int i10, int i11, boolean z11, boolean z12) {
        StaggeredGridLayoutManager staggeredGridLayoutManager = this.f3914f;
        int iK = staggeredGridLayoutManager.mPrimaryOrientation.k();
        int i12 = staggeredGridLayoutManager.mPrimaryOrientation.i();
        int i13 = i10;
        int i14 = i11 > i13 ? 1 : -1;
        while (i13 != i11) {
            View view = (View) this.f3909a.get(i13);
            int iG = staggeredGridLayoutManager.mPrimaryOrientation.g(view);
            int iD = staggeredGridLayoutManager.mPrimaryOrientation.d(view);
            boolean z13 = false;
            boolean z14 = !z12 ? iG >= i12 : iG > i12;
            if (!z12 ? iD > iK : iD >= iK) {
                z13 = true;
            }
            if (z14 && z13) {
                if (z10 && z11) {
                    if (iG >= iK && iD <= i12) {
                        return staggeredGridLayoutManager.getPosition(view);
                    }
                } else {
                    if (z11) {
                        return staggeredGridLayoutManager.getPosition(view);
                    }
                    if (iG < iK || iD > i12) {
                        return staggeredGridLayoutManager.getPosition(view);
                    }
                }
            }
            i13 += i14;
        }
        return -1;
    }

    public final int f(int i10) {
        int i11 = this.f3911c;
        if (i11 != Integer.MIN_VALUE) {
            return i11;
        }
        if (this.f3909a.size() == 0) {
            return i10;
        }
        a();
        return this.f3911c;
    }

    public final View g(int i10, int i11) {
        StaggeredGridLayoutManager staggeredGridLayoutManager = this.f3914f;
        ArrayList arrayList = this.f3909a;
        View view = null;
        if (i11 != -1) {
            int size = arrayList.size() - 1;
            while (size >= 0) {
                View view2 = (View) arrayList.get(size);
                if ((staggeredGridLayoutManager.mReverseLayout && staggeredGridLayoutManager.getPosition(view2) >= i10) || ((!staggeredGridLayoutManager.mReverseLayout && staggeredGridLayoutManager.getPosition(view2) <= i10) || !view2.hasFocusable())) {
                    break;
                }
                size--;
                view = view2;
            }
        } else {
            int size2 = arrayList.size();
            int i12 = 0;
            while (i12 < size2) {
                View view3 = (View) arrayList.get(i12);
                if ((staggeredGridLayoutManager.mReverseLayout && staggeredGridLayoutManager.getPosition(view3) <= i10) || ((!staggeredGridLayoutManager.mReverseLayout && staggeredGridLayoutManager.getPosition(view3) >= i10) || !view3.hasFocusable())) {
                    break;
                }
                i12++;
                view = view3;
            }
        }
        return view;
    }

    public final int h(int i10) {
        int i11 = this.f3910b;
        if (i11 != Integer.MIN_VALUE) {
            return i11;
        }
        if (this.f3909a.size() == 0) {
            return i10;
        }
        View view = (View) this.f3909a.get(0);
        q2 q2Var = (q2) view.getLayoutParams();
        this.f3910b = this.f3914f.mPrimaryOrientation.g(view);
        q2Var.getClass();
        return this.f3910b;
    }
}
