"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildRuleLabelsLabel = buildRuleLabelsLabel;
exports.formatSummaryLine = formatSummaryLine;
exports.formatBucketLabel = formatBucketLabel;
exports.formatWorkflowState = formatWorkflowState;
exports.formatAssignees = formatAssignees;
exports.formatPullRequestDisplayId = formatPullRequestDisplayId;
exports.formatTextPullRequest = formatTextPullRequest;
exports.formatIssueReference = formatIssueReference;
exports.escapeMarkdownText = escapeMarkdownText;
exports.escapeMarkdownTitle = escapeMarkdownTitle;
exports.renderFilterSegments = renderFilterSegments;
exports.renderFilterSuffix = renderFilterSuffix;
exports.toMarkdownCodeBlock = toMarkdownCodeBlock;
exports.formatRelatedPullRequestMarkdown = formatRelatedPullRequestMarkdown;
exports.formatRelatedPullRequestText = formatRelatedPullRequestText;
const catalog_1 = require("../../i18n/catalog");
const types_1 = require("../../issues/types");
const workflow_labels_1 = require("../../platform/ticket-tracker/workflow-labels");
const types_2 = require("../../platform/workflow-state/types");
const types_3 = require("./types");
const WORKFLOW_STATUS_BY_BUCKET = {
    "ai-ready": types_2.WORKFLOW_STATUSES.aiReady,
    "ai-running": types_2.WORKFLOW_STATUSES.aiRunning,
    "ai-needs-human": types_2.WORKFLOW_STATUSES.aiNeedsHuman,
    "ai-working-in-pr": types_2.WORKFLOW_STATUSES.aiWorkingInPr,
    "ai-recurring": types_2.WORKFLOW_STATUSES.aiRecurring,
    "ai-scheduled": types_2.WORKFLOW_STATUSES.aiScheduled,
};
function buildRuleLabelsLabel(rules, workflowLabels) {
    if (rules.length === 0) {
        return "-";
    }
    return rules
        .map((rule) => {
        switch (rule) {
            case types_2.WORKFLOW_RULES.aiDisabled:
                return (0, workflow_labels_1.getWorkflowLabelForRule)(workflowLabels, rule);
            default:
                return rule;
        }
    })
        .join(", ");
}
function formatSummaryLine(summary, workflowLabels, locale) {
    return [
        (0, catalog_1.translate)(locale, "issue.command.report.summary.open", {
            count: summary.open,
        }),
        `${workflowLabels.aiReady} ${summary.aiReady}`,
        `${workflowLabels.aiRunning} ${summary.aiRunning}`,
        `${workflowLabels.aiNeedsHuman} ${summary.aiNeedsHuman}`,
        `${(0, workflow_labels_1.resolveWorkflowLabels)(workflowLabels).aiWorkingInPr} ${summary.aiWorkingInPr}`,
        `${(0, workflow_labels_1.resolveWorkflowLabels)(workflowLabels).aiRecurring} ${summary.aiRecurring}`,
        `${(0, workflow_labels_1.resolveWorkflowLabels)(workflowLabels).aiScheduled} ${summary.aiScheduled}`,
        `${workflowLabels.aiDisabled} ${summary.aiDisabled}`,
    ].join(" | ");
}
function formatBucketLabel(bucket, workflowLabels) {
    if (bucket === "ai-disabled") {
        return (0, workflow_labels_1.getWorkflowLabelForRule)(workflowLabels, types_2.WORKFLOW_RULES.aiDisabled);
    }
    return (0, workflow_labels_1.getWorkflowLabelForStatus)(workflowLabels, WORKFLOW_STATUS_BY_BUCKET[bucket]);
}
function formatWorkflowState(bucket, workflowLabels) {
    return formatBucketLabel(bucket, workflowLabels);
}
function formatAssignees(assignees) {
    return assignees.length > 0 ? assignees.join(", ") : "-";
}
function formatPullRequestDisplayId(pr) {
    return pr ? `#${pr.number}` : null;
}
function formatTextPullRequest(item, locale) {
    return (0, catalog_1.translate)(locale, "issue.command.report.item.pr", {
        value: item.prDisplayId ?? "-",
    });
}
function formatIssueReference(label, url, format) {
    if (!url || format === types_1.ISSUE_REPORT_FORMATS.text) {
        return label;
    }
    return `[${escapeMarkdownText(label)}](${url})`;
}
function escapeMarkdownText(value) {
    return value
        .replaceAll("\\", "\\\\")
        .replaceAll("*", "\\*")
        .replaceAll("_", "\\_")
        .replaceAll("[", "\\[")
        .replaceAll("]", "\\]")
        .replaceAll("`", "\\`")
        .replaceAll("|", "\\|");
}
function escapeMarkdownTitle(value) {
    return value
        .replaceAll("\\", "\\\\")
        .replaceAll("*", "\\*")
        .replaceAll("_", "\\_")
        .replaceAll("`", "\\`")
        .replaceAll("|", "\\|");
}
function renderFilterSegments(options) {
    const segments = [`state=${options.state ?? types_3.DEFAULT_ISSUE_STATE}`];
    if (options.days !== undefined) {
        segments.push(`days=${options.days}`);
    }
    if (options.labels && options.labels.length > 0) {
        segments.push(`labels=${options.labels.join(",")}`);
    }
    if (options.assignees && options.assignees.length > 0) {
        segments.push(`assignees=${options.assignees.join(",")}`);
    }
    if (options.limit !== undefined) {
        segments.push(`limit=${options.limit}`);
    }
    return segments;
}
function renderFilterSuffix(options) {
    return ` [${renderFilterSegments(options).join(" ")}]`;
}
function toMarkdownCodeBlock(body) {
    const longestBacktickFence = Math.max(3, ...[...body.matchAll(/`+/g)].map((match) => match[0].length + 1));
    const fence = "`".repeat(longestBacktickFence);
    return [fence, body, fence];
}
function formatRelatedPullRequestMarkdown(pr) {
    if (!pr) {
        return "-";
    }
    return `${formatIssueReference(`#${pr.number}`, pr.url, types_1.ISSUE_REPORT_FORMATS.markdown)} (${pr.state}, ${escapeMarkdownText(pr.headBranch)} -> ${escapeMarkdownText(pr.baseBranch)})`;
}
function formatRelatedPullRequestText(pr) {
    if (!pr) {
        return "-";
    }
    return `#${pr.number} (${pr.state}, ${pr.headBranch} -> ${pr.baseBranch}) ${pr.url}`;
}
