package com.google.firebase.firestore;

import com.google.firebase.Timestamp;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.model.DatabaseId;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.ServerTimestamps;
import com.google.firebase.firestore.model.Values;
import com.google.firebase.firestore.util.Assert;
import com.google.firebase.firestore.util.Logger;
import com.google.protobuf.R0;
import e4.C1975d;
import e4.H0;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import m1.AbstractC2464a;

/* JADX INFO: loaded from: classes3.dex */
public class UserDataWriter {
    private final FirebaseFirestore firestore;
    private final DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior;

    /* JADX INFO: renamed from: com.google.firebase.firestore.UserDataWriter$1, reason: invalid class name */
    public static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$google$firebase$firestore$DocumentSnapshot$ServerTimestampBehavior;

        static {
            int[] iArr = new int[DocumentSnapshot.ServerTimestampBehavior.values().length];
            $SwitchMap$com$google$firebase$firestore$DocumentSnapshot$ServerTimestampBehavior = iArr;
            try {
                iArr[DocumentSnapshot.ServerTimestampBehavior.PREVIOUS.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$com$google$firebase$firestore$DocumentSnapshot$ServerTimestampBehavior[DocumentSnapshot.ServerTimestampBehavior.ESTIMATE.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
        }
    }

    public UserDataWriter(FirebaseFirestore firebaseFirestore, DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior) {
        this.firestore = firebaseFirestore;
        this.serverTimestampBehavior = serverTimestampBehavior;
    }

    private List<Object> convertArray(C1975d c1975d) {
        ArrayList arrayList = new ArrayList(c1975d.k());
        Iterator it = c1975d.a().iterator();
        while (it.hasNext()) {
            arrayList.add(convertValue((H0) it.next()));
        }
        return arrayList;
    }

    private Object convertReference(H0 h0) {
        DatabaseId databaseIdFromName = DatabaseId.fromName(h0.y());
        DocumentKey documentKeyFromName = DocumentKey.fromName(h0.y());
        DatabaseId databaseId = this.firestore.getDatabaseId();
        if (!databaseIdFromName.equals(databaseId)) {
            Logger.warn("DocumentSnapshot", "Document %s contains a document reference within a different database (%s/%s) which is not supported. It will be treated as a reference in the current database (%s/%s) instead.", documentKeyFromName.getPath(), databaseIdFromName.getProjectId(), databaseIdFromName.getDatabaseId(), databaseId.getProjectId(), databaseId.getDatabaseId());
        }
        return new DocumentReference(documentKeyFromName, this.firestore);
    }

    private Object convertServerTimestamp(H0 h0) {
        int i6 = AnonymousClass1.$SwitchMap$com$google$firebase$firestore$DocumentSnapshot$ServerTimestampBehavior[this.serverTimestampBehavior.ordinal()];
        if (i6 != 1) {
            if (i6 != 2) {
                return null;
            }
            return convertTimestamp(ServerTimestamps.getLocalWriteTime(h0));
        }
        H0 previousValue = ServerTimestamps.getPreviousValue(h0);
        if (previousValue == null) {
            return null;
        }
        return convertValue(previousValue);
    }

    private Object convertTimestamp(R0 r02) {
        return new Timestamp(r02.j(), r02.i());
    }

    public Map<String, Object> convertObject(Map<String, H0> map) {
        HashMap map2 = new HashMap();
        for (Map.Entry<String, H0> entry : map.entrySet()) {
            map2.put(entry.getKey(), convertValue(entry.getValue()));
        }
        return map2;
    }

    public Object convertValue(H0 h0) {
        switch (Values.typeOrder(h0)) {
            case 0:
                return null;
            case 1:
                return Boolean.valueOf(h0.r());
            case 2:
                return AbstractC2464a.a(h0.B(), 3) ? Long.valueOf(h0.w()) : Double.valueOf(h0.u());
            case 3:
                return convertTimestamp(h0.A());
            case 4:
                return convertServerTimestamp(h0);
            case 5:
                return h0.z();
            case 6:
                return Blob.fromByteString(h0.s());
            case 7:
                return convertReference(h0);
            case 8:
                return new GeoPoint(h0.v().i(), h0.v().j());
            case 9:
                return convertArray(h0.q());
            case 10:
                return convertVectorValue(h0.x().i());
            case 11:
                return convertObject(h0.x().i());
            default:
                throw Assert.fail("Unknown value type: ".concat(com.google.android.recaptcha.internal.a.x(h0.B())), new Object[0]);
        }
    }

    public VectorValue convertVectorValue(Map<String, H0> map) {
        List listA = map.get("value").q().a();
        double[] dArr = new double[listA.size()];
        for (int i6 = 0; i6 < listA.size(); i6++) {
            dArr[i6] = ((H0) listA.get(i6)).u();
        }
        return new VectorValue(dArr);
    }
}
