package com.google.firebase.firestore.model;

import com.google.firebase.firestore.model.mutation.FieldMask;
import com.google.firebase.firestore.util.Assert;
import e4.G0;
import e4.H;
import e4.H0;
import e4.J;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/* JADX INFO: loaded from: classes3.dex */
public final class ObjectValue implements Cloneable {
    private final Map<String, Object> overlayMap;
    private H0 partialValue;

    public ObjectValue(H0 h0) {
        this.overlayMap = new HashMap();
        Assert.hardAssert(h0.B() == 11, "ObjectValues should be backed by a MapValue", new Object[0]);
        Assert.hardAssert(!ServerTimestamps.isServerTimestamp(h0), "ServerTimestamps should not be used as an ObjectValue", new Object[0]);
        this.partialValue = h0;
    }

    private J applyOverlay(FieldPath fieldPath, Map<String, Object> map) {
        H0 h0ExtractNestedValue = extractNestedValue(this.partialValue, fieldPath);
        H hL = Values.isMapValue(h0ExtractNestedValue) ? (H) h0ExtractNestedValue.x().m367toBuilder() : J.l();
        boolean z6 = false;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof Map) {
                J jApplyOverlay = applyOverlay(fieldPath.append(key), (Map) value);
                if (jApplyOverlay != null) {
                    G0 g0C = H0.C();
                    g0C.m(jApplyOverlay);
                    hL.e((H0) g0C.m359build(), key);
                    z6 = true;
                }
            } else {
                if (value instanceof H0) {
                    hL.e((H0) value, key);
                } else if (hL.c(key)) {
                    Assert.hardAssert(value == null, "Expected entry to be a Map, a Value or null", new Object[0]);
                    hL.f(key);
                }
                z6 = true;
            }
        }
        if (z6) {
            return (J) hL.m359build();
        }
        return null;
    }

    private H0 buildProto() {
        synchronized (this.overlayMap) {
            try {
                J jApplyOverlay = applyOverlay(FieldPath.EMPTY_PATH, this.overlayMap);
                if (jApplyOverlay != null) {
                    G0 g0C = H0.C();
                    g0C.m(jApplyOverlay);
                    this.partialValue = (H0) g0C.m359build();
                    this.overlayMap.clear();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return this.partialValue;
    }

    private FieldMask extractFieldMask(J j4) {
        HashSet hashSet = new HashSet();
        for (Map.Entry entry : j4.i().entrySet()) {
            FieldPath fieldPathFromSingleSegment = FieldPath.fromSingleSegment((String) entry.getKey());
            if (Values.isMapValue((H0) entry.getValue())) {
                Set<FieldPath> mask = extractFieldMask(((H0) entry.getValue()).x()).getMask();
                if (mask.isEmpty()) {
                    hashSet.add(fieldPathFromSingleSegment);
                } else {
                    Iterator<FieldPath> it = mask.iterator();
                    while (it.hasNext()) {
                        hashSet.add(fieldPathFromSingleSegment.append(it.next()));
                    }
                }
            } else {
                hashSet.add(fieldPathFromSingleSegment);
            }
        }
        return FieldMask.fromSet(hashSet);
    }

    private H0 extractNestedValue(H0 h0, FieldPath fieldPath) {
        if (fieldPath.isEmpty()) {
            return h0;
        }
        for (int i6 = 0; i6 < fieldPath.length() - 1; i6++) {
            h0 = h0.x().j(fieldPath.getSegment(i6));
            if (!Values.isMapValue(h0)) {
                return null;
            }
        }
        return h0.x().j(fieldPath.getLastSegment());
    }

    public static ObjectValue fromMap(Map<String, H0> map) {
        G0 g0C = H0.C();
        H hL = J.l();
        hL.d(map);
        g0C.l(hL);
        return new ObjectValue((H0) g0C.m359build());
    }

    private void setOverlay(FieldPath fieldPath, H0 h0) {
        Map<String, Object> map;
        Map<String, Object> map2 = this.overlayMap;
        for (int i6 = 0; i6 < fieldPath.length() - 1; i6++) {
            String segment = fieldPath.getSegment(i6);
            Object obj = map2.get(segment);
            if (obj instanceof Map) {
                map = (Map) obj;
            } else {
                if (obj instanceof H0) {
                    H0 h02 = (H0) obj;
                    if (h02.B() == 11) {
                        HashMap map3 = new HashMap(h02.x().i());
                        map2.put(segment, map3);
                        map2 = map3;
                    }
                }
                map = new HashMap<>();
                map2.put(segment, map);
            }
            map2 = map;
        }
        map2.put(fieldPath.getLastSegment(), h0);
    }

    public void delete(FieldPath fieldPath) {
        Assert.hardAssert(!fieldPath.isEmpty(), "Cannot delete field for empty path on ObjectValue", new Object[0]);
        setOverlay(fieldPath, null);
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof ObjectValue) {
            return Values.equals(buildProto(), ((ObjectValue) obj).buildProto());
        }
        return false;
    }

    public H0 get(FieldPath fieldPath) {
        return extractNestedValue(buildProto(), fieldPath);
    }

    public FieldMask getFieldMask() {
        return extractFieldMask(buildProto().x());
    }

    public Map<String, H0> getFieldsMap() {
        return buildProto().x().i();
    }

    public int hashCode() {
        return buildProto().hashCode();
    }

    public void set(FieldPath fieldPath, H0 h0) {
        Assert.hardAssert(!fieldPath.isEmpty(), "Cannot set field for empty path on ObjectValue", new Object[0]);
        setOverlay(fieldPath, h0);
    }

    public void setAll(Map<FieldPath, H0> map) {
        for (Map.Entry<FieldPath, H0> entry : map.entrySet()) {
            FieldPath key = entry.getKey();
            if (entry.getValue() == null) {
                delete(key);
            } else {
                set(key, entry.getValue());
            }
        }
    }

    public String toString() {
        return "ObjectValue{internalValue=" + Values.canonicalId(buildProto()) + '}';
    }

    /* JADX INFO: renamed from: clone, reason: merged with bridge method [inline-methods] */
    public ObjectValue m354clone() {
        return new ObjectValue(buildProto());
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public ObjectValue() {
        G0 g0C = H0.C();
        g0C.m(J.g());
        this((H0) g0C.m359build());
    }
}
