package com.google.firebase.firestore.model;

import C1.c;
import com.google.auto.value.AutoValue;
import com.google.firebase.Timestamp;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/* JADX INFO: loaded from: classes3.dex */
@AutoValue
public abstract class FieldIndex {
    public static final int INITIAL_LARGEST_BATCH_ID = -1;
    public static final int INITIAL_SEQUENCE_NUMBER = 0;
    public static IndexState INITIAL_STATE = IndexState.create(0, IndexOffset.NONE);
    public static final Comparator<FieldIndex> SEMANTIC_COMPARATOR = new c(11);
    public static final int UNKNOWN_ID = -1;

    @AutoValue
    public static abstract class IndexOffset implements Comparable<IndexOffset> {
        public static final IndexOffset NONE = create(SnapshotVersion.NONE, DocumentKey.empty(), -1);
        public static final Comparator<MutableDocument> DOCUMENT_COMPARATOR = new c(12);

        public static IndexOffset create(SnapshotVersion snapshotVersion, DocumentKey documentKey, int i6) {
            return new AutoValue_FieldIndex_IndexOffset(snapshotVersion, documentKey, i6);
        }

        public static IndexOffset createSuccessor(SnapshotVersion snapshotVersion, int i6) {
            long seconds = snapshotVersion.getTimestamp().getSeconds();
            int nanoseconds = snapshotVersion.getTimestamp().getNanoseconds() + 1;
            return create(new SnapshotVersion(((double) nanoseconds) == 1.0E9d ? new Timestamp(seconds + 1, 0) : new Timestamp(seconds, nanoseconds)), DocumentKey.empty(), i6);
        }

        public static IndexOffset fromDocument(Document document) {
            return create(document.getReadTime(), document.getKey(), -1);
        }

        /* JADX INFO: Access modifiers changed from: private */
        public static /* synthetic */ int lambda$static$0(MutableDocument mutableDocument, MutableDocument mutableDocument2) {
            return fromDocument(mutableDocument).compareTo(fromDocument(mutableDocument2));
        }

        public abstract DocumentKey getDocumentKey();

        public abstract int getLargestBatchId();

        public abstract SnapshotVersion getReadTime();

        @Override // java.lang.Comparable
        public int compareTo(IndexOffset indexOffset) {
            int iCompareTo = getReadTime().compareTo(indexOffset.getReadTime());
            if (iCompareTo != 0) {
                return iCompareTo;
            }
            int iCompareTo2 = getDocumentKey().compareTo(indexOffset.getDocumentKey());
            return iCompareTo2 != 0 ? iCompareTo2 : Integer.compare(getLargestBatchId(), indexOffset.getLargestBatchId());
        }
    }

    @AutoValue
    public static abstract class IndexState {
        public static IndexState create(long j4, IndexOffset indexOffset) {
            return new AutoValue_FieldIndex_IndexState(j4, indexOffset);
        }

        public abstract IndexOffset getOffset();

        public abstract long getSequenceNumber();

        public static IndexState create(long j4, SnapshotVersion snapshotVersion, DocumentKey documentKey, int i6) {
            return create(j4, IndexOffset.create(snapshotVersion, documentKey, i6));
        }
    }

    @AutoValue
    public static abstract class Segment implements Comparable<Segment> {

        public enum Kind {
            ASCENDING,
            DESCENDING,
            CONTAINS
        }

        public static Segment create(FieldPath fieldPath, Kind kind) {
            return new AutoValue_FieldIndex_Segment(fieldPath, kind);
        }

        public abstract FieldPath getFieldPath();

        public abstract Kind getKind();

        @Override // java.lang.Comparable
        public int compareTo(Segment segment) {
            int iCompareTo = getFieldPath().compareTo(segment.getFieldPath());
            return iCompareTo != 0 ? iCompareTo : getKind().compareTo(segment.getKind());
        }
    }

    public static FieldIndex create(int i6, String str, List<Segment> list, IndexState indexState) {
        return new AutoValue_FieldIndex(i6, str, list, indexState);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ int lambda$static$0(FieldIndex fieldIndex, FieldIndex fieldIndex2) {
        int iCompareTo = fieldIndex.getCollectionGroup().compareTo(fieldIndex2.getCollectionGroup());
        if (iCompareTo != 0) {
            return iCompareTo;
        }
        Iterator<Segment> it = fieldIndex.getSegments().iterator();
        Iterator<Segment> it2 = fieldIndex2.getSegments().iterator();
        while (it.hasNext() && it2.hasNext()) {
            int iCompareTo2 = it.next().compareTo(it2.next());
            if (iCompareTo2 != 0) {
                return iCompareTo2;
            }
        }
        return Boolean.compare(it.hasNext(), it2.hasNext());
    }

    public Segment getArraySegment() {
        for (Segment segment : getSegments()) {
            if (segment.getKind().equals(Segment.Kind.CONTAINS)) {
                return segment;
            }
        }
        return null;
    }

    public abstract String getCollectionGroup();

    public List<Segment> getDirectionalSegments() {
        ArrayList arrayList = new ArrayList();
        for (Segment segment : getSegments()) {
            if (!segment.getKind().equals(Segment.Kind.CONTAINS)) {
                arrayList.add(segment);
            }
        }
        return arrayList;
    }

    public abstract int getIndexId();

    public abstract IndexState getIndexState();

    public abstract List<Segment> getSegments();
}
