package androidx.datastore.preferences.protobuf;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/* JADX INFO: loaded from: classes.dex */
public final class Z extends AbstractMap {

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final /* synthetic */ int f11354j = 0;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public List f11356b = Collections.EMPTY_LIST;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public volatile f0 f11359e;

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

    public Z(int i6) {
        this.f11355a = i6;
        Map map = Collections.EMPTY_MAP;
        this.f11357c = map;
        this.f11360f = map;
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x0024  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final int a(Comparable comparable) {
        int i6;
        int i7;
        int size = this.f11356b.size();
        int i8 = size - 1;
        if (i8 < 0) {
            i6 = 0;
            while (i6 <= i8) {
                int i9 = (i6 + i8) / 2;
                int iCompareTo = comparable.compareTo(((d0) this.f11356b.get(i9)).f11370a);
                if (iCompareTo < 0) {
                    i8 = i9 - 1;
                } else {
                    if (iCompareTo <= 0) {
                        return i9;
                    }
                    i6 = i9 + 1;
                }
            }
            i7 = i6 + 1;
        } else {
            int iCompareTo2 = comparable.compareTo(((d0) this.f11356b.get(i8)).f11370a);
            if (iCompareTo2 > 0) {
                i7 = size + 1;
            } else {
                if (iCompareTo2 == 0) {
                    return i8;
                }
                i6 = 0;
                while (i6 <= i8) {
                }
                i7 = i6 + 1;
            }
        }
        return -i7;
    }

    public final void b() {
        if (this.f11358d) {
            throw new UnsupportedOperationException();
        }
    }

    public final Map.Entry c(int i6) {
        return (Map.Entry) this.f11356b.get(i6);
    }

    @Override // java.util.AbstractMap, java.util.Map
    public final void clear() {
        b();
        if (!this.f11356b.isEmpty()) {
            this.f11356b.clear();
        }
        if (this.f11357c.isEmpty()) {
            return;
        }
        this.f11357c.clear();
    }

    @Override // java.util.AbstractMap, java.util.Map
    public final boolean containsKey(Object obj) {
        Comparable comparable = (Comparable) obj;
        return a(comparable) >= 0 || this.f11357c.containsKey(comparable);
    }

    public final Iterable d() {
        return this.f11357c.isEmpty() ? c0.f11365b : this.f11357c.entrySet();
    }

    @Override // java.util.AbstractMap, java.util.Map
    public final Set entrySet() {
        if (this.f11359e == null) {
            this.f11359e = new f0(0, this);
        }
        return this.f11359e;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public final boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Z)) {
            return super.equals(obj);
        }
        Z z6 = (Z) obj;
        int size = size();
        if (size == z6.size()) {
            int size2 = this.f11356b.size();
            if (size2 != z6.f11356b.size()) {
                return ((AbstractSet) entrySet()).equals(z6.entrySet());
            }
            for (int i6 = 0; i6 < size2; i6++) {
                if (c(i6).equals(z6.c(i6))) {
                }
            }
            if (size2 != size) {
                return this.f11357c.equals(z6.f11357c);
            }
            return true;
        }
        return false;
    }

    public final SortedMap f() {
        b();
        if (this.f11357c.isEmpty() && !(this.f11357c instanceof TreeMap)) {
            TreeMap treeMap = new TreeMap();
            this.f11357c = treeMap;
            this.f11360f = treeMap.descendingMap();
        }
        return (SortedMap) this.f11357c;
    }

    @Override // java.util.AbstractMap, java.util.Map
    /* JADX INFO: renamed from: g, reason: merged with bridge method [inline-methods] */
    public final Object put(Comparable comparable, Object obj) {
        b();
        int iA = a(comparable);
        if (iA >= 0) {
            return ((d0) this.f11356b.get(iA)).setValue(obj);
        }
        b();
        boolean zIsEmpty = this.f11356b.isEmpty();
        int i6 = this.f11355a;
        if (zIsEmpty && !(this.f11356b instanceof ArrayList)) {
            this.f11356b = new ArrayList(i6);
        }
        int i7 = -(iA + 1);
        if (i7 >= i6) {
            return f().put(comparable, obj);
        }
        if (this.f11356b.size() == i6) {
            d0 d0Var = (d0) this.f11356b.remove(i6 - 1);
            f().put(d0Var.f11370a, d0Var.f11371b);
        }
        this.f11356b.add(i7, new d0(this, comparable, obj));
        return null;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public final Object get(Object obj) {
        Comparable comparable = (Comparable) obj;
        int iA = a(comparable);
        return iA >= 0 ? ((d0) this.f11356b.get(iA)).f11371b : this.f11357c.get(comparable);
    }

    public final Object h(int i6) {
        b();
        Object obj = ((d0) this.f11356b.remove(i6)).f11371b;
        if (!this.f11357c.isEmpty()) {
            Iterator it = f().entrySet().iterator();
            List list = this.f11356b;
            Map.Entry entry = (Map.Entry) it.next();
            list.add(new d0(this, (Comparable) entry.getKey(), entry.getValue()));
            it.remove();
        }
        return obj;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public final int hashCode() {
        int size = this.f11356b.size();
        int iHashCode = 0;
        for (int i6 = 0; i6 < size; i6++) {
            iHashCode += ((d0) this.f11356b.get(i6)).hashCode();
        }
        return this.f11357c.size() > 0 ? this.f11357c.hashCode() + iHashCode : iHashCode;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public final Object remove(Object obj) {
        b();
        Comparable comparable = (Comparable) obj;
        int iA = a(comparable);
        if (iA >= 0) {
            return h(iA);
        }
        if (this.f11357c.isEmpty()) {
            return null;
        }
        return this.f11357c.remove(comparable);
    }

    @Override // java.util.AbstractMap, java.util.Map
    public final int size() {
        return this.f11357c.size() + this.f11356b.size();
    }
}
