package c5;

import android.graphics.Bitmap;
import android.os.Build;
import androidx.annotation.Nullable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Queue;
import java.util.TreeMap;

/* JADX INFO: loaded from: classes.dex */
public final class q implements m {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final Bitmap.Config[] f4901d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final Bitmap.Config[] f4902e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Bitmap.Config[] f4903f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final Bitmap.Config[] f4904g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final Bitmap.Config[] f4905h;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final i f4907b = new i();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final HashMap f4908c = new HashMap();

    static {
        Bitmap.Config[] configArr = {Bitmap.Config.ARGB_8888, null};
        if (Build.VERSION.SDK_INT >= 26) {
            configArr = (Bitmap.Config[]) Arrays.copyOf(configArr, 3);
            configArr[configArr.length - 1] = Bitmap.Config.RGBA_F16;
        }
        f4901d = configArr;
        f4902e = configArr;
        f4903f = new Bitmap.Config[]{Bitmap.Config.RGB_565};
        f4904g = new Bitmap.Config[]{Bitmap.Config.ARGB_4444};
        f4905h = new Bitmap.Config[]{Bitmap.Config.ALPHA_8};
    }

    public static String c(int i10, Bitmap.Config config) {
        return "[" + i10 + "](" + config + ")";
    }

    @Override // c5.m
    public final void a(Bitmap bitmap) {
        int iB = t5.p.b(bitmap);
        Bitmap.Config config = bitmap.getConfig();
        d dVar = this.f4906a;
        n nVarX = (n) ((Queue) dVar.f31277a).poll();
        if (nVarX == null) {
            nVarX = dVar.x();
        }
        p pVar = (p) nVarX;
        pVar.f4899b = iB;
        pVar.f4900c = config;
        this.f4907b.a(pVar, bitmap);
        NavigableMap navigableMapD = d(bitmap.getConfig());
        Integer num = (Integer) navigableMapD.get(Integer.valueOf(pVar.f4899b));
        navigableMapD.put(Integer.valueOf(pVar.f4899b), Integer.valueOf(num != null ? 1 + num.intValue() : 1));
    }

    public final void b(Integer num, Bitmap bitmap) {
        NavigableMap navigableMapD = d(bitmap.getConfig());
        Integer num2 = (Integer) navigableMapD.get(num);
        if (num2 != null) {
            if (num2.intValue() == 1) {
                navigableMapD.remove(num);
                return;
            } else {
                navigableMapD.put(num, Integer.valueOf(num2.intValue() - 1));
                return;
            }
        }
        throw new NullPointerException("Tried to decrement empty size, size: " + num + ", removed: " + c(t5.p.b(bitmap), bitmap.getConfig()) + ", this: " + this);
    }

    public final NavigableMap d(Bitmap.Config config) {
        HashMap map = this.f4908c;
        NavigableMap navigableMap = (NavigableMap) map.get(config);
        if (navigableMap != null) {
            return navigableMap;
        }
        TreeMap treeMap = new TreeMap();
        map.put(config, treeMap);
        return treeMap;
    }

    @Override // c5.m
    public final String f(int i10, int i11, Bitmap.Config config) {
        return c(t5.p.getBitmapByteSize(i10, i11, config), config);
    }

    @Override // c5.m
    @Nullable
    public Bitmap get(int i10, int i11, Bitmap.Config config) {
        Bitmap.Config[] configArr;
        int bitmapByteSize = t5.p.getBitmapByteSize(i10, i11, config);
        d dVar = this.f4906a;
        n nVarX = (n) ((Queue) dVar.f31277a).poll();
        if (nVarX == null) {
            nVarX = dVar.x();
        }
        p pVar = (p) nVarX;
        pVar.f4899b = bitmapByteSize;
        pVar.f4900c = config;
        int i12 = 0;
        if (Build.VERSION.SDK_INT < 26 || !Bitmap.Config.RGBA_F16.equals(config)) {
            int i13 = o.f4897a[config.ordinal()];
            configArr = i13 != 1 ? i13 != 2 ? i13 != 3 ? i13 != 4 ? new Bitmap.Config[]{config} : f4905h : f4904g : f4903f : f4901d;
        } else {
            configArr = f4902e;
        }
        int length = configArr.length;
        while (true) {
            if (i12 >= length) {
                break;
            }
            Bitmap.Config config2 = configArr[i12];
            Integer num = (Integer) d(config2).ceilingKey(Integer.valueOf(bitmapByteSize));
            if (num == null || num.intValue() > bitmapByteSize * 8) {
                i12++;
            } else if (num.intValue() != bitmapByteSize || (config2 != null ? !config2.equals(config) : config != null)) {
                dVar.n(pVar);
                int iIntValue = num.intValue();
                n nVarX2 = (n) ((Queue) dVar.f31277a).poll();
                if (nVarX2 == null) {
                    nVarX2 = dVar.x();
                }
                pVar = (p) nVarX2;
                pVar.f4899b = iIntValue;
                pVar.f4900c = config2;
            }
        }
        Bitmap bitmap = (Bitmap) this.f4907b.get(pVar);
        if (bitmap != null) {
            b(Integer.valueOf(pVar.f4899b), bitmap);
            bitmap.reconfigure(i10, i11, config);
        }
        return bitmap;
    }

    @Override // c5.m
    public final int h(Bitmap bitmap) {
        return t5.p.b(bitmap);
    }

    @Override // c5.m
    public final String j(Bitmap bitmap) {
        return c(t5.p.b(bitmap), bitmap.getConfig());
    }

    @Override // c5.m
    @Nullable
    public Bitmap removeLast() {
        Bitmap bitmap = (Bitmap) this.f4907b.removeLast();
        if (bitmap != null) {
            b(Integer.valueOf(t5.p.b(bitmap)), bitmap);
        }
        return bitmap;
    }

    public final String toString() {
        StringBuilder sbC = u.a.c("SizeConfigStrategy{groupedMap=");
        sbC.append(this.f4907b);
        sbC.append(", sortedSizes=(");
        HashMap map = this.f4908c;
        for (Map.Entry entry : map.entrySet()) {
            sbC.append(entry.getKey());
            sbC.append('[');
            sbC.append(entry.getValue());
            sbC.append("], ");
        }
        if (!map.isEmpty()) {
            sbC.replace(sbC.length() - 2, sbC.length(), "");
        }
        sbC.append(")}");
        return sbC.toString();
    }
}
