package androidx.recyclerview.widget;

import android.util.Log;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.j;
import defpackage.kf7;
import defpackage.vw2;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public final class i extends RecyclerView.g<RecyclerView.ag> {
    public final j a;

    public static final class a {

        /* JADX INFO: renamed from: androidx.recyclerview.widget.i$a$a, reason: collision with other inner class name */
        public static final class C0063a {
        }

        /* JADX WARN: Failed to restore enum class, 'enum' modifier and super class removed */
        /* JADX WARN: Unknown enum class pattern. Please report as an issue! */
        public static final class b {
            public static final b a;
            public static final /* synthetic */ b[] b;

            static {
                b bVar = new b("NO_STABLE_IDS", 0);
                a = bVar;
                b = new b[]{bVar, new b("ISOLATED_STABLE_IDS", 1), new b("SHARED_STABLE_IDS", 2)};
            }

            public static b valueOf(String str) {
                return (b) Enum.valueOf(b.class, str);
            }

            public static b[] values() {
                return (b[]) b.clone();
            }
        }
    }

    public i(RecyclerView.g... gVarArr) {
        ArrayList arrayList;
        int size;
        List listAsList = Arrays.asList(gVarArr);
        this.a = new j(this);
        Iterator it = listAsList.iterator();
        while (true) {
            boolean zHasNext = it.hasNext();
            a.b bVar = a.b.a;
            int i = 0;
            if (!zHasNext) {
                super.setHasStableIds(this.a.g != bVar);
                return;
            }
            RecyclerView.g gVar = (RecyclerView.g) it.next();
            j jVar = this.a;
            arrayList = jVar.e;
            size = arrayList.size();
            if (size < 0 || size > arrayList.size()) {
                break;
            }
            if (jVar.g != bVar) {
                kf7.a("All sub adapters must have stable ids when stable id mode is ISOLATED_STABLE_IDS or SHARED_STABLE_IDS", gVar.hasStableIds());
            } else if (gVar.hasStableIds()) {
                Log.w("ConcatAdapter", "Stable ids in the adapter will be ignored as the ConcatAdapter is configured not to have stable ids");
            }
            int size2 = arrayList.size();
            while (true) {
                if (i >= size2) {
                    i = -1;
                    break;
                } else if (((ag) arrayList.get(i)).c == gVar) {
                    break;
                } else {
                    i++;
                }
            }
            if ((i == -1 ? null : (ag) arrayList.get(i)) == null) {
                ag agVar = new ag(gVar, jVar, jVar.b, jVar.h.a());
                arrayList.add(size, agVar);
                Iterator it2 = jVar.c.iterator();
                while (it2.hasNext()) {
                    RecyclerView recyclerView = (RecyclerView) ((WeakReference) it2.next()).get();
                    if (recyclerView != null) {
                        gVar.onAttachedToRecyclerView(recyclerView);
                    }
                }
                if (agVar.e > 0) {
                    jVar.a.notifyItemRangeInserted(jVar.h(agVar), agVar.e);
                }
                jVar.g();
            }
        }
        throw new IndexOutOfBoundsException("Index must be between 0 and " + arrayList.size() + ". Given:" + size);
    }

    public final void d(RecyclerView.g.a aVar) {
        super.setStateRestorationPolicy(aVar);
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final int findRelativeAdapterPositionIn(RecyclerView.g gVar, RecyclerView.ag agVar, int i) {
        j jVar = this.a;
        ag agVar2 = (ag) jVar.d.get(agVar);
        if (agVar2 == null) {
            return -1;
        }
        RecyclerView.g gVar2 = agVar2.c;
        int iH = i - jVar.h(agVar2);
        int itemCount = gVar2.getItemCount();
        if (iH >= 0 && iH < itemCount) {
            return gVar2.findRelativeAdapterPositionIn(gVar, agVar, iH);
        }
        StringBuilder sbR = vw2.r("Detected inconsistent adapter updates. The local position of the view holder maps to ", iH, " which is out of bounds for the adapter with size ", ".Make sure to immediately call notify methods in your adapter when you change the backing dataviewHolder:", itemCount);
        sbR.append(agVar);
        sbR.append("adapter:");
        sbR.append(gVar);
        throw new IllegalStateException(sbR.toString());
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final int getItemCount() {
        Iterator it = this.a.e.iterator();
        int i = 0;
        while (it.hasNext()) {
            i += ((ag) it.next()).e;
        }
        return i;
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final long getItemId(int i) {
        j jVar = this.a;
        j.a aVarI = jVar.i(i);
        ag agVar = aVarI.a;
        long jA = agVar.b.a(agVar.c.getItemId(aVarI.b));
        aVarI.c = false;
        aVarI.a = null;
        aVarI.b = -1;
        jVar.f = aVarI;
        return jA;
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final int getItemViewType(int i) {
        j jVar = this.a;
        j.a aVarI = jVar.i(i);
        ag agVar = aVarI.a;
        int iB = agVar.a.b(agVar.c.getItemViewType(aVarI.b));
        aVarI.c = false;
        aVarI.a = null;
        aVarI.b = -1;
        jVar.f = aVarI;
        return iB;
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final void onAttachedToRecyclerView(RecyclerView recyclerView) {
        j jVar = this.a;
        ArrayList arrayList = jVar.c;
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            if (((WeakReference) it.next()).get() == recyclerView) {
                return;
            }
        }
        arrayList.add(new WeakReference(recyclerView));
        Iterator it2 = jVar.e.iterator();
        while (it2.hasNext()) {
            ((ag) it2.next()).c.onAttachedToRecyclerView(recyclerView);
        }
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final void onBindViewHolder(RecyclerView.ag agVar, int i) {
        j jVar = this.a;
        j.a aVarI = jVar.i(i);
        jVar.d.put(agVar, aVarI.a);
        ag agVar2 = aVarI.a;
        agVar2.c.bindViewHolder(agVar, aVarI.b);
        aVarI.c = false;
        aVarI.a = null;
        aVarI.b = -1;
        jVar.f = aVarI;
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final RecyclerView.ag onCreateViewHolder(ViewGroup viewGroup, int i) {
        ag agVarA = this.a.b.a(i);
        return agVarA.c.onCreateViewHolder(viewGroup, agVarA.a.a(i));
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        j jVar = this.a;
        ArrayList arrayList = jVar.c;
        int size = arrayList.size() - 1;
        while (true) {
            if (size < 0) {
                break;
            }
            WeakReference weakReference = (WeakReference) arrayList.get(size);
            if (weakReference.get() == null) {
                arrayList.remove(size);
            } else if (weakReference.get() == recyclerView) {
                arrayList.remove(size);
                break;
            }
            size--;
        }
        Iterator it = jVar.e.iterator();
        while (it.hasNext()) {
            ((ag) it.next()).c.onDetachedFromRecyclerView(recyclerView);
        }
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final boolean onFailedToRecycleView(RecyclerView.ag agVar) {
        j jVar = this.a;
        IdentityHashMap identityHashMap = jVar.d;
        ag agVar2 = (ag) identityHashMap.get(agVar);
        if (agVar2 != null) {
            boolean zOnFailedToRecycleView = agVar2.c.onFailedToRecycleView(agVar);
            identityHashMap.remove(agVar);
            return zOnFailedToRecycleView;
        }
        throw new IllegalStateException("Cannot find wrapper for " + agVar + ", seems like it is not bound by this adapter: " + jVar);
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final void onViewAttachedToWindow(RecyclerView.ag agVar) {
        this.a.j(agVar).c.onViewAttachedToWindow(agVar);
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final void onViewDetachedFromWindow(RecyclerView.ag agVar) {
        this.a.j(agVar).c.onViewDetachedFromWindow(agVar);
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final void onViewRecycled(RecyclerView.ag agVar) {
        j jVar = this.a;
        IdentityHashMap identityHashMap = jVar.d;
        ag agVar2 = (ag) identityHashMap.get(agVar);
        if (agVar2 != null) {
            agVar2.c.onViewRecycled(agVar);
            identityHashMap.remove(agVar);
        } else {
            throw new IllegalStateException("Cannot find wrapper for " + agVar + ", seems like it is not bound by this adapter: " + jVar);
        }
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final void setHasStableIds(boolean z) {
        throw new UnsupportedOperationException("Calling setHasStableIds is not allowed on the ConcatAdapter. Use the Config object passed in the constructor to control this behavior");
    }

    @Override // androidx.recyclerview.widget.RecyclerView.g
    public final void setStateRestorationPolicy(RecyclerView.g.a aVar) {
        throw new UnsupportedOperationException("Calling setStateRestorationPolicy is not allowed on the ConcatAdapter. This value is inferred from added adapters");
    }
}
