"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderMarkdownPullRequestDetails = renderMarkdownPullRequestDetails;
exports.renderTextPullRequestDetails = renderTextPullRequestDetails;
const catalog_1 = require("../../i18n/catalog");
const format_1 = require("./format");
function renderMarkdownPullRequestDetails(viewModel, locale) {
    const details = viewModel.prDetails ?? null;
    if (details === null) {
        return [];
    }
    return [
        ...(details.automationState === null
            ? []
            : [
                `- ${(0, catalog_1.translate)(locale, "issue.command.show.relatedPr.review", {
                    value: (0, format_1.escapeMarkdownText)(renderPullRequestAutomationState(details)),
                })}`,
            ]),
        ...(details.files.length > 0
            ? [
                `- ${(0, catalog_1.translate)(locale, "issue.command.show.relatedPr.files", {
                    value: (0, format_1.escapeMarkdownText)(renderPullRequestFiles(details.files)),
                })}`,
            ]
            : []),
        ...(details.commits.length > 0
            ? [
                `- ${(0, catalog_1.translate)(locale, "issue.command.show.relatedPr.commits", {
                    value: (0, format_1.escapeMarkdownText)(renderPullRequestCommits(details.commits)),
                })}`,
            ]
            : []),
    ];
}
function renderTextPullRequestDetails(viewModel, locale) {
    const details = viewModel.prDetails ?? null;
    if (details === null) {
        return [];
    }
    return [
        ...(details.automationState === null
            ? []
            : [
                (0, catalog_1.translate)(locale, "issue.command.show.relatedPr.review", {
                    value: renderPullRequestAutomationState(details),
                }),
            ]),
        ...(details.files.length > 0
            ? [
                (0, catalog_1.translate)(locale, "issue.command.show.relatedPr.files", {
                    value: renderPullRequestFiles(details.files),
                }),
            ]
            : []),
        ...(details.commits.length > 0
            ? [
                (0, catalog_1.translate)(locale, "issue.command.show.relatedPr.commits", {
                    value: renderPullRequestCommits(details.commits),
                }),
            ]
            : []),
    ];
}
function renderPullRequestAutomationState(details) {
    const state = details.automationState;
    if (state === null) {
        return "-";
    }
    return [
        `review=${state.reviewDecision ?? "-"}`,
        `mergeable=${state.mergeability ?? "-"}`,
        `mergeState=${state.mergeStateStatus ?? "-"}`,
        `draft=${state.isDraft ? "yes" : "no"}`,
        ...renderPullRequestAttention(state),
    ].join(" ");
}
function renderPullRequestAttention(state) {
    return state.mergeability === "CONFLICTING" ||
        state.mergeStateStatus === "DIRTY"
        ? ["attention=merge-conflict"]
        : [];
}
function renderPullRequestFiles(files) {
    return files
        .map((file) => [
        file.path,
        file.status ?? null,
        file.changes === null || file.changes === undefined
            ? null
            : `${file.changes} changes`,
    ]
        .filter((segment) => Boolean(segment))
        .join(" "))
        .join("; ");
}
function renderPullRequestCommits(commits) {
    return commits
        .map((commit) => `${commit.oid.slice(0, 12)} ${commit.title}`.trim())
        .join("; ");
}
