package com.google.firebase.firestore.model;

import C1.c;
import com.google.firebase.database.collection.ImmutableSortedSet;
import com.google.firebase.firestore.util.Assert;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/* JADX INFO: loaded from: classes3.dex */
public final class DocumentKey implements Comparable<DocumentKey> {
    private static final Comparator<DocumentKey> COMPARATOR;
    private static final ImmutableSortedSet<DocumentKey> EMPTY_KEY_SET;
    public static final String KEY_FIELD_NAME = "__name__";
    private final ResourcePath path;

    static {
        c cVar = new c(10);
        COMPARATOR = cVar;
        EMPTY_KEY_SET = new ImmutableSortedSet<>(Collections.EMPTY_LIST, cVar);
    }

    private DocumentKey(ResourcePath resourcePath) {
        Assert.hardAssert(isDocumentKey(resourcePath), "Not a document key path: %s", resourcePath);
        this.path = resourcePath;
    }

    public static Comparator<DocumentKey> comparator() {
        return COMPARATOR;
    }

    public static DocumentKey empty() {
        return fromSegments(Collections.EMPTY_LIST);
    }

    public static ImmutableSortedSet<DocumentKey> emptyKeySet() {
        return EMPTY_KEY_SET;
    }

    public static DocumentKey fromName(String str) {
        ResourcePath resourcePathFromString = ResourcePath.fromString(str);
        boolean z6 = false;
        if (resourcePathFromString.length() > 4 && resourcePathFromString.getSegment(0).equals("projects") && resourcePathFromString.getSegment(2).equals("databases") && resourcePathFromString.getSegment(4).equals("documents")) {
            z6 = true;
        }
        Assert.hardAssert(z6, "Tried to parse an invalid key: %s", resourcePathFromString);
        return fromPath(resourcePathFromString.popFirst(5));
    }

    public static DocumentKey fromPath(ResourcePath resourcePath) {
        return new DocumentKey(resourcePath);
    }

    public static DocumentKey fromPathString(String str) {
        return new DocumentKey(ResourcePath.fromString(str));
    }

    public static DocumentKey fromSegments(List<String> list) {
        return new DocumentKey(ResourcePath.fromSegments(list));
    }

    public static boolean isDocumentKey(ResourcePath resourcePath) {
        return resourcePath.length() % 2 == 0;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || DocumentKey.class != obj.getClass()) {
            return false;
        }
        return this.path.equals(((DocumentKey) obj).path);
    }

    public String getCollectionGroup() {
        return this.path.getSegment(r0.length() - 2);
    }

    public ResourcePath getCollectionPath() {
        return this.path.popLast();
    }

    public String getDocumentId() {
        return this.path.getLastSegment();
    }

    public ResourcePath getPath() {
        return this.path;
    }

    public boolean hasCollectionId(String str) {
        if (this.path.length() < 2) {
            return false;
        }
        ResourcePath resourcePath = this.path;
        return resourcePath.segments.get(resourcePath.length() - 2).equals(str);
    }

    public int hashCode() {
        return this.path.hashCode();
    }

    public String toString() {
        return this.path.toString();
    }

    @Override // java.lang.Comparable
    public int compareTo(DocumentKey documentKey) {
        return this.path.compareTo(documentKey.path);
    }
}
