package com.google.android.material.carousel;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
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.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.d2;
import androidx.recyclerview.widget.f2;
import androidx.recyclerview.widget.q1;
import androidx.recyclerview.widget.r1;
import androidx.recyclerview.widget.y1;
import com.google.android.gms.measurement.internal.u1;
import com.google.android.material.carousel.CarouselLayoutManager;
import e9.b;
import e9.c;
import e9.d;
import e9.e;
import e9.f;
import e9.g;
import e9.h;
import e9.j;
import e9.k;
import e9.l;
import e9.m;
import e9.n;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import kotlinx.coroutines.y;
import y8.a;

/* JADX INFO: loaded from: classes2.dex */
public class CarouselLayoutManager extends q1 implements b, d2 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int f22396a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f22397b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f22398c;

    @Nullable
    private k currentKeylineState;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final f f22399d;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int f22401f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public g f22402g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final c f22403h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public int f22404i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public int f22405j;

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

    @Nullable
    private l keylineStateList;

    @Nullable
    private Map<Integer, k> keylineStatePositionMap;

    /* JADX WARN: Type inference failed for: r2v0, types: [e9.c] */
    public CarouselLayoutManager() {
        n nVar = new n();
        this.f22399d = new f();
        final int i10 = 0;
        this.f22401f = 0;
        this.f22403h = new View.OnLayoutChangeListener(this) { // from class: e9.c

            /* JADX INFO: renamed from: b, reason: collision with root package name */
            public final /* synthetic */ CarouselLayoutManager f25582b;

            {
                this.f25582b = this;
            }

            @Override // android.view.View.OnLayoutChangeListener
            public final void onLayoutChange(View view, int i11, int i12, int i13, int i14, int i15, int i16, int i17, int i18) {
                int i19 = i10;
                int i20 = 11;
                CarouselLayoutManager carouselLayoutManager = this.f25582b;
                switch (i19) {
                    case 0:
                        carouselLayoutManager.getClass();
                        if (i13 - i11 != i17 - i15 || i14 - i12 != i18 - i16) {
                            view.post(new y.a(carouselLayoutManager, i20));
                        }
                        break;
                    default:
                        carouselLayoutManager.getClass();
                        if (i13 - i11 != i17 - i15 || i14 - i12 != i18 - i16) {
                            view.post(new y.a(carouselLayoutManager, i20));
                        }
                        break;
                }
            }
        };
        this.f22405j = -1;
        this.f22406k = 0;
        this.f22400e = nVar;
        H();
        setOrientation(0);
    }

    public static fe.h B(float f10, List list, boolean z10) {
        float f11 = Float.MAX_VALUE;
        int i10 = -1;
        int i11 = -1;
        int i12 = -1;
        int i13 = -1;
        float f12 = -3.4028235E38f;
        float f13 = Float.MAX_VALUE;
        float f14 = Float.MAX_VALUE;
        for (int i14 = 0; i14 < list.size(); i14++) {
            j jVar = (j) list.get(i14);
            float f15 = z10 ? jVar.f25604b : jVar.f25603a;
            float fAbs = Math.abs(f15 - f10);
            if (f15 <= f10 && fAbs <= f11) {
                i10 = i14;
                f11 = fAbs;
            }
            if (f15 > f10 && fAbs <= f13) {
                i12 = i14;
                f13 = fAbs;
            }
            if (f15 <= f14) {
                i11 = i14;
                f14 = f15;
            }
            if (f15 > f12) {
                i13 = i14;
                f12 = f15;
            }
        }
        if (i10 == -1) {
            i10 = i11;
        }
        if (i12 == -1) {
            i12 = i13;
        }
        return new fe.h((j) list.get(i10), (j) list.get(i12));
    }

    public static float y(float f10, fe.h hVar) {
        j jVar = (j) hVar.f26080b;
        float f11 = jVar.f25606d;
        j jVar2 = (j) hVar.f26081c;
        return a.b(f11, jVar2.f25606d, jVar.f25604b, jVar2.f25604b, f10);
    }

    public final int A(int i10, k kVar) {
        int i11 = Integer.MAX_VALUE;
        for (j jVar : kVar.f25613c.subList(kVar.f25614d, kVar.f25615e + 1)) {
            float f10 = kVar.f25611a;
            float f11 = (f10 / 2.0f) + (i10 * f10);
            int iW = (D() ? (int) ((w() - jVar.f25603a) - f11) : (int) (f11 - jVar.f25603a)) - this.f22396a;
            if (Math.abs(i11) > Math.abs(iW)) {
                i11 = iW;
            }
        }
        return i11;
    }

    public final boolean C() {
        return this.f22402g.f32097b == 0;
    }

    public final boolean D() {
        return C() && getLayoutDirection() == 1;
    }

    public final boolean E(float f10, fe.h hVar) {
        float fY = y(f10, hVar) / 2.0f;
        float f11 = D() ? f10 + fY : f10 - fY;
        if (D()) {
            if (f11 >= 0.0f) {
                return false;
            }
        } else if (f11 <= w()) {
            return false;
        }
        return true;
    }

    public final boolean F(float f10, fe.h hVar) {
        float fP = p(f10, y(f10, hVar) / 2.0f);
        if (D()) {
            if (fP <= w()) {
                return false;
            }
        } else if (fP >= 0.0f) {
            return false;
        }
        return true;
    }

    /*  JADX ERROR: NullPointerException in pass: ConstructorVisitor
        java.lang.NullPointerException: Cannot invoke "jadx.core.dex.instructions.args.RegisterArg.sameRegAndSVar(jadx.core.dex.instructions.args.InsnArg)" because "resultArg" is null
        	at jadx.core.dex.visitors.MoveInlineVisitor.processMove(MoveInlineVisitor.java:52)
        	at jadx.core.dex.visitors.MoveInlineVisitor.moveInline(MoveInlineVisitor.java:41)
        	at jadx.core.dex.visitors.ConstructorVisitor.visit(ConstructorVisitor.java:43)
        */
    public final void G(
    /*  JADX ERROR: Method generation error
        jadx.core.utils.exceptions.JadxRuntimeException: Code variable not set in r31v0 ??
        	at jadx.core.dex.instructions.args.SSAVar.getCodeVar(SSAVar.java:236)
        	at jadx.core.codegen.MethodGen.addMethodArguments(MethodGen.java:224)
        	at jadx.core.codegen.MethodGen.addDefinition(MethodGen.java:169)
        	at jadx.core.codegen.ClassGen.addMethodCode(ClassGen.java:407)
        	at jadx.core.codegen.ClassGen.addMethod(ClassGen.java:337)
        	at jadx.core.codegen.ClassGen.lambda$addInnerClsAndMethods$3(ClassGen.java:303)
        	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
        	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
        	at java.base/java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:395)
        	at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:261)
        */
    /*  JADX ERROR: NullPointerException in pass: ConstructorVisitor
        java.lang.NullPointerException: Cannot invoke "jadx.core.dex.instructions.args.RegisterArg.sameRegAndSVar(jadx.core.dex.instructions.args.InsnArg)" because "resultArg" is null
        	at jadx.core.dex.visitors.MoveInlineVisitor.processMove(MoveInlineVisitor.java:52)
        	at jadx.core.dex.visitors.MoveInlineVisitor.moveInline(MoveInlineVisitor.java:41)
        */

    public final void H() {
        this.keylineStateList = null;
        requestLayout();
    }

    public final int I(int i10, y1 y1Var, f2 f2Var) {
        if (getChildCount() == 0 || i10 == 0) {
            return 0;
        }
        if (this.keylineStateList == null) {
            G(y1Var);
        }
        int itemCount = getItemCount();
        l lVar = this.keylineStateList;
        if (itemCount <= (D() ? lVar.a() : lVar.c()).f25612b) {
            return 0;
        }
        int i11 = this.f22396a;
        int i12 = this.f22397b;
        int i13 = this.f22398c;
        int i14 = i11 + i10;
        if (i14 < i12) {
            i10 = i12 - i11;
        } else if (i14 > i13) {
            i10 = i13 - i11;
        }
        this.f22396a = i11 + i10;
        K(this.keylineStateList);
        float f10 = this.currentKeylineState.f25611a / 2.0f;
        float fU = u(getPosition(getChildAt(0)));
        Rect rect = new Rect();
        float f11 = D() ? this.currentKeylineState.c().f25604b : this.currentKeylineState.a().f25604b;
        float f12 = Float.MAX_VALUE;
        for (int i15 = 0; i15 < getChildCount(); i15++) {
            View childAt = getChildAt(i15);
            float fP = p(fU, f10);
            fe.h hVarB = B(fP, this.currentKeylineState.f25613c, false);
            float fT = t(fP, hVarB);
            super.getDecoratedBoundsWithMargins(childAt, rect);
            J(childAt, fP, hVarB);
            this.f22402g.p(f10, fT, rect, childAt);
            float fAbs = Math.abs(f11 - fT);
            if (fAbs < f12) {
                this.f22405j = getPosition(childAt);
                f12 = fAbs;
            }
            fU = p(fU, this.currentKeylineState.f25611a);
        }
        v(y1Var, f2Var);
        return i10;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final void J(View view, float f10, fe.h hVar) {
        int paddingLeft;
        if (view instanceof m) {
            j jVar = (j) hVar.f26080b;
            float f11 = jVar.f25605c;
            j jVar2 = (j) hVar.f26081c;
            float fB = a.b(f11, jVar2.f25605c, jVar.f25603a, jVar2.f25603a, f10);
            float height = view.getHeight();
            float width = view.getWidth();
            RectF rectFJ = this.f22402g.j(height, width, a.b(0.0f, height / 2.0f, 0.0f, 1.0f, fB), a.b(0.0f, width / 2.0f, 0.0f, 1.0f, fB));
            float fT = t(f10, hVar);
            RectF rectF = new RectF(fT - (rectFJ.width() / 2.0f), fT - (rectFJ.height() / 2.0f), (rectFJ.width() / 2.0f) + fT, (rectFJ.height() / 2.0f) + fT);
            g gVar = this.f22402g;
            int paddingTop = 0;
            switch (gVar.f25590c) {
                case 0:
                    paddingLeft = gVar.f25591d.getPaddingLeft();
                    break;
                default:
                    paddingLeft = 0;
                    break;
            }
            float f12 = paddingLeft;
            g gVar2 = this.f22402g;
            switch (gVar2.f25590c) {
                case 0:
                    break;
                default:
                    paddingTop = gVar2.f25591d.getPaddingTop();
                    break;
            }
            RectF rectF2 = new RectF(f12, paddingTop, this.f22402g.l(), this.f22402g.k());
            this.f22400e.getClass();
            this.f22402g.h(rectFJ, rectF, rectF2);
            this.f22402g.o(rectFJ, rectF, rectF2);
            ((m) view).setMaskRectF(rectFJ);
        }
    }

    public final void K(l lVar) {
        int i10 = this.f22398c;
        int i11 = this.f22397b;
        if (i10 <= i11) {
            this.currentKeylineState = D() ? lVar.a() : lVar.c();
        } else {
            this.currentKeylineState = lVar.b(this.f22396a, i11, i10);
        }
        List list = this.currentKeylineState.f25613c;
        f fVar = this.f22399d;
        fVar.getClass();
        fVar.f25589b = Collections.unmodifiableList(list);
    }

    public final void L() {
        int itemCount = getItemCount();
        int i10 = this.f22404i;
        if (itemCount == i10 || this.keylineStateList == null) {
            return;
        }
        n nVar = (n) this.f22400e;
        if ((i10 < nVar.f25626c && getItemCount() >= nVar.f25626c) || (i10 >= nVar.f25626c && getItemCount() < nVar.f25626c)) {
            H();
        }
        this.f22404i = itemCount;
    }

    @Override // androidx.recyclerview.widget.q1
    public final boolean canScrollHorizontally() {
        return C();
    }

    @Override // androidx.recyclerview.widget.q1
    public final boolean canScrollVertically() {
        return !C();
    }

    @Override // androidx.recyclerview.widget.q1
    public final int computeHorizontalScrollExtent(f2 f2Var) {
        if (getChildCount() == 0 || this.keylineStateList == null || getItemCount() <= 1) {
            return 0;
        }
        return (int) (getWidth() * (this.keylineStateList.f25617a.f25611a / computeHorizontalScrollRange(f2Var)));
    }

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

    @Override // androidx.recyclerview.widget.q1
    public final int computeHorizontalScrollRange(f2 f2Var) {
        return this.f22398c - this.f22397b;
    }

    @Override // androidx.recyclerview.widget.d2
    @Nullable
    public PointF computeScrollVectorForPosition(int i10) {
        if (this.keylineStateList == null) {
            return null;
        }
        int iZ = z(i10, x(i10)) - this.f22396a;
        return C() ? new PointF(iZ, 0.0f) : new PointF(0.0f, iZ);
    }

    @Override // androidx.recyclerview.widget.q1
    public final int computeVerticalScrollExtent(f2 f2Var) {
        if (getChildCount() == 0 || this.keylineStateList == null || getItemCount() <= 1) {
            return 0;
        }
        return (int) (getHeight() * (this.keylineStateList.f25617a.f25611a / computeVerticalScrollRange(f2Var)));
    }

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

    @Override // androidx.recyclerview.widget.q1
    public final int computeVerticalScrollRange(f2 f2Var) {
        return this.f22398c - this.f22397b;
    }

    @Override // androidx.recyclerview.widget.q1
    public final r1 generateDefaultLayoutParams() {
        return new r1(-2, -2);
    }

    @Override // androidx.recyclerview.widget.q1
    public final void getDecoratedBoundsWithMargins(View view, Rect rect) {
        super.getDecoratedBoundsWithMargins(view, rect);
        float fCenterY = rect.centerY();
        if (C()) {
            fCenterY = rect.centerX();
        }
        float fY = y(fCenterY, B(fCenterY, this.currentKeylineState.f25613c, true));
        float fWidth = C() ? (rect.width() - fY) / 2.0f : 0.0f;
        float fHeight = C() ? 0.0f : (rect.height() - fY) / 2.0f;
        rect.set((int) (rect.left + fWidth), (int) (rect.top + fHeight), (int) (rect.right - fWidth), (int) (rect.bottom - fHeight));
    }

    @Override // androidx.recyclerview.widget.q1
    public final boolean isAutoMeasureEnabled() {
        return true;
    }

    @Override // androidx.recyclerview.widget.q1
    public final void measureChildWithMargins(View view, int i10, int i11) {
        if (!(view instanceof m)) {
            throw new IllegalStateException("All children of a RecyclerView using CarouselLayoutManager must use MaskableFrameLayout as their root ViewGroup.");
        }
        r1 r1Var = (r1) view.getLayoutParams();
        Rect rect = new Rect();
        calculateItemDecorationsForChild(view, rect);
        int i12 = rect.left + rect.right + i10;
        int i13 = rect.top + rect.bottom + i11;
        l lVar = this.keylineStateList;
        view.measure(q1.getChildMeasureSpec(getWidth(), getWidthMode(), getPaddingRight() + getPaddingLeft() + ((ViewGroup.MarginLayoutParams) r1Var).leftMargin + ((ViewGroup.MarginLayoutParams) r1Var).rightMargin + i12, (int) ((lVar == null || this.f22402g.f32097b != 0) ? ((ViewGroup.MarginLayoutParams) r1Var).width : lVar.f25617a.f25611a), C()), q1.getChildMeasureSpec(getHeight(), getHeightMode(), getPaddingBottom() + getPaddingTop() + ((ViewGroup.MarginLayoutParams) r1Var).topMargin + ((ViewGroup.MarginLayoutParams) r1Var).bottomMargin + i13, (int) ((lVar == null || this.f22402g.f32097b != 1) ? ((ViewGroup.MarginLayoutParams) r1Var).height : lVar.f25617a.f25611a), canScrollVertically()));
    }

    public final void o(View view, int i10, e eVar) {
        float f10 = this.currentKeylineState.f25611a / 2.0f;
        addView(view, i10);
        measureChildWithMargins(view, 0, 0);
        float f11 = eVar.f25586c;
        this.f22402g.n(view, (int) (f11 - f10), (int) (f11 + f10));
        J(view, eVar.f25585b, eVar.f25587d);
    }

    @Override // androidx.recyclerview.widget.q1
    public final void onAttachedToWindow(RecyclerView recyclerView) {
        super.onAttachedToWindow(recyclerView);
        h hVar = this.f22400e;
        Context context = recyclerView.getContext();
        float dimension = hVar.f25592a;
        if (dimension <= 0.0f) {
            dimension = context.getResources().getDimension(x8.e.m3_carousel_small_item_size_min);
        }
        hVar.f25592a = dimension;
        float dimension2 = hVar.f25593b;
        if (dimension2 <= 0.0f) {
            dimension2 = context.getResources().getDimension(x8.e.m3_carousel_small_item_size_max);
        }
        hVar.f25593b = dimension2;
        H();
        recyclerView.addOnLayoutChangeListener(this.f22403h);
    }

    @Override // androidx.recyclerview.widget.q1
    public final void onDetachedFromWindow(RecyclerView recyclerView, y1 y1Var) {
        onDetachedFromWindow(recyclerView);
        recyclerView.removeOnLayoutChangeListener(this.f22403h);
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0025  */
    /* JADX WARN: Removed duplicated region for block: B:29:0x003e  */
    /* JADX WARN: Removed duplicated region for block: B:30:0x0040  */
    @Override // androidx.recyclerview.widget.q1
    @Nullable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public View onFocusSearchFailed(@NonNull View view, int i10, @NonNull y1 y1Var, @NonNull f2 f2Var) {
        byte b10;
        if (getChildCount() == 0) {
            return null;
        }
        int i11 = this.f22402g.f32097b;
        if (i10 == 1) {
            b10 = -1;
        } else if (i10 == 2) {
            b10 = 1;
        } else if (i10 == 17) {
            if (i11 == 0) {
                if (D()) {
                }
            }
            b10 = -2147483648;
        } else if (i10 != 33) {
            if (i10 == 66) {
                if (i11 == 0) {
                    if (D()) {
                    }
                }
                b10 = -2147483648;
            } else if (i10 != 130 || i11 != 1) {
                b10 = -2147483648;
            }
        } else if (i11 == 1) {
        }
        if (b10 == -2147483648) {
            return null;
        }
        if (b10 == -1) {
            if (getPosition(view) == 0) {
                return null;
            }
            q(getPosition(getChildAt(0)) - 1, y1Var, 0);
            return getChildAt(D() ? getChildCount() - 1 : 0);
        }
        if (getPosition(view) == getItemCount() - 1) {
            return null;
        }
        q(getPosition(getChildAt(getChildCount() - 1)) + 1, y1Var, -1);
        return getChildAt(D() ? 0 : getChildCount() - 1);
    }

    @Override // androidx.recyclerview.widget.q1
    public final void onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        super.onInitializeAccessibilityEvent(accessibilityEvent);
        if (getChildCount() > 0) {
            accessibilityEvent.setFromIndex(getPosition(getChildAt(0)));
            accessibilityEvent.setToIndex(getPosition(getChildAt(getChildCount() - 1)));
        }
    }

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

    @Override // androidx.recyclerview.widget.q1
    public final void onItemsChanged(RecyclerView recyclerView) {
        super.onItemsChanged(recyclerView);
        L();
    }

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

    @Override // androidx.recyclerview.widget.q1
    public final void onLayoutChildren(y1 y1Var, f2 f2Var) {
        if (f2Var.b() <= 0 || w() <= 0.0f) {
            removeAndRecycleAllViews(y1Var);
            this.f22401f = 0;
            return;
        }
        boolean zD = D();
        l lVar = this.keylineStateList;
        boolean z10 = lVar == null;
        if (z10 || lVar.f25617a.f25616f != w()) {
            G(y1Var);
        }
        l lVar2 = this.keylineStateList;
        boolean zD2 = D();
        k kVarA = zD2 ? lVar2.a() : lVar2.c();
        float f10 = (zD2 ? kVarA.c() : kVarA.a()).f25603a;
        float f11 = kVarA.f25611a / 2.0f;
        int iM = (int) (this.f22402g.m() - (D() ? f10 + f11 : f10 - f11));
        l lVar3 = this.keylineStateList;
        boolean zD3 = D();
        k kVarC = zD3 ? lVar3.c() : lVar3.a();
        j jVarA = zD3 ? kVarC.a() : kVarC.c();
        int iB = (int) ((((zD3 ? -1 : 1) * jVarA.f25606d) / 2.0f) + ((((f2Var.b() - 1) * kVarC.f25611a) * (zD3 ? -1.0f : 1.0f)) - (jVarA.f25603a - this.f22402g.m())));
        int iMin = zD3 ? Math.min(0, iB) : Math.max(0, iB);
        this.f22397b = zD ? iMin : iM;
        if (zD) {
            iMin = iM;
        }
        this.f22398c = iMin;
        if (z10) {
            this.f22396a = iM;
            l lVar4 = this.keylineStateList;
            int itemCount = getItemCount();
            int i10 = this.f22397b;
            int i11 = this.f22398c;
            boolean zD4 = D();
            float f12 = lVar4.f25617a.f25611a;
            HashMap map = new HashMap();
            int i12 = 0;
            for (int i13 = 0; i13 < itemCount; i13++) {
                int i14 = zD4 ? (itemCount - i13) - 1 : i13;
                float f13 = i14 * f12 * (zD4 ? -1 : 1);
                float f14 = i11 - lVar4.f25623g;
                List list = lVar4.f25619c;
                if (f13 > f14 || i13 >= itemCount - list.size()) {
                    map.put(Integer.valueOf(i14), (k) list.get(y.h(i12, 0, list.size() - 1)));
                    i12++;
                }
            }
            int i15 = 0;
            for (int i16 = itemCount - 1; i16 >= 0; i16--) {
                int i17 = zD4 ? (itemCount - i16) - 1 : i16;
                float f15 = i17 * f12 * (zD4 ? -1 : 1);
                float f16 = i10 + lVar4.f25622f;
                List list2 = lVar4.f25618b;
                if (f15 < f16 || i16 < list2.size()) {
                    map.put(Integer.valueOf(i17), (k) list2.get(y.h(i15, 0, list2.size() - 1)));
                    i15++;
                }
            }
            this.keylineStatePositionMap = map;
            int i18 = this.f22405j;
            if (i18 != -1) {
                this.f22396a = z(i18, x(i18));
            }
        }
        int i19 = this.f22396a;
        int i20 = this.f22397b;
        int i21 = this.f22398c;
        this.f22396a = (i19 < i20 ? i20 - i19 : i19 > i21 ? i21 - i19 : 0) + i19;
        this.f22401f = y.h(this.f22401f, 0, f2Var.b());
        K(this.keylineStateList);
        detachAndScrapAttachedViews(y1Var);
        v(y1Var, f2Var);
        this.f22404i = getItemCount();
    }

    @Override // androidx.recyclerview.widget.q1
    public final void onLayoutCompleted(f2 f2Var) {
        super.onLayoutCompleted(f2Var);
        if (getChildCount() == 0) {
            this.f22401f = 0;
        } else {
            this.f22401f = getPosition(getChildAt(0));
        }
    }

    public final float p(float f10, float f11) {
        return D() ? f10 - f11 : f10 + f11;
    }

    public final void q(int i10, y1 y1Var, int i11) {
        if (i10 < 0 || i10 >= getItemCount()) {
            return;
        }
        float fU = u(i10);
        View viewD = y1Var.d(i10);
        measureChildWithMargins(viewD, 0, 0);
        float fP = p(fU, this.currentKeylineState.f25611a / 2.0f);
        fe.h hVarB = B(fP, this.currentKeylineState.f25613c, false);
        o(viewD, i11, new e(viewD, fP, t(fP, hVarB), hVarB));
    }

    public final void r(int i10, y1 y1Var, f2 f2Var) {
        float fU = u(i10);
        while (i10 < f2Var.b()) {
            float fP = p(fU, this.currentKeylineState.f25611a / 2.0f);
            fe.h hVarB = B(fP, this.currentKeylineState.f25613c, false);
            float fT = t(fP, hVarB);
            if (E(fT, hVarB)) {
                return;
            }
            fU = p(fU, this.currentKeylineState.f25611a);
            if (!F(fT, hVarB)) {
                View viewD = y1Var.d(i10);
                o(viewD, -1, new e(viewD, fP, fT, hVarB));
            }
            i10++;
        }
    }

    @Override // androidx.recyclerview.widget.q1
    public final boolean requestChildRectangleOnScreen(RecyclerView recyclerView, View view, Rect rect, boolean z10, boolean z11) {
        int iA;
        if (this.keylineStateList == null || (iA = A(getPosition(view), x(getPosition(view)))) == 0) {
            return false;
        }
        int i10 = this.f22396a;
        int i11 = this.f22397b;
        int i12 = this.f22398c;
        int i13 = i10 + iA;
        if (i13 < i11) {
            iA = i11 - i10;
        } else if (i13 > i12) {
            iA = i12 - i10;
        }
        int iA2 = A(getPosition(view), this.keylineStateList.b(i10 + iA, i11, i12));
        if (C()) {
            recyclerView.scrollBy(iA2, 0);
            return true;
        }
        recyclerView.scrollBy(0, iA2);
        return true;
    }

    public final void s(int i10, y1 y1Var) {
        float fU = u(i10);
        while (i10 >= 0) {
            float fP = p(fU, this.currentKeylineState.f25611a / 2.0f);
            fe.h hVarB = B(fP, this.currentKeylineState.f25613c, false);
            float fT = t(fP, hVarB);
            if (F(fT, hVarB)) {
                return;
            }
            float f10 = this.currentKeylineState.f25611a;
            fU = D() ? fU + f10 : fU - f10;
            if (!E(fT, hVarB)) {
                View viewD = y1Var.d(i10);
                o(viewD, 0, new e(viewD, fP, fT, hVarB));
            }
            i10--;
        }
    }

    @Override // androidx.recyclerview.widget.q1
    public final int scrollHorizontallyBy(int i10, y1 y1Var, f2 f2Var) {
        if (C()) {
            return I(i10, y1Var, f2Var);
        }
        return 0;
    }

    @Override // androidx.recyclerview.widget.q1
    public final void scrollToPosition(int i10) {
        this.f22405j = i10;
        if (this.keylineStateList == null) {
            return;
        }
        this.f22396a = z(i10, x(i10));
        this.f22401f = y.h(i10, 0, Math.max(0, getItemCount() - 1));
        K(this.keylineStateList);
        requestLayout();
    }

    @Override // androidx.recyclerview.widget.q1
    public final int scrollVerticallyBy(int i10, y1 y1Var, f2 f2Var) {
        if (canScrollVertically()) {
            return I(i10, y1Var, f2Var);
        }
        return 0;
    }

    public final void setOrientation(int i10) {
        g gVar;
        if (i10 != 0 && i10 != 1) {
            throw new IllegalArgumentException(u1.g("invalid orientation:", i10));
        }
        assertNotInLayoutOrScroll(null);
        g gVar2 = this.f22402g;
        if (gVar2 == null || i10 != gVar2.f32097b) {
            if (i10 == 0) {
                gVar = new g(0, this, 1);
            } else {
                if (i10 != 1) {
                    throw new IllegalArgumentException("invalid orientation");
                }
                gVar = new g(1, this, 0);
            }
            this.f22402g = gVar;
            H();
        }
    }

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

    public final float t(float f10, fe.h hVar) {
        Object obj = hVar.f26080b;
        float f11 = ((j) obj).f25604b;
        Object obj2 = hVar.f26081c;
        float fB = a.b(f11, ((j) obj2).f25604b, ((j) obj).f25603a, ((j) obj2).f25603a, f10);
        if (((j) hVar.f26081c) != this.currentKeylineState.b() && ((j) hVar.f26080b) != this.currentKeylineState.d()) {
            return fB;
        }
        Object obj3 = hVar.f26081c;
        return u1.a(1.0f, ((j) obj3).f25605c, f10 - ((j) obj3).f25603a, fB);
    }

    public final float u(int i10) {
        return p(this.f22402g.m() - this.f22396a, this.currentKeylineState.f25611a * i10);
    }

    public final void v(y1 y1Var, f2 f2Var) {
        while (getChildCount() > 0) {
            View childAt = getChildAt(0);
            Rect rect = new Rect();
            super.getDecoratedBoundsWithMargins(childAt, rect);
            float fCenterX = C() ? rect.centerX() : rect.centerY();
            if (!F(fCenterX, B(fCenterX, this.currentKeylineState.f25613c, true))) {
                break;
            } else {
                removeAndRecycleView(childAt, y1Var);
            }
        }
        while (getChildCount() - 1 >= 0) {
            View childAt2 = getChildAt(getChildCount() - 1);
            Rect rect2 = new Rect();
            super.getDecoratedBoundsWithMargins(childAt2, rect2);
            float fCenterX2 = C() ? rect2.centerX() : rect2.centerY();
            if (!E(fCenterX2, B(fCenterX2, this.currentKeylineState.f25613c, true))) {
                break;
            } else {
                removeAndRecycleView(childAt2, y1Var);
            }
        }
        if (getChildCount() == 0) {
            s(this.f22401f - 1, y1Var);
            r(this.f22401f, y1Var, f2Var);
        } else {
            int position = getPosition(getChildAt(0));
            int position2 = getPosition(getChildAt(getChildCount() - 1));
            s(position - 1, y1Var);
            r(position2 + 1, y1Var, f2Var);
        }
    }

    public final int w() {
        return C() ? getWidth() : getHeight();
    }

    public final k x(int i10) {
        k kVar;
        Map<Integer, k> map = this.keylineStatePositionMap;
        return (map == null || (kVar = map.get(Integer.valueOf(y.h(i10, 0, Math.max(0, getItemCount() + (-1)))))) == null) ? this.keylineStateList.f25617a : kVar;
    }

    public final int z(int i10, k kVar) {
        if (!D()) {
            return (int) ((kVar.f25611a / 2.0f) + ((i10 * kVar.f25611a) - kVar.a().f25603a));
        }
        float fW = w() - kVar.c().f25603a;
        float f10 = kVar.f25611a;
        return (int) ((fW - (i10 * f10)) - (f10 / 2.0f));
    }

    /* JADX WARN: Type inference failed for: r5v1, types: [e9.c] */
    @SuppressLint({"UnknownNullness"})
    public CarouselLayoutManager(Context context, AttributeSet attributeSet, int i10, int i11) {
        this.f22399d = new f();
        this.f22401f = 0;
        final int i12 = 1;
        this.f22403h = new View.OnLayoutChangeListener(this) { // from class: e9.c

            /* JADX INFO: renamed from: b, reason: collision with root package name */
            public final /* synthetic */ CarouselLayoutManager f25582b;

            {
                this.f25582b = this;
            }

            @Override // android.view.View.OnLayoutChangeListener
            public final void onLayoutChange(View view, int i112, int i122, int i13, int i14, int i15, int i16, int i17, int i18) {
                int i19 = i12;
                int i20 = 11;
                CarouselLayoutManager carouselLayoutManager = this.f25582b;
                switch (i19) {
                    case 0:
                        carouselLayoutManager.getClass();
                        if (i13 - i112 != i17 - i15 || i14 - i122 != i18 - i16) {
                            view.post(new y.a(carouselLayoutManager, i20));
                        }
                        break;
                    default:
                        carouselLayoutManager.getClass();
                        if (i13 - i112 != i17 - i15 || i14 - i122 != i18 - i16) {
                            view.post(new y.a(carouselLayoutManager, i20));
                        }
                        break;
                }
            }
        };
        this.f22405j = -1;
        this.f22406k = 0;
        this.f22400e = new n();
        H();
        if (attributeSet != null) {
            TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, x8.m.Carousel);
            this.f22406k = typedArrayObtainStyledAttributes.getInt(x8.m.Carousel_carousel_alignment, 0);
            H();
            setOrientation(typedArrayObtainStyledAttributes.getInt(e3.d.RecyclerView_android_orientation, 0));
            typedArrayObtainStyledAttributes.recycle();
        }
    }
}
