"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.gitlabProjectRefCapabilities = void 0;
exports.normalizeGitLabProjectRef = normalizeGitLabProjectRef;
exports.parseGitLabProjectRef = parseGitLabProjectRef;
exports.buildGitLabRemoteUrl = buildGitLabRemoteUrl;
exports.buildGitLabApiUrl = buildGitLabApiUrl;
exports.buildGitLabProjectApiBaseUrl = buildGitLabProjectApiBaseUrl;
exports.detectGitLabProjectRefFromRemoteUrl = detectGitLabProjectRefFromRemoteUrl;
exports.toGitLabStorageSegments = toGitLabStorageSegments;
const catalog_1 = require("../../i18n/catalog");
const project_ref_support_1 = require("../project-ref-support");
function normalizeGitLabProjectRef(projectRef, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const parsed = parseGitLabProjectRef(projectRef, locale);
    return `${parsed.origin}/${parsed.fullPath}`;
}
function parseGitLabProjectRef(projectRef, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const parsed = parseGitLabProjectRefInput(projectRef.trim());
    if (parsed === null) {
        throw new Error((0, catalog_1.translate)(locale, "gitlab.projectRef.invalid", { projectRef }));
    }
    return parsed;
}
function buildGitLabRemoteUrl(projectRef, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const parsed = parseGitLabProjectRef(projectRef, locale);
    return `${parsed.origin}/${parsed.fullPath}.git`;
}
function buildGitLabApiUrl(projectRef, apiPath, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const parsed = parseGitLabProjectRef(projectRef, locale);
    return (0, project_ref_support_1.buildProjectApiUrl)({
        origin: parsed.origin,
        apiBasePath: "/api/v4",
        apiPath,
    });
}
function buildGitLabProjectApiBaseUrl(projectRef, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const parsed = parseGitLabProjectRef(projectRef, locale);
    return buildGitLabApiUrl(projectRef, `/projects/${encodeURIComponent(parsed.fullPath)}`, locale);
}
function detectGitLabProjectRefFromRemoteUrl(remoteUrl) {
    return exports.gitlabProjectRefCapabilities.detectProjectRefFromRemoteUrl(remoteUrl);
}
function toGitLabStorageSegments(projectRef, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const parsed = parseGitLabProjectRef(projectRef, locale);
    return (0, project_ref_support_1.toHostStorageSegments)(parsed.origin, [
        ...parsed.namespaceSegments,
        parsed.repo,
    ]);
}
exports.gitlabProjectRefCapabilities = (0, project_ref_support_1.createProjectRefCapabilities)({
    normalizeProjectRef: normalizeGitLabProjectRef,
    parseProjectRef: parseGitLabProjectRef,
    toStorageSegments: toGitLabStorageSegments,
});
function parseGitLabProjectRefInput(projectRef) {
    const shorthandSegments = (0, project_ref_support_1.parseShorthandProjectRefSegments)(projectRef, {
        prefix: "gitlab:",
        minSegments: 2,
    });
    if (shorthandSegments !== null) {
        const repo = shorthandSegments[shorthandSegments.length - 1] ?? "";
        const namespaceSegments = shorthandSegments.slice(0, -1);
        return {
            origin: "https://gitlab.com",
            host: "gitlab.com",
            namespaceSegments,
            repo,
            fullPath: shorthandSegments.join("/"),
        };
    }
    const remoteProjectRef = (0, project_ref_support_1.parseProviderRemoteProjectRef)(projectRef, {
        namespaceSegments: { kind: "minimum", count: 1 },
    });
    if (remoteProjectRef === null) {
        return null;
    }
    return {
        origin: remoteProjectRef.origin,
        host: remoteProjectRef.host,
        namespaceSegments: remoteProjectRef.namespaceSegments,
        repo: remoteProjectRef.repo,
        fullPath: [
            ...remoteProjectRef.namespaceSegments,
            remoteProjectRef.repo,
        ].join("/"),
    };
}
