package com.google.firebase.firestore.model;

import A0.a;
import Z0.o;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/* JADX INFO: loaded from: classes3.dex */
public final class FieldPath extends BasePath<FieldPath> {
    public static final FieldPath KEY_PATH = fromSingleSegment(DocumentKey.KEY_FIELD_NAME);
    public static final FieldPath EMPTY_PATH = new FieldPath(Collections.EMPTY_LIST);

    private FieldPath(List<String> list) {
        super(list);
    }

    public static FieldPath fromSegments(List<String> list) {
        return list.isEmpty() ? EMPTY_PATH : new FieldPath(list);
    }

    public static FieldPath fromServerFormat(String str) {
        ArrayList arrayList = new ArrayList();
        StringBuilder sb = new StringBuilder();
        int i6 = 0;
        boolean z6 = false;
        while (i6 < str.length()) {
            char cCharAt = str.charAt(i6);
            if (cCharAt == '\\') {
                i6++;
                if (i6 == str.length()) {
                    throw new IllegalArgumentException("Trailing escape character is not allowed");
                }
                sb.append(str.charAt(i6));
            } else if (cCharAt == '.') {
                if (z6) {
                    sb.append(cCharAt);
                } else {
                    String string = sb.toString();
                    if (string.isEmpty()) {
                        throw new IllegalArgumentException(o.o("Invalid field path (", str, "). Paths must not be empty, begin with '.', end with '.', or contain '..'"));
                    }
                    StringBuilder sb2 = new StringBuilder();
                    arrayList.add(string);
                    sb = sb2;
                }
            } else if (cCharAt == '`') {
                z6 = !z6;
            } else {
                sb.append(cCharAt);
            }
            i6++;
        }
        String string2 = sb.toString();
        if (string2.isEmpty()) {
            throw new IllegalArgumentException(o.o("Invalid field path (", str, "). Paths must not be empty, begin with '.', end with '.', or contain '..'"));
        }
        arrayList.add(string2);
        return new FieldPath(arrayList);
    }

    public static FieldPath fromSingleSegment(String str) {
        return new FieldPath(Collections.singletonList(str));
    }

    private static boolean isValidIdentifier(String str) {
        if (str.isEmpty()) {
            return false;
        }
        char cCharAt = str.charAt(0);
        if (cCharAt != '_' && ((cCharAt < 'a' || cCharAt > 'z') && (cCharAt < 'A' || cCharAt > 'Z'))) {
            return false;
        }
        for (int i6 = 1; i6 < str.length(); i6++) {
            char cCharAt2 = str.charAt(i6);
            if (cCharAt2 != '_' && ((cCharAt2 < 'a' || cCharAt2 > 'z') && ((cCharAt2 < 'A' || cCharAt2 > 'Z') && (cCharAt2 < '0' || cCharAt2 > '9')))) {
                return false;
            }
        }
        return true;
    }

    @Override // com.google.firebase.firestore.model.BasePath
    public String canonicalString() {
        StringBuilder sb = new StringBuilder();
        for (int i6 = 0; i6 < this.segments.size(); i6++) {
            if (i6 > 0) {
                sb.append(".");
            }
            String strReplace = this.segments.get(i6).replace("\\", "\\\\").replace("`", "\\`");
            if (!isValidIdentifier(strReplace)) {
                strReplace = a.k('`', "`", strReplace);
            }
            sb.append(strReplace);
        }
        return sb.toString();
    }

    @Override // com.google.firebase.firestore.model.BasePath
    public /* bridge */ /* synthetic */ BasePath createPathWithSegments(List list) {
        return createPathWithSegments((List<String>) list);
    }

    public boolean isKeyField() {
        return equals(KEY_PATH);
    }

    @Override // com.google.firebase.firestore.model.BasePath
    public FieldPath createPathWithSegments(List<String> list) {
        return new FieldPath(list);
    }
}
