package mh;

import android.graphics.Bitmap;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;

/* loaded from: classes4.dex */
public class m implements k {

    /* renamed from: d, reason: collision with root package name */
    private static final Bitmap.Config[] f29142d;

    /* renamed from: e, reason: collision with root package name */
    private static final Bitmap.Config[] f29143e;

    /* renamed from: f, reason: collision with root package name */
    private static final Bitmap.Config[] f29144f;

    /* renamed from: g, reason: collision with root package name */
    private static final Bitmap.Config[] f29145g;

    /* renamed from: h, reason: collision with root package name */
    private static final Bitmap.Config[] f29146h;

    /* renamed from: a, reason: collision with root package name */
    private final c f29147a = new c();

    /* renamed from: b, reason: collision with root package name */
    private final g f29148b = new g();

    /* renamed from: c, reason: collision with root package name */
    private final Map f29149c = new HashMap();

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static /* synthetic */ class a {

        /* renamed from: a, reason: collision with root package name */
        static final /* synthetic */ int[] f29150a;

        static {
            int[] iArr = new int[Bitmap.Config.values().length];
            f29150a = iArr;
            try {
                iArr[Bitmap.Config.ARGB_8888.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                f29150a[Bitmap.Config.RGB_565.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                f29150a[Bitmap.Config.ARGB_4444.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                f29150a[Bitmap.Config.ALPHA_8.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static final class b implements l {

        /* renamed from: a, reason: collision with root package name */
        private final c f29151a;

        /* renamed from: b, reason: collision with root package name */
        int f29152b;

        /* renamed from: c, reason: collision with root package name */
        private Bitmap.Config f29153c;

        public b(c cVar) {
            this.f29151a = cVar;
        }

        @Override // mh.l
        public void a() {
            this.f29151a.c(this);
        }

        public void b(int i10, Bitmap.Config config) {
            this.f29152b = i10;
            this.f29153c = config;
        }

        public boolean equals(Object obj) {
            if (!(obj instanceof b)) {
                return false;
            }
            b bVar = (b) obj;
            return this.f29152b == bVar.f29152b && ei.l.c(this.f29153c, bVar.f29153c);
        }

        public int hashCode() {
            int i10 = this.f29152b * 31;
            Bitmap.Config config = this.f29153c;
            return i10 + (config != null ? config.hashCode() : 0);
        }

        public String toString() {
            return m.h(this.f29152b, this.f29153c);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static class c extends mh.c {
        c() {
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // mh.c
        /* renamed from: d, reason: merged with bridge method [inline-methods] */
        public b a() {
            return new b(this);
        }

        public b e(int i10, Bitmap.Config config) {
            b bVar = (b) b();
            bVar.b(i10, config);
            return bVar;
        }
    }

    static {
        Bitmap.Config[] configArr = (Bitmap.Config[]) Arrays.copyOf(new Bitmap.Config[]{Bitmap.Config.ARGB_8888, null}, 3);
        configArr[configArr.length - 1] = Bitmap.Config.RGBA_F16;
        f29142d = configArr;
        f29143e = configArr;
        f29144f = new Bitmap.Config[]{Bitmap.Config.RGB_565};
        f29145g = new Bitmap.Config[]{Bitmap.Config.ARGB_4444};
        f29146h = new Bitmap.Config[]{Bitmap.Config.ALPHA_8};
    }

    private void f(Integer num, Bitmap bitmap) {
        NavigableMap j10 = j(bitmap.getConfig());
        Integer num2 = (Integer) j10.get(num);
        if (num2 != null) {
            if (num2.intValue() == 1) {
                j10.remove(num);
                return;
            } else {
                j10.put(num, Integer.valueOf(num2.intValue() - 1));
                return;
            }
        }
        throw new NullPointerException("Tried to decrement empty size, size: " + num + ", removed: " + e(bitmap) + ", this: " + this);
    }

    private b g(int i10, Bitmap.Config config) {
        b e10 = this.f29147a.e(i10, config);
        for (Bitmap.Config config2 : i(config)) {
            Integer num = (Integer) j(config2).ceilingKey(Integer.valueOf(i10));
            if (num != null && num.intValue() <= i10 * 8) {
                if (num.intValue() == i10) {
                    if (config2 == null) {
                        if (config == null) {
                            return e10;
                        }
                    } else if (config2.equals(config)) {
                        return e10;
                    }
                }
                this.f29147a.c(e10);
                return this.f29147a.e(num.intValue(), config2);
            }
        }
        return e10;
    }

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

    private static Bitmap.Config[] i(Bitmap.Config config) {
        if (Bitmap.Config.RGBA_F16.equals(config)) {
            return f29143e;
        }
        int i10 = a.f29150a[config.ordinal()];
        return i10 != 1 ? i10 != 2 ? i10 != 3 ? i10 != 4 ? new Bitmap.Config[]{config} : f29146h : f29145g : f29144f : f29142d;
    }

    private NavigableMap j(Bitmap.Config config) {
        NavigableMap navigableMap = (NavigableMap) this.f29149c.get(config);
        if (navigableMap != null) {
            return navigableMap;
        }
        TreeMap treeMap = new TreeMap();
        this.f29149c.put(config, treeMap);
        return treeMap;
    }

    @Override // mh.k
    public String a(int i10, int i11, Bitmap.Config config) {
        return h(ei.l.f(i10, i11, config), config);
    }

    @Override // mh.k
    public int b(Bitmap bitmap) {
        return ei.l.g(bitmap);
    }

    @Override // mh.k
    public void c(Bitmap bitmap) {
        b e10 = this.f29147a.e(ei.l.g(bitmap), bitmap.getConfig());
        this.f29148b.d(e10, bitmap);
        NavigableMap j10 = j(bitmap.getConfig());
        Integer num = (Integer) j10.get(Integer.valueOf(e10.f29152b));
        j10.put(Integer.valueOf(e10.f29152b), Integer.valueOf(num != null ? 1 + num.intValue() : 1));
    }

    @Override // mh.k
    public Bitmap d(int i10, int i11, Bitmap.Config config) {
        b g10 = g(ei.l.f(i10, i11, config), config);
        Bitmap bitmap = (Bitmap) this.f29148b.a(g10);
        if (bitmap != null) {
            f(Integer.valueOf(g10.f29152b), bitmap);
            bitmap.reconfigure(i10, i11, config);
        }
        return bitmap;
    }

    @Override // mh.k
    public String e(Bitmap bitmap) {
        return h(ei.l.g(bitmap), bitmap.getConfig());
    }

    @Override // mh.k
    public Bitmap removeLast() {
        Bitmap bitmap = (Bitmap) this.f29148b.f();
        if (bitmap != null) {
            f(Integer.valueOf(ei.l.g(bitmap)), bitmap);
        }
        return bitmap;
    }

    public String toString() {
        StringBuilder sb2 = new StringBuilder();
        sb2.append("SizeConfigStrategy{groupedMap=");
        sb2.append(this.f29148b);
        sb2.append(", sortedSizes=(");
        for (Map.Entry entry : this.f29149c.entrySet()) {
            sb2.append(entry.getKey());
            sb2.append('[');
            sb2.append(entry.getValue());
            sb2.append("], ");
        }
        if (!this.f29149c.isEmpty()) {
            sb2.replace(sb2.length() - 2, sb2.length(), "");
        }
        sb2.append(")}");
        return sb2.toString();
    }
}
