package com.google.firebase.firestore.model;

import Z0.o;
import com.google.android.recaptcha.internal.a;
import com.google.firebase.firestore.util.Assert;
import com.google.firebase.firestore.util.Util;
import com.google.firebase.sessions.settings.RemoteSettings;
import com.google.protobuf.AbstractC1320n;
import com.google.protobuf.Q0;
import com.google.protobuf.R0;
import com.revenuecat.purchases.common.Constants;
import e4.C1975d;
import e4.G0;
import e4.H;
import e4.H0;
import e4.InterfaceC1977e;
import e4.J;
import i4.C2281a;
import i4.b;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import m1.AbstractC2464a;
import q.AbstractC2761L;

/* JADX INFO: loaded from: classes3.dex */
public class Values {
    public static final H0 MAX_VALUE;
    public static final H0 MAX_VALUE_TYPE;
    public static H0 MIN_ARRAY = null;
    public static H0 MIN_BOOLEAN = null;
    public static H0 MIN_BYTES = null;
    public static H0 MIN_GEO_POINT = null;
    public static H0 MIN_MAP = null;
    public static H0 MIN_NUMBER = null;
    public static H0 MIN_REFERENCE = null;
    public static H0 MIN_STRING = null;
    public static H0 MIN_TIMESTAMP = null;
    public static final H0 MIN_VALUE;
    private static final H0 MIN_VECTOR_VALUE;
    public static final H0 NAN_VALUE;
    public static final H0 NULL_VALUE;
    public static final String TYPE_KEY = "__type__";
    public static final int TYPE_ORDER_ARRAY = 9;
    public static final int TYPE_ORDER_BLOB = 6;
    public static final int TYPE_ORDER_BOOLEAN = 1;
    public static final int TYPE_ORDER_GEOPOINT = 8;
    public static final int TYPE_ORDER_MAP = 11;
    public static final int TYPE_ORDER_MAX_VALUE = Integer.MAX_VALUE;
    public static final int TYPE_ORDER_NULL = 0;
    public static final int TYPE_ORDER_NUMBER = 2;
    public static final int TYPE_ORDER_REFERENCE = 7;
    public static final int TYPE_ORDER_SERVER_TIMESTAMP = 4;
    public static final int TYPE_ORDER_STRING = 5;
    public static final int TYPE_ORDER_TIMESTAMP = 3;
    public static final int TYPE_ORDER_VECTOR = 10;
    public static final String VECTOR_MAP_VECTORS_KEY = "value";
    public static final H0 VECTOR_VALUE_TYPE;

    /* JADX INFO: renamed from: com.google.firebase.firestore.model.Values$1, reason: invalid class name */
    public static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase;

        static {
            int[] iArr = new int[AbstractC2464a.d(12).length];
            $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase = iArr;
            try {
                iArr[0] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase[1] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase[2] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase[3] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase[4] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase[5] = 6;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase[6] = 7;
            } catch (NoSuchFieldError unused7) {
            }
            try {
                $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase[7] = 8;
            } catch (NoSuchFieldError unused8) {
            }
            try {
                $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase[8] = 9;
            } catch (NoSuchFieldError unused9) {
            }
            try {
                $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase[9] = 10;
            } catch (NoSuchFieldError unused10) {
            }
            try {
                $SwitchMap$com$google$firestore$v1$Value$ValueTypeCase[10] = 11;
            } catch (NoSuchFieldError unused11) {
            }
        }
    }

    static {
        G0 g0C = H0.C();
        g0C.i(Double.NaN);
        NAN_VALUE = (H0) g0C.m359build();
        G0 g0C2 = H0.C();
        g0C2.n();
        H0 h0 = (H0) g0C2.m359build();
        NULL_VALUE = h0;
        MIN_VALUE = h0;
        G0 g0C3 = H0.C();
        g0C3.p("__max__");
        H0 h02 = (H0) g0C3.m359build();
        MAX_VALUE_TYPE = h02;
        G0 g0C4 = H0.C();
        H hL = J.l();
        hL.e(h02, TYPE_KEY);
        g0C4.l(hL);
        MAX_VALUE = (H0) g0C4.m359build();
        G0 g0C5 = H0.C();
        g0C5.p("__vector__");
        H0 h03 = (H0) g0C5.m359build();
        VECTOR_VALUE_TYPE = h03;
        G0 g0C6 = H0.C();
        H hL2 = J.l();
        hL2.e(h03, TYPE_KEY);
        G0 g0C7 = H0.C();
        g0C7.d(C1975d.l());
        hL2.e((H0) g0C7.m359build(), "value");
        g0C6.l(hL2);
        MIN_VECTOR_VALUE = (H0) g0C6.m359build();
        G0 g0C8 = H0.C();
        g0C8.f(false);
        MIN_BOOLEAN = (H0) g0C8.m359build();
        G0 g0C9 = H0.C();
        g0C9.i(Double.NaN);
        MIN_NUMBER = (H0) g0C9.m359build();
        G0 g0C10 = H0.C();
        Q0 q0K = R0.k();
        q0K.d(Long.MIN_VALUE);
        g0C10.q(q0K);
        MIN_TIMESTAMP = (H0) g0C10.m359build();
        G0 g0C11 = H0.C();
        g0C11.p("");
        MIN_STRING = (H0) g0C11.m359build();
        G0 g0C12 = H0.C();
        g0C12.h(AbstractC1320n.f14310b);
        MIN_BYTES = (H0) g0C12.m359build();
        MIN_REFERENCE = refValue(DatabaseId.EMPTY, DocumentKey.empty());
        G0 g0C13 = H0.C();
        C2281a c2281aK = b.k();
        c2281aK.c(-90.0d);
        c2281aK.d(-180.0d);
        g0C13.j(c2281aK);
        MIN_GEO_POINT = (H0) g0C13.m359build();
        G0 g0C14 = H0.C();
        g0C14.e(C1975d.i());
        MIN_ARRAY = (H0) g0C14.m359build();
        G0 g0C15 = H0.C();
        g0C15.m(J.g());
        MIN_MAP = (H0) g0C15.m359build();
    }

    private static boolean arrayEquals(H0 h0, H0 h02) {
        C1975d c1975dQ = h0.q();
        C1975d c1975dQ2 = h02.q();
        if (c1975dQ.k() != c1975dQ2.k()) {
            return false;
        }
        for (int i6 = 0; i6 < c1975dQ.k(); i6++) {
            if (!equals(c1975dQ.j(i6), c1975dQ2.j(i6))) {
                return false;
            }
        }
        return true;
    }

    public static String canonicalId(H0 h0) {
        StringBuilder sb = new StringBuilder();
        canonifyValue(sb, h0);
        return sb.toString();
    }

    private static void canonifyArray(StringBuilder sb, C1975d c1975d) {
        sb.append("[");
        for (int i6 = 0; i6 < c1975d.k(); i6++) {
            canonifyValue(sb, c1975d.j(i6));
            if (i6 != c1975d.k() - 1) {
                sb.append(",");
            }
        }
        sb.append("]");
    }

    private static void canonifyGeoPoint(StringBuilder sb, b bVar) {
        sb.append("geo(" + bVar.i() + "," + bVar.j() + ")");
    }

    private static void canonifyObject(StringBuilder sb, J j4) {
        ArrayList<String> arrayList = new ArrayList(j4.i().keySet());
        Collections.sort(arrayList);
        sb.append("{");
        boolean z6 = true;
        for (String str : arrayList) {
            if (z6) {
                z6 = false;
            } else {
                sb.append(",");
            }
            sb.append(str);
            sb.append(Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR);
            canonifyValue(sb, j4.k(str));
        }
        sb.append("}");
    }

    private static void canonifyReference(StringBuilder sb, H0 h0) {
        Assert.hardAssert(isReferenceValue(h0), "Value should be a ReferenceValue", new Object[0]);
        sb.append(DocumentKey.fromName(h0.y()));
    }

    private static void canonifyTimestamp(StringBuilder sb, R0 r02) {
        sb.append("time(" + r02.j() + "," + r02.i() + ")");
    }

    private static void canonifyValue(StringBuilder sb, H0 h0) {
        switch (AbstractC2464a.c(h0.B())) {
            case 0:
                sb.append("null");
                return;
            case 1:
                sb.append(h0.r());
                return;
            case 2:
                sb.append(h0.w());
                return;
            case 3:
                sb.append(h0.u());
                return;
            case 4:
                canonifyTimestamp(sb, h0.A());
                return;
            case 5:
                sb.append(h0.z());
                return;
            case 6:
                sb.append(Util.toDebugString(h0.s()));
                return;
            case 7:
                canonifyReference(sb, h0);
                return;
            case 8:
                canonifyGeoPoint(sb, h0.v());
                return;
            case 9:
                canonifyArray(sb, h0.q());
                return;
            case 10:
                canonifyObject(sb, h0.x());
                return;
            default:
                throw Assert.fail("Invalid value type: ".concat(a.x(h0.B())), new Object[0]);
        }
    }

    public static int compare(H0 h0, H0 h02) {
        int iTypeOrder = typeOrder(h0);
        int iTypeOrder2 = typeOrder(h02);
        if (iTypeOrder != iTypeOrder2) {
            return Util.compareIntegers(iTypeOrder, iTypeOrder2);
        }
        if (iTypeOrder != Integer.MAX_VALUE) {
            switch (iTypeOrder) {
                case 0:
                    break;
                case 1:
                    return Util.compareBooleans(h0.r(), h02.r());
                case 2:
                    return compareNumbers(h0, h02);
                case 3:
                    return compareTimestamps(h0.A(), h02.A());
                case 4:
                    return compareTimestamps(ServerTimestamps.getLocalWriteTime(h0), ServerTimestamps.getLocalWriteTime(h02));
                case 5:
                    return Util.compareUtf8Strings(h0.z(), h02.z());
                case 6:
                    return Util.compareByteStrings(h0.s(), h02.s());
                case 7:
                    return compareReferences(h0.y(), h02.y());
                case 8:
                    return compareGeoPoints(h0.v(), h02.v());
                case 9:
                    return compareArrays(h0.q(), h02.q());
                case 10:
                    return compareVectors(h0.x(), h02.x());
                case 11:
                    return compareMaps(h0.x(), h02.x());
                default:
                    throw Assert.fail(o.l("Invalid value type: ", iTypeOrder), new Object[0]);
            }
        }
        return 0;
    }

    private static int compareArrays(C1975d c1975d, C1975d c1975d2) {
        int iMin = Math.min(c1975d.k(), c1975d2.k());
        for (int i6 = 0; i6 < iMin; i6++) {
            int iCompare = compare(c1975d.j(i6), c1975d2.j(i6));
            if (iCompare != 0) {
                return iCompare;
            }
        }
        return Util.compareIntegers(c1975d.k(), c1975d2.k());
    }

    private static int compareGeoPoints(b bVar, b bVar2) {
        int iCompareDoubles = Util.compareDoubles(bVar.i(), bVar2.i());
        return iCompareDoubles == 0 ? Util.compareDoubles(bVar.j(), bVar2.j()) : iCompareDoubles;
    }

    private static int compareMaps(J j4, J j6) {
        Iterator it = new TreeMap(j4.i()).entrySet().iterator();
        Iterator it2 = new TreeMap(j6.i()).entrySet().iterator();
        while (it.hasNext() && it2.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            Map.Entry entry2 = (Map.Entry) it2.next();
            int iCompareUtf8Strings = Util.compareUtf8Strings((String) entry.getKey(), (String) entry2.getKey());
            if (iCompareUtf8Strings != 0) {
                return iCompareUtf8Strings;
            }
            int iCompare = compare((H0) entry.getValue(), (H0) entry2.getValue());
            if (iCompare != 0) {
                return iCompare;
            }
        }
        return Util.compareBooleans(it.hasNext(), it2.hasNext());
    }

    private static int compareNumbers(H0 h0, H0 h02) {
        if (h0.B() == 4) {
            double dU = h0.u();
            if (h02.B() == 4) {
                return Util.compareDoubles(dU, h02.u());
            }
            if (h02.B() == 3) {
                return Util.compareMixed(dU, h02.w());
            }
        } else if (h0.B() == 3) {
            long jW = h0.w();
            if (h02.B() == 3) {
                return Util.compareLongs(jW, h02.w());
            }
            if (h02.B() == 4) {
                return Util.compareMixed(h02.u(), jW) * (-1);
            }
        }
        throw Assert.fail("Unexpected values: %s vs %s", h0, h02);
    }

    private static int compareReferences(String str, String str2) {
        String[] strArrSplit = str.split(RemoteSettings.FORWARD_SLASH_STRING, -1);
        String[] strArrSplit2 = str2.split(RemoteSettings.FORWARD_SLASH_STRING, -1);
        int iMin = Math.min(strArrSplit.length, strArrSplit2.length);
        for (int i6 = 0; i6 < iMin; i6++) {
            int iCompareTo = strArrSplit[i6].compareTo(strArrSplit2[i6]);
            if (iCompareTo != 0) {
                return iCompareTo;
            }
        }
        return Util.compareIntegers(strArrSplit.length, strArrSplit2.length);
    }

    private static int compareTimestamps(R0 r02, R0 r03) {
        int iCompareLongs = Util.compareLongs(r02.j(), r03.j());
        return iCompareLongs != 0 ? iCompareLongs : Util.compareIntegers(r02.i(), r03.i());
    }

    private static int compareVectors(J j4, J j6) {
        Map mapI = j4.i();
        Map mapI2 = j6.i();
        C1975d c1975dQ = ((H0) mapI.get("value")).q();
        C1975d c1975dQ2 = ((H0) mapI2.get("value")).q();
        int iCompareIntegers = Util.compareIntegers(c1975dQ.k(), c1975dQ2.k());
        return iCompareIntegers != 0 ? iCompareIntegers : compareArrays(c1975dQ, c1975dQ2);
    }

    public static boolean contains(InterfaceC1977e interfaceC1977e, H0 h0) {
        Iterator it = interfaceC1977e.a().iterator();
        while (it.hasNext()) {
            if (equals((H0) it.next(), h0)) {
                return true;
            }
        }
        return false;
    }

    public static boolean equals(H0 h0, H0 h02) {
        int iTypeOrder;
        if (h0 == h02) {
            return true;
        }
        if (h0 == null || h02 == null || (iTypeOrder = typeOrder(h0)) != typeOrder(h02)) {
            return false;
        }
        if (iTypeOrder == 2) {
            return numberEquals(h0, h02);
        }
        if (iTypeOrder == 4) {
            return ServerTimestamps.getLocalWriteTime(h0).equals(ServerTimestamps.getLocalWriteTime(h02));
        }
        if (iTypeOrder == Integer.MAX_VALUE) {
            return true;
        }
        switch (iTypeOrder) {
            case 9:
                return arrayEquals(h0, h02);
            case 10:
            case 11:
                return objectEquals(h0, h02);
            default:
                return h0.equals(h02);
        }
    }

    public static H0 getLowerBound(H0 h0) {
        switch (AbstractC2464a.c(h0.B())) {
            case 0:
                return NULL_VALUE;
            case 1:
                return MIN_BOOLEAN;
            case 2:
            case 3:
                return MIN_NUMBER;
            case 4:
                return MIN_TIMESTAMP;
            case 5:
                return MIN_STRING;
            case 6:
                return MIN_BYTES;
            case 7:
                return MIN_REFERENCE;
            case 8:
                return MIN_GEO_POINT;
            case 9:
                return MIN_ARRAY;
            case 10:
                return isVectorValue(h0) ? MIN_VECTOR_VALUE : MIN_MAP;
            default:
                throw new IllegalArgumentException("Unknown value type: ".concat(a.x(h0.B())));
        }
    }

    public static H0 getUpperBound(H0 h0) {
        switch (AbstractC2464a.c(h0.B())) {
            case 0:
                return MIN_BOOLEAN;
            case 1:
                return MIN_NUMBER;
            case 2:
            case 3:
                return MIN_TIMESTAMP;
            case 4:
                return MIN_STRING;
            case 5:
                return MIN_BYTES;
            case 6:
                return MIN_REFERENCE;
            case 7:
                return MIN_GEO_POINT;
            case 8:
                return MIN_ARRAY;
            case 9:
                return MIN_VECTOR_VALUE;
            case 10:
                return isVectorValue(h0) ? MIN_MAP : MAX_VALUE;
            default:
                throw new IllegalArgumentException("Unknown value type: ".concat(a.x(h0.B())));
        }
    }

    public static boolean isArray(H0 h0) {
        return h0 != null && h0.B() == 10;
    }

    public static boolean isDouble(H0 h0) {
        return h0 != null && h0.B() == 4;
    }

    public static boolean isInteger(H0 h0) {
        return h0 != null && h0.B() == 3;
    }

    public static boolean isMapValue(H0 h0) {
        return h0 != null && h0.B() == 11;
    }

    public static boolean isMaxValue(H0 h0) {
        return MAX_VALUE_TYPE.equals(h0.x().i().get(TYPE_KEY));
    }

    public static boolean isNanValue(H0 h0) {
        return h0 != null && Double.isNaN(h0.u());
    }

    public static boolean isNullValue(H0 h0) {
        return h0 != null && h0.B() == 1;
    }

    public static boolean isNumber(H0 h0) {
        return isInteger(h0) || isDouble(h0);
    }

    public static boolean isReferenceValue(H0 h0) {
        return h0 != null && h0.B() == 8;
    }

    public static boolean isVectorValue(H0 h0) {
        return VECTOR_VALUE_TYPE.equals(h0.x().i().get(TYPE_KEY));
    }

    public static int lowerBoundCompare(H0 h0, boolean z6, H0 h02, boolean z7) {
        int iCompare = compare(h0, h02);
        if (iCompare != 0) {
            return iCompare;
        }
        if (!z6 || z7) {
            return (z6 || !z7) ? 0 : 1;
        }
        return -1;
    }

    private static boolean numberEquals(H0 h0, H0 h02) {
        return (h0.B() == 3 && h02.B() == 3) ? h0.w() == h02.w() : h0.B() == 4 && h02.B() == 4 && Double.doubleToLongBits(h0.u()) == Double.doubleToLongBits(h02.u());
    }

    private static boolean objectEquals(H0 h0, H0 h02) {
        J jX = h0.x();
        J jX2 = h02.x();
        if (jX.h() != jX2.h()) {
            return false;
        }
        for (Map.Entry entry : jX.i().entrySet()) {
            if (!equals((H0) entry.getValue(), (H0) jX2.i().get(entry.getKey()))) {
                return false;
            }
        }
        return true;
    }

    public static H0 refValue(DatabaseId databaseId, DocumentKey documentKey) {
        G0 g0C = H0.C();
        String projectId = databaseId.getProjectId();
        String databaseId2 = databaseId.getDatabaseId();
        String string = documentKey.toString();
        StringBuilder sbI = AbstractC2761L.i("projects/", projectId, "/databases/", databaseId2, "/documents/");
        sbI.append(string);
        g0C.o(sbI.toString());
        return (H0) g0C.m359build();
    }

    public static int typeOrder(H0 h0) {
        switch (AbstractC2464a.c(h0.B())) {
            case 0:
                return 0;
            case 1:
                return 1;
            case 2:
            case 3:
                return 2;
            case 4:
                return 3;
            case 5:
                return 5;
            case 6:
                return 6;
            case 7:
                return 7;
            case 8:
                return 8;
            case 9:
                return 9;
            case 10:
                if (ServerTimestamps.isServerTimestamp(h0)) {
                    return 4;
                }
                if (isMaxValue(h0)) {
                    return Integer.MAX_VALUE;
                }
                return isVectorValue(h0) ? 10 : 11;
            default:
                throw Assert.fail("Invalid value type: ".concat(a.x(h0.B())), new Object[0]);
        }
    }

    public static int upperBoundCompare(H0 h0, boolean z6, H0 h02, boolean z7) {
        int iCompare = compare(h0, h02);
        if (iCompare != 0) {
            return iCompare;
        }
        if (!z6 || z7) {
            return (z6 || !z7) ? 0 : -1;
        }
        return 1;
    }
}
