package androidx.recyclerview.widget;

import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import defpackage.vw2;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes.dex */
class h {
    public final an a;
    public View e;
    public int d = 0;
    public final a b = new a();
    public final ArrayList c = new ArrayList();

    public static class a {
        public long a = 0;
        public a b;

        public final void a(int i) {
            if (i < 64) {
                this.a &= ~(1 << i);
                return;
            }
            a aVar = this.b;
            if (aVar != null) {
                aVar.a(i - 64);
            }
        }

        public final int b(int i) {
            a aVar = this.b;
            if (aVar == null) {
                return i >= 64 ? Long.bitCount(this.a) : Long.bitCount(this.a & ((1 << i) - 1));
            }
            if (i < 64) {
                return Long.bitCount(this.a & ((1 << i) - 1));
            }
            return Long.bitCount(this.a) + aVar.b(i - 64);
        }

        public final void c() {
            if (this.b == null) {
                this.b = new a();
            }
        }

        public final boolean d(int i) {
            if (i < 64) {
                return (this.a & (1 << i)) != 0;
            }
            c();
            return this.b.d(i - 64);
        }

        public final void e(int i, boolean z) {
            if (i >= 64) {
                c();
                this.b.e(i - 64, z);
                return;
            }
            long j = this.a;
            boolean z2 = (Long.MIN_VALUE & j) != 0;
            long j2 = (1 << i) - 1;
            this.a = ((j & (~j2)) << 1) | (j & j2);
            if (z) {
                h(i);
            } else {
                a(i);
            }
            if (z2 || this.b != null) {
                c();
                this.b.e(0, z2);
            }
        }

        public final boolean f(int i) {
            if (i >= 64) {
                c();
                return this.b.f(i - 64);
            }
            long j = 1 << i;
            long j2 = this.a;
            boolean z = (j2 & j) != 0;
            long j3 = j2 & (~j);
            this.a = j3;
            long j4 = j - 1;
            this.a = (j3 & j4) | Long.rotateRight((~j4) & j3, 1);
            a aVar = this.b;
            if (aVar != null) {
                if (aVar.d(0)) {
                    h(63);
                }
                this.b.f(0);
            }
            return z;
        }

        public final void g() {
            this.a = 0L;
            a aVar = this.b;
            if (aVar != null) {
                aVar.g();
            }
        }

        public final void h(int i) {
            if (i < 64) {
                this.a |= 1 << i;
            } else {
                c();
                this.b.h(i - 64);
            }
        }

        public final String toString() {
            if (this.b == null) {
                return Long.toBinaryString(this.a);
            }
            return this.b.toString() + "xx" + Long.toBinaryString(this.a);
        }
    }

    public interface b {
    }

    public h(an anVar) {
        this.a = anVar;
    }

    public final void a(View view, int i, ViewGroup.LayoutParams layoutParams, boolean z) {
        an anVar = this.a;
        RecyclerView recyclerView = anVar.a;
        int childCount = i < 0 ? recyclerView.getChildCount() : d(i);
        this.b.e(childCount, z);
        if (z) {
            this.c.add(view);
            RecyclerView.ag childViewHolderInt = RecyclerView.getChildViewHolderInt(view);
            if (childViewHolderInt != null) {
                childViewHolderInt.onEnteredHiddenState(anVar.a);
            }
        }
        RecyclerView.ag childViewHolderInt2 = RecyclerView.getChildViewHolderInt(view);
        if (childViewHolderInt2 != null) {
            if (!childViewHolderInt2.isTmpDetached() && !childViewHolderInt2.shouldIgnore()) {
                StringBuilder sb = new StringBuilder("Called attach on a child which is not detached: ");
                sb.append(childViewHolderInt2);
                throw new IllegalArgumentException(vw2.j(recyclerView, sb));
            }
            if (RecyclerView.sVerboseLoggingEnabled) {
                childViewHolderInt2.toString();
            }
            childViewHolderInt2.clearTmpDetachFlag();
        } else if (RecyclerView.sDebugAssertionsEnabled) {
            StringBuilder sb2 = new StringBuilder("No ViewHolder found for child: ");
            sb2.append(view);
            sb2.append(", index: ");
            sb2.append(childCount);
            throw new IllegalArgumentException(vw2.j(recyclerView, sb2));
        }
        recyclerView.attachViewToParent(view, childCount, layoutParams);
    }

    public final View b(int i) {
        return this.a.a.getChildAt(d(i));
    }

    public final int c() {
        return this.a.a.getChildCount() - this.c.size();
    }

    public final int d(int i) {
        if (i < 0) {
            return -1;
        }
        int childCount = this.a.a.getChildCount();
        int i2 = i;
        while (i2 < childCount) {
            a aVar = this.b;
            int iB = i - (i2 - aVar.b(i2));
            if (iB == 0) {
                while (aVar.d(i2)) {
                    i2++;
                }
                return i2;
            }
            i2 += iB;
        }
        return -1;
    }

    public final int e(View view) {
        int iIndexOfChild = this.a.a.indexOfChild(view);
        if (iIndexOfChild != -1) {
            a aVar = this.b;
            if (!aVar.d(iIndexOfChild)) {
                return iIndexOfChild - aVar.b(iIndexOfChild);
            }
        }
        return -1;
    }

    public final void f(int i) {
        an anVar = this.a;
        int i2 = this.d;
        if (i2 == 1) {
            throw new IllegalStateException("Cannot call removeView(At) within removeView(At)");
        }
        if (i2 == 2) {
            throw new IllegalStateException("Cannot call removeView(At) within removeViewIfHidden");
        }
        try {
            int iD = d(i);
            View childAt = anVar.a.getChildAt(iD);
            if (childAt != null) {
                this.d = 1;
                this.e = childAt;
                if (this.b.f(iD)) {
                    g(childAt);
                }
                anVar.b(iD);
            }
            this.d = 0;
            this.e = null;
        } catch (Throwable th) {
            this.d = 0;
            this.e = null;
            throw th;
        }
    }

    public final void g(View view) {
        if (this.c.remove(view)) {
            this.a.a(view);
        }
    }

    public final String toString() {
        return this.b.toString() + ", hidden list:" + this.c.size();
    }
}
