"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createProjectRefCapabilities = createProjectRefCapabilities;
exports.buildProjectApiUrl = buildProjectApiUrl;
exports.detectNormalizedProjectRefFromRemoteUrl = detectNormalizedProjectRefFromRemoteUrl;
exports.parseProviderRemoteProjectRef = parseProviderRemoteProjectRef;
exports.parseShorthandProjectRefSegments = parseShorthandProjectRefSegments;
exports.toHostStorageSegments = toHostStorageSegments;
const git_remote_project_ref_1 = require("./git-remote-project-ref");
function createProjectRefCapabilities(input) {
    return {
        normalizeProjectRef: input.normalizeProjectRef,
        validateProjectRef(projectRef, locale) {
            input.parseProjectRef(projectRef, locale);
        },
        detectProjectRefFromRemoteUrl(remoteUrl) {
            return detectNormalizedProjectRefFromRemoteUrl(remoteUrl, input.normalizeProjectRef);
        },
        toStorageSegments: input.toStorageSegments,
    };
}
function buildProjectApiUrl(input) {
    const normalizedApiPath = input.apiPath.startsWith("/")
        ? input.apiPath
        : `/${input.apiPath}`;
    return `${input.origin}${input.apiBasePath}${normalizedApiPath}`;
}
function detectNormalizedProjectRefFromRemoteUrl(remoteUrl, normalizeProjectRef) {
    try {
        return normalizeProjectRef(remoteUrl);
    }
    catch {
        return null;
    }
}
function parseProviderRemoteProjectRef(projectRef, options) {
    const remoteProjectRef = (0, git_remote_project_ref_1.parseGitRemoteProjectRef)(projectRef);
    if (remoteProjectRef === null) {
        return null;
    }
    if (options.host !== undefined && remoteProjectRef.host !== options.host) {
        return null;
    }
    const namespaceSegmentCount = remoteProjectRef.namespaceSegments.length;
    if (options.namespaceSegments.kind === "exact" &&
        namespaceSegmentCount !== options.namespaceSegments.count) {
        return null;
    }
    if (options.namespaceSegments.kind === "minimum" &&
        namespaceSegmentCount < options.namespaceSegments.count) {
        return null;
    }
    return remoteProjectRef;
}
function parseShorthandProjectRefSegments(projectRef, options) {
    const normalized = projectRef.startsWith(options.prefix)
        ? projectRef.slice(options.prefix.length)
        : projectRef;
    const segments = normalized.split("/");
    if (segments.length < options.minSegments ||
        (options.maxSegments !== undefined &&
            segments.length > options.maxSegments) ||
        segments.some(isInvalidProjectRefPathSegment)) {
        return null;
    }
    const repo = segments[segments.length - 1] ?? "";
    if (!repo || repo.endsWith(".git")) {
        return null;
    }
    return segments;
}
function toHostStorageSegments(origin, pathSegments) {
    return [new URL(origin).host, ...pathSegments];
}
function isInvalidProjectRefPathSegment(segment) {
    return (segment.length === 0 ||
        segment.trim() !== segment ||
        segment.includes(":") ||
        segment.includes("?") ||
        segment.includes("#"));
}
