"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseGitRemoteProjectRef = parseGitRemoteProjectRef;
const GIT_PLUS_PREFIX = "git+";
function parseGitRemoteProjectRef(projectRef) {
    const trimmed = projectRef.trim();
    if (!trimmed) {
        return null;
    }
    const hasGitPlusPrefix = trimmed.startsWith(GIT_PLUS_PREFIX);
    const remoteRef = hasGitPlusPrefix
        ? trimmed.slice(GIT_PLUS_PREFIX.length)
        : trimmed;
    if (hasGitPlusPrefix &&
        !remoteRef.startsWith("http://") &&
        !remoteRef.startsWith("https://") &&
        !remoteRef.startsWith("ssh://")) {
        return null;
    }
    return (parseUrlRemoteProjectRef(remoteRef) ?? parseScpRemoteProjectRef(remoteRef));
}
function parseUrlRemoteProjectRef(projectRef) {
    let url;
    try {
        url = new URL(projectRef);
    }
    catch {
        return null;
    }
    if (url.search || url.hash || url.password || !url.host) {
        return null;
    }
    const protocolKind = parseRemoteProtocol(url.protocol);
    if (protocolKind === null) {
        return null;
    }
    if (protocolKind === "ssh") {
        if (url.username !== "git") {
            return null;
        }
    }
    else if (url.username) {
        return null;
    }
    const host = protocolKind === "ssh"
        ? url.hostname.toLowerCase()
        : url.host.toLowerCase();
    return buildGitRemoteProjectRef({
        protocolKind,
        host,
        origin: protocolKind === "ssh" ? `https://${host}` : `${protocolKind}://${host}`,
        path: url.pathname,
    });
}
function parseScpRemoteProjectRef(projectRef) {
    const match = projectRef.match(/^([^@\s:]+)@([^:\s/]+):(.+)$/);
    if (!match) {
        return null;
    }
    const [, user, rawHost, path] = match;
    if (user !== "git" || !rawHost || !path || path.startsWith("/")) {
        return null;
    }
    const host = rawHost.toLowerCase();
    return buildGitRemoteProjectRef({
        protocolKind: "scp",
        host,
        origin: `https://${host}`,
        path,
    });
}
function parseRemoteProtocol(protocol) {
    switch (protocol) {
        case "http:":
            return "http";
        case "https:":
            return "https";
        case "ssh:":
            return "ssh";
        default:
            return null;
    }
}
function buildGitRemoteProjectRef(input) {
    const pathSegments = parseRemotePathSegments(input.path);
    if (pathSegments === null) {
        return null;
    }
    return {
        protocolKind: input.protocolKind,
        host: input.host,
        origin: input.origin,
        namespaceSegments: pathSegments.slice(0, -1),
        repo: pathSegments[pathSegments.length - 1] ?? "",
        pathSegments,
    };
}
function parseRemotePathSegments(path) {
    if (path.includes("?") || path.includes("#")) {
        return null;
    }
    const segments = path.split("/");
    if (segments[0] === "") {
        segments.shift();
    }
    if (segments[segments.length - 1] === "") {
        segments.pop();
    }
    if (segments.length < 2 ||
        segments.some((segment) => segment.length === 0 || segment.trim() !== segment)) {
        return null;
    }
    const repo = stripGitSuffix(segments[segments.length - 1] ?? "");
    if (!repo) {
        return null;
    }
    return [...segments.slice(0, -1), repo];
}
function stripGitSuffix(repo) {
    return repo.replace(/\.git$/i, "");
}
