package com.google.android.gms.internal.p002firebaseauthapi;

import java.lang.Comparable;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/* JADX INFO: loaded from: classes3.dex */
class zzamh<K extends Comparable<K>, V> extends AbstractMap<K, V> {
    private Object[] zza;
    private int zzb;
    private Map<K, V> zzc;
    private boolean zzd;
    private volatile zzamn zze;
    private Map<K, V> zzf;

    private final SortedMap<K, V> zzf() {
        zzg();
        if (this.zzc.isEmpty() && !(this.zzc instanceof TreeMap)) {
            TreeMap treeMap = new TreeMap();
            this.zzc = treeMap;
            this.zzf = treeMap.descendingMap();
        }
        return (SortedMap) this.zzc;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void zzg() {
        if (this.zzd) {
            throw new UnsupportedOperationException();
        }
    }

    @Override // java.util.AbstractMap, java.util.Map
    public void clear() {
        zzg();
        if (this.zzb != 0) {
            this.zza = null;
            this.zzb = 0;
        }
        if (this.zzc.isEmpty()) {
            return;
        }
        this.zzc.clear();
    }

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

    @Override // java.util.AbstractMap, java.util.Map
    public Set<Map.Entry<K, V>> entrySet() {
        if (this.zze == null) {
            this.zze = new zzamn(this);
        }
        return this.zze;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof zzamh)) {
            return super.equals(obj);
        }
        zzamh zzamhVar = (zzamh) obj;
        int size = size();
        if (size != zzamhVar.size()) {
            return false;
        }
        int i6 = this.zzb;
        if (i6 != zzamhVar.zzb) {
            return entrySet().equals(zzamhVar.entrySet());
        }
        for (int i7 = 0; i7 < i6; i7++) {
            if (!zza(i7).equals(zzamhVar.zza(i7))) {
                return false;
            }
        }
        if (i6 != size) {
            return this.zzc.equals(zzamhVar.zzc);
        }
        return true;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public V get(Object obj) {
        Comparable comparable = (Comparable) obj;
        int iZza = zza(comparable);
        return iZza >= 0 ? (V) ((zzaml) this.zza[iZza]).getValue() : this.zzc.get(comparable);
    }

    @Override // java.util.AbstractMap, java.util.Map
    public int hashCode() {
        int i6 = this.zzb;
        int iHashCode = 0;
        for (int i7 = 0; i7 < i6; i7++) {
            iHashCode += this.zza[i7].hashCode();
        }
        return this.zzc.size() > 0 ? this.zzc.hashCode() + iHashCode : iHashCode;
    }

    @Override // java.util.AbstractMap, java.util.Map
    public V remove(Object obj) {
        zzg();
        Comparable comparable = (Comparable) obj;
        int iZza = zza(comparable);
        if (iZza >= 0) {
            return zzb(iZza);
        }
        if (this.zzc.isEmpty()) {
            return null;
        }
        return this.zzc.remove(comparable);
    }

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

    private zzamh() {
        Map<K, V> map = Collections.EMPTY_MAP;
        this.zzc = map;
        this.zzf = map;
    }

    public final int zzb() {
        return this.zzb;
    }

    public final Iterable<Map.Entry<K, V>> zzc() {
        return this.zzc.isEmpty() ? Collections.EMPTY_SET : this.zzc.entrySet();
    }

    public final Set<Map.Entry<K, V>> zzd() {
        return new zzami(this);
    }

    public final boolean zze() {
        return this.zzd;
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x0022  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private final int zza(K k6) {
        int i6;
        int i7;
        int i8 = this.zzb;
        int i9 = i8 - 1;
        if (i9 < 0) {
            i6 = 0;
            while (i6 <= i9) {
                int i10 = (i6 + i9) / 2;
                int iCompareTo = k6.compareTo((Comparable) ((zzaml) this.zza[i10]).getKey());
                if (iCompareTo < 0) {
                    i9 = i10 - 1;
                } else {
                    if (iCompareTo <= 0) {
                        return i10;
                    }
                    i6 = i10 + 1;
                }
            }
            i7 = i6 + 1;
        } else {
            int iCompareTo2 = k6.compareTo((Comparable) ((zzaml) this.zza[i9]).getKey());
            if (iCompareTo2 > 0) {
                i7 = i8 + 1;
            } else {
                if (iCompareTo2 == 0) {
                    return i9;
                }
                i6 = 0;
                while (i6 <= i9) {
                }
                i7 = i6 + 1;
            }
        }
        return -i7;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final V zzb(int i6) {
        zzg();
        V v3 = (V) ((zzaml) this.zza[i6]).getValue();
        Object[] objArr = this.zza;
        System.arraycopy(objArr, i6 + 1, objArr, i6, (this.zzb - i6) - 1);
        this.zzb--;
        if (!this.zzc.isEmpty()) {
            Iterator<Map.Entry<K, V>> it = zzf().entrySet().iterator();
            this.zza[this.zzb] = new zzaml(this, it.next());
            this.zzb++;
            it.remove();
        }
        return v3;
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // java.util.AbstractMap, java.util.Map
    /* JADX INFO: renamed from: zza, reason: merged with bridge method [inline-methods] */
    public final V put(K k6, V v3) {
        zzg();
        int iZza = zza(k6);
        if (iZza >= 0) {
            return (V) ((zzaml) this.zza[iZza]).setValue(v3);
        }
        zzg();
        if (this.zza == null) {
            this.zza = new Object[16];
        }
        int i6 = -(iZza + 1);
        if (i6 >= 16) {
            return zzf().put(k6, v3);
        }
        int i7 = this.zzb;
        if (i7 == 16) {
            zzaml zzamlVar = (zzaml) this.zza[15];
            this.zzb = i7 - 1;
            zzf().put((Comparable) zzamlVar.getKey(), zzamlVar.getValue());
        }
        Object[] objArr = this.zza;
        System.arraycopy(objArr, i6, objArr, i6 + 1, (objArr.length - i6) - 1);
        this.zza[i6] = new zzaml(this, k6, v3);
        this.zzb++;
        return null;
    }

    public final Map.Entry<K, V> zza(int i6) {
        if (i6 < this.zzb) {
            return (zzaml) this.zza[i6];
        }
        throw new ArrayIndexOutOfBoundsException(i6);
    }

    public void zza() {
        Map<K, V> mapUnmodifiableMap;
        Map<K, V> mapUnmodifiableMap2;
        if (this.zzd) {
            return;
        }
        if (this.zzc.isEmpty()) {
            mapUnmodifiableMap = Collections.EMPTY_MAP;
        } else {
            mapUnmodifiableMap = Collections.unmodifiableMap(this.zzc);
        }
        this.zzc = mapUnmodifiableMap;
        if (this.zzf.isEmpty()) {
            mapUnmodifiableMap2 = Collections.EMPTY_MAP;
        } else {
            mapUnmodifiableMap2 = Collections.unmodifiableMap(this.zzf);
        }
        this.zzf = mapUnmodifiableMap2;
        this.zzd = true;
    }
}
