"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.giteaProjectRefCapabilities = void 0;
exports.normalizeGiteaProjectRef = normalizeGiteaProjectRef;
exports.parseGiteaProjectRef = parseGiteaProjectRef;
exports.buildGiteaRemoteUrl = buildGiteaRemoteUrl;
exports.buildGiteaApiUrl = buildGiteaApiUrl;
exports.detectGiteaProjectRefFromRemoteUrl = detectGiteaProjectRefFromRemoteUrl;
exports.toGiteaStorageSegments = toGiteaStorageSegments;
const catalog_1 = require("../../i18n/catalog");
const project_ref_support_1 = require("../project-ref-support");
function normalizeGiteaProjectRef(projectRef, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const { origin, owner, repo } = parseGiteaProjectRef(projectRef, locale);
    return `${origin}/${owner}/${repo}`;
}
function parseGiteaProjectRef(projectRef, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const remoteProjectRef = (0, project_ref_support_1.parseProviderRemoteProjectRef)(projectRef, {
        namespaceSegments: { kind: "exact", count: 1 },
    });
    if (remoteProjectRef === null) {
        throw new Error((0, catalog_1.translate)(locale, "gitea.projectRef.invalid", { projectRef }));
    }
    const [owner = ""] = remoteProjectRef.namespaceSegments;
    return {
        origin: remoteProjectRef.origin,
        owner,
        repo: remoteProjectRef.repo,
    };
}
function buildGiteaRemoteUrl(projectRef, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const { origin, owner, repo } = parseGiteaProjectRef(projectRef, locale);
    return `${origin}/${owner}/${repo}.git`;
}
function buildGiteaApiUrl(projectRef, apiPath, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const { origin } = parseGiteaProjectRef(projectRef, locale);
    return (0, project_ref_support_1.buildProjectApiUrl)({
        origin,
        apiBasePath: "/api/v1",
        apiPath,
    });
}
function detectGiteaProjectRefFromRemoteUrl(remoteUrl) {
    return exports.giteaProjectRefCapabilities.detectProjectRefFromRemoteUrl(remoteUrl);
}
function toGiteaStorageSegments(projectRef, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const { origin, owner, repo } = parseGiteaProjectRef(projectRef, locale);
    return (0, project_ref_support_1.toHostStorageSegments)(origin, [owner, repo]);
}
exports.giteaProjectRefCapabilities = (0, project_ref_support_1.createProjectRefCapabilities)({
    normalizeProjectRef: normalizeGiteaProjectRef,
    parseProjectRef: parseGiteaProjectRef,
    toStorageSegments: toGiteaStorageSegments,
});
