package kotlin.reflect.jvm.internal.impl.name;

import com.google.android.gms.measurement.api.AppMeasurementSdk;
import defpackage.cr4;
import defpackage.kp6;
import defpackage.n89;
import defpackage.pq6;
import defpackage.uz1;
import defpackage.vx8;
import defpackage.x15;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/* JADX INFO: loaded from: classes3.dex */
@vx8
public final class FqNameUnsafe {

    @kp6
    public static final Companion Companion = new Companion(null);

    @kp6
    private static final Name ROOT_NAME;

    @kp6
    private static final Pattern SPLIT_BY_DOTS;

    @kp6
    private final String fqName;

    @pq6
    private transient FqNameUnsafe parent;

    @pq6
    private transient FqName safe;

    @pq6
    private transient Name shortName;

    public static final class Companion {
        public /* synthetic */ Companion(uz1 uz1Var) {
            this();
        }

        @x15
        @kp6
        public final FqNameUnsafe topLevel(@kp6 Name name) {
            cr4.g(name, "shortName");
            String strAsString = name.asString();
            cr4.f(strAsString, "asString(...)");
            return new FqNameUnsafe(strAsString, FqName.ROOT.toUnsafe(), name, null);
        }

        private Companion() {
        }
    }

    static {
        Name nameSpecial = Name.special("<root>");
        cr4.f(nameSpecial, "special(...)");
        ROOT_NAME = nameSpecial;
        Pattern patternCompile = Pattern.compile("\\.");
        cr4.f(patternCompile, "compile(...)");
        SPLIT_BY_DOTS = patternCompile;
    }

    public /* synthetic */ FqNameUnsafe(String str, FqNameUnsafe fqNameUnsafe, Name name, uz1 uz1Var) {
        this(str, fqNameUnsafe, name);
    }

    private final void compute() {
        int iIndexOfLastDotWithBackticksSupport = indexOfLastDotWithBackticksSupport(this.fqName);
        if (iIndexOfLastDotWithBackticksSupport < 0) {
            this.shortName = Name.guessByFirstCharacter(this.fqName);
            this.parent = FqName.ROOT.toUnsafe();
            return;
        }
        String strSubstring = this.fqName.substring(iIndexOfLastDotWithBackticksSupport + 1);
        cr4.f(strSubstring, "substring(...)");
        this.shortName = Name.guessByFirstCharacter(strSubstring);
        String strSubstring2 = this.fqName.substring(0, iIndexOfLastDotWithBackticksSupport);
        cr4.f(strSubstring2, "substring(...)");
        this.parent = new FqNameUnsafe(strSubstring2);
    }

    private final int indexOfLastDotWithBackticksSupport(String str) {
        int length = str.length() - 1;
        boolean z = false;
        while (length >= 0) {
            char cCharAt = str.charAt(length);
            if (cCharAt == '.' && !z) {
                return length;
            }
            if (cCharAt == '`') {
                z = !z;
            } else if (cCharAt == '\\') {
                length--;
            }
            length--;
        }
        return -1;
    }

    private static final List<Name> pathSegments$collectSegmentsOf(FqNameUnsafe fqNameUnsafe) {
        if (fqNameUnsafe.isRoot()) {
            return new ArrayList();
        }
        List<Name> listPathSegments$collectSegmentsOf = pathSegments$collectSegmentsOf(fqNameUnsafe.parent());
        listPathSegments$collectSegmentsOf.add(fqNameUnsafe.shortName());
        return listPathSegments$collectSegmentsOf;
    }

    @kp6
    public final String asString() {
        return this.fqName;
    }

    @kp6
    public final FqNameUnsafe child(@kp6 Name name) {
        String strAsString;
        cr4.g(name, AppMeasurementSdk.ConditionalUserProperty.NAME);
        if (isRoot()) {
            strAsString = name.asString();
        } else {
            strAsString = this.fqName + '.' + name.asString();
        }
        cr4.d(strAsString);
        return new FqNameUnsafe(strAsString, this, name);
    }

    public boolean equals(@pq6 Object obj) {
        if (this == obj) {
            return true;
        }
        return (obj instanceof FqNameUnsafe) && cr4.b(this.fqName, ((FqNameUnsafe) obj).fqName);
    }

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

    public final boolean isRoot() {
        return this.fqName.length() == 0;
    }

    public final boolean isSafe() {
        return this.safe != null || n89.y(asString(), '<', 0, 6) < 0;
    }

    @kp6
    public final FqNameUnsafe parent() {
        FqNameUnsafe fqNameUnsafe = this.parent;
        if (fqNameUnsafe != null) {
            return fqNameUnsafe;
        }
        if (isRoot()) {
            throw new IllegalStateException("root");
        }
        compute();
        FqNameUnsafe fqNameUnsafe2 = this.parent;
        cr4.d(fqNameUnsafe2);
        return fqNameUnsafe2;
    }

    @kp6
    public final List<Name> pathSegments() {
        return pathSegments$collectSegmentsOf(this);
    }

    @kp6
    public final Name shortName() {
        Name name = this.shortName;
        if (name != null) {
            return name;
        }
        if (isRoot()) {
            throw new IllegalStateException("root");
        }
        compute();
        Name name2 = this.shortName;
        cr4.d(name2);
        return name2;
    }

    @kp6
    public final Name shortNameOrSpecial() {
        return isRoot() ? ROOT_NAME : shortName();
    }

    public final boolean startsWith(@kp6 Name name) {
        cr4.g(name, "segment");
        if (!isRoot()) {
            int iY = n89.y(this.fqName, '.', 0, 6);
            if (iY == -1) {
                iY = this.fqName.length();
            }
            int i = iY;
            String strAsString = name.asString();
            cr4.f(strAsString, "asString(...)");
            if (i == strAsString.length() && n89.ah(0, 0, i, this.fqName, strAsString, false)) {
                return true;
            }
        }
        return false;
    }

    @kp6
    public final FqName toSafe() {
        FqName fqName = this.safe;
        if (fqName != null) {
            return fqName;
        }
        FqName fqName2 = new FqName(this);
        this.safe = fqName2;
        return fqName2;
    }

    @kp6
    public String toString() {
        if (!isRoot()) {
            return this.fqName;
        }
        String strAsString = ROOT_NAME.asString();
        cr4.f(strAsString, "asString(...)");
        return strAsString;
    }

    public FqNameUnsafe(@kp6 String str, @kp6 FqName fqName) {
        cr4.g(str, "fqName");
        cr4.g(fqName, "safe");
        this.fqName = str;
        this.safe = fqName;
    }

    public FqNameUnsafe(@kp6 String str) {
        cr4.g(str, "fqName");
        this.fqName = str;
    }

    private FqNameUnsafe(String str, FqNameUnsafe fqNameUnsafe, Name name) {
        this.fqName = str;
        this.parent = fqNameUnsafe;
        this.shortName = name;
    }
}
