package N1;

import android.view.View;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import r.AbstractC1481a;

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

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

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

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

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

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

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

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

    public B0(int i) {
        this.f4416a = 1;
        this.f4417b = i;
        if (i <= 0) {
            AbstractC1481a.c("maxSize <= 0");
            throw null;
        }
        this.f4421f = new O1.y(2);
        this.f4422g = new d3.E(13);
    }

    public void a() {
        View view = (View) ((ArrayList) this.f4421f).get(r0.size() - 1);
        y0 y0Var = (y0) view.getLayoutParams();
        this.f4418c = ((StaggeredGridLayoutManager) this.f4422g).H.d(view);
        y0Var.getClass();
    }

    public void b() {
        ((ArrayList) this.f4421f).clear();
        this.f4417b = Integer.MIN_VALUE;
        this.f4418c = Integer.MIN_VALUE;
        this.f4419d = 0;
    }

    public Object c(Object obj) {
        P6.g.e(obj, "key");
        return null;
    }

    public void d(Object obj, Object obj2, Object obj3) {
        P6.g.e(obj, "key");
        P6.g.e(obj2, "oldValue");
    }

    public int e() {
        return ((StaggeredGridLayoutManager) this.f4422g).f9096M ? g(r1.size() - 1, -1) : g(0, ((ArrayList) this.f4421f).size());
    }

    public int f() {
        return ((StaggeredGridLayoutManager) this.f4422g).f9096M ? g(0, ((ArrayList) this.f4421f).size()) : g(r1.size() - 1, -1);
    }

    public int g(int i, int i5) {
        StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) this.f4422g;
        int iM = staggeredGridLayoutManager.H.m();
        int i8 = staggeredGridLayoutManager.H.i();
        int i9 = i5 > i ? 1 : -1;
        while (i != i5) {
            View view = (View) ((ArrayList) this.f4421f).get(i);
            int iG = staggeredGridLayoutManager.H.g(view);
            int iD = staggeredGridLayoutManager.H.d(view);
            boolean z8 = iG <= i8;
            boolean z9 = iD >= iM;
            if (z8 && z9 && (iG < iM || iD > i8)) {
                return androidx.recyclerview.widget.a.T(view);
            }
            i += i9;
        }
        return -1;
    }

    public Object h(Object obj) {
        Object objPut;
        P6.g.e(obj, "key");
        synchronized (((d3.E) this.f4422g)) {
            O1.y yVar = (O1.y) this.f4421f;
            yVar.getClass();
            Object obj2 = yVar.f5089a.get(obj);
            if (obj2 != null) {
                this.f4419d++;
                return obj2;
            }
            this.f4420e++;
            Object objC = c(obj);
            if (objC == null) {
                return null;
            }
            synchronized (((d3.E) this.f4422g)) {
                O1.y yVar2 = (O1.y) this.f4421f;
                yVar2.getClass();
                objPut = yVar2.f5089a.put(obj, objC);
                if (objPut != null) {
                    O1.y yVar3 = (O1.y) this.f4421f;
                    yVar3.getClass();
                    yVar3.f5089a.put(obj, objPut);
                } else {
                    this.f4418c += o(obj, objC);
                }
            }
            if (objPut != null) {
                d(obj, objC, objPut);
                return objPut;
            }
            r(this.f4417b);
            return objC;
        }
    }

    public int i(int i) {
        int i5 = this.f4418c;
        if (i5 != Integer.MIN_VALUE) {
            return i5;
        }
        if (((ArrayList) this.f4421f).size() == 0) {
            return i;
        }
        a();
        return this.f4418c;
    }

    public View j(int i, int i5) {
        StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) this.f4422g;
        ArrayList arrayList = (ArrayList) this.f4421f;
        View view = null;
        if (i5 != -1) {
            int size = arrayList.size() - 1;
            while (size >= 0) {
                View view2 = (View) arrayList.get(size);
                if ((staggeredGridLayoutManager.f9096M && androidx.recyclerview.widget.a.T(view2) >= i) || ((!staggeredGridLayoutManager.f9096M && androidx.recyclerview.widget.a.T(view2) <= i) || !view2.hasFocusable())) {
                    break;
                }
                size--;
                view = view2;
            }
            return view;
        }
        int size2 = arrayList.size();
        int i8 = 0;
        while (i8 < size2) {
            View view3 = (View) arrayList.get(i8);
            if ((staggeredGridLayoutManager.f9096M && androidx.recyclerview.widget.a.T(view3) <= i) || ((!staggeredGridLayoutManager.f9096M && androidx.recyclerview.widget.a.T(view3) >= i) || !view3.hasFocusable())) {
                break;
            }
            i8++;
            view = view3;
        }
        return view;
    }

    public int k(int i) {
        int i5 = this.f4417b;
        if (i5 != Integer.MIN_VALUE) {
            return i5;
        }
        if (((ArrayList) this.f4421f).size() == 0) {
            return i;
        }
        View view = (View) ((ArrayList) this.f4421f).get(0);
        y0 y0Var = (y0) view.getLayoutParams();
        this.f4417b = ((StaggeredGridLayoutManager) this.f4422g).H.g(view);
        y0Var.getClass();
        return this.f4417b;
    }

    public int l() {
        int i;
        synchronized (((d3.E) this.f4422g)) {
            i = this.f4417b;
        }
        return i;
    }

    public Object m(Object obj, Object obj2) {
        Object objPut;
        P6.g.e(obj, "key");
        P6.g.e(obj2, "value");
        synchronized (((d3.E) this.f4422g)) {
            this.f4418c += o(obj, obj2);
            O1.y yVar = (O1.y) this.f4421f;
            yVar.getClass();
            objPut = yVar.f5089a.put(obj, obj2);
            if (objPut != null) {
                this.f4418c -= o(obj, objPut);
            }
        }
        if (objPut != null) {
            d(obj, objPut, obj2);
        }
        r(this.f4417b);
        return objPut;
    }

    public Object n(Object obj) {
        Object objRemove;
        P6.g.e(obj, "key");
        synchronized (((d3.E) this.f4422g)) {
            O1.y yVar = (O1.y) this.f4421f;
            yVar.getClass();
            objRemove = yVar.f5089a.remove(obj);
            if (objRemove != null) {
                this.f4418c -= o(obj, objRemove);
            }
        }
        if (objRemove != null) {
            d(obj, objRemove, null);
        }
        return objRemove;
    }

    public int o(Object obj, Object obj2) {
        int iP = p(obj, obj2);
        if (iP >= 0) {
            return iP;
        }
        String str = "Negative size: " + obj + '=' + obj2;
        P6.g.e(str, "message");
        throw new IllegalStateException(str);
    }

    public int p(Object obj, Object obj2) {
        P6.g.e(obj, "key");
        P6.g.e(obj2, "value");
        return 1;
    }

    public LinkedHashMap q() {
        LinkedHashMap linkedHashMap;
        synchronized (((d3.E) this.f4422g)) {
            Set setEntrySet = ((O1.y) this.f4421f).f5089a.entrySet();
            P6.g.d(setEntrySet, "map.entries");
            linkedHashMap = new LinkedHashMap(setEntrySet.size());
            Set<Map.Entry> setEntrySet2 = ((O1.y) this.f4421f).f5089a.entrySet();
            P6.g.d(setEntrySet2, "map.entries");
            for (Map.Entry entry : setEntrySet2) {
                linkedHashMap.put(entry.getKey(), entry.getValue());
            }
        }
        return linkedHashMap;
    }

    /* JADX WARN: Code restructure failed: missing block: B:39:0x0095, code lost:
    
        return;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void r(int i) {
        Object next;
        Object key;
        Object value;
        while (true) {
            synchronized (((d3.E) this.f4422g)) {
                try {
                    if (!(this.f4418c >= 0 && (!((O1.y) this.f4421f).f5089a.isEmpty() || this.f4418c == 0))) {
                        throw new IllegalStateException("LruCache.sizeOf() is reporting inconsistent results!");
                    }
                    if (this.f4418c <= i || ((O1.y) this.f4421f).f5089a.isEmpty()) {
                        break;
                    }
                    Set setEntrySet = ((O1.y) this.f4421f).f5089a.entrySet();
                    P6.g.d(setEntrySet, "map.entries");
                    if (setEntrySet instanceof List) {
                        List list = (List) setEntrySet;
                        next = list.isEmpty() ? null : list.get(0);
                    } else {
                        Iterator it = setEntrySet.iterator();
                        if (it.hasNext()) {
                            next = it.next();
                        }
                    }
                    Map.Entry entry = (Map.Entry) next;
                    if (entry == null) {
                        return;
                    }
                    key = entry.getKey();
                    value = entry.getValue();
                    O1.y yVar = (O1.y) this.f4421f;
                    yVar.getClass();
                    P6.g.e(key, "key");
                    yVar.f5089a.remove(key);
                    this.f4418c -= o(key, value);
                } catch (Throwable th) {
                    throw th;
                }
            }
            d(key, value, null);
        }
    }

    public String toString() {
        String str;
        switch (this.f4416a) {
            case 1:
                synchronized (((d3.E) this.f4422g)) {
                    try {
                        int i = this.f4419d;
                        int i5 = this.f4420e + i;
                        str = "LruCache[maxSize=" + this.f4417b + ",hits=" + this.f4419d + ",misses=" + this.f4420e + ",hitRate=" + (i5 != 0 ? (i * 100) / i5 : 0) + "%]";
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                return str;
            default:
                return super.toString();
        }
    }

    public B0(StaggeredGridLayoutManager staggeredGridLayoutManager, int i) {
        this.f4416a = 0;
        this.f4422g = staggeredGridLayoutManager;
        this.f4421f = new ArrayList();
        this.f4417b = Integer.MIN_VALUE;
        this.f4418c = Integer.MIN_VALUE;
        this.f4419d = 0;
        this.f4420e = i;
    }
}
