"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.appendCompletionReviewLinks = appendCompletionReviewLinks;
exports.resolvePullRequestFilesUrl = resolvePullRequestFilesUrl;
const catalog_1 = require("../../i18n/catalog");
function appendCompletionReviewLinks(input) {
    const filesUrl = resolvePullRequestFilesUrl(input.pullRequestUrl);
    if (filesUrl === null || input.summary.includes(filesUrl)) {
        return input.summary;
    }
    return [
        input.summary.trim(),
        "",
        `## ${(0, catalog_1.translate)(input.locale, "run.presentation.completionReviewLinksHeading")}`,
        "",
        `- [${(0, catalog_1.translate)(input.locale, "run.presentation.completionPrFilesLink")}](${filesUrl})`,
    ].join("\n");
}
function resolvePullRequestFilesUrl(pullRequestUrl) {
    const normalized = pullRequestUrl?.trim();
    if (!normalized) {
        return null;
    }
    let url;
    try {
        url = new URL(normalized);
    }
    catch {
        return null;
    }
    const path = url.pathname.replace(/\/+$/, "");
    if (/\/pulls?\/\d+\/files$/.test(path) ||
        /\/-\/merge_requests\/\d+\/diffs$/.test(path)) {
        return `${url.origin}${path}`;
    }
    const pullMatch = path.match(/^(.*\/pulls?\/\d+)(?:\/.*)?$/);
    if (pullMatch?.[1] !== undefined) {
        return `${url.origin}${pullMatch[1]}/files`;
    }
    const mergeRequestMatch = path.match(/^(.*\/-\/merge_requests\/\d+)(?:\/.*)?$/);
    if (mergeRequestMatch?.[1] !== undefined) {
        return `${url.origin}${mergeRequestMatch[1]}/diffs`;
    }
    return null;
}
