"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderEmptyIssueSearchResults = renderEmptyIssueSearchResults;
exports.renderIssueSearchResults = renderIssueSearchResults;
const catalog_1 = require("../../i18n/catalog");
const issue_report_1 = require("../../issues/issue-report");
const types_1 = require("../../issues/types");
const types_2 = require("./types");
const format_1 = require("./format");
function renderEmptyIssueSearchResults(options, locale = catalog_1.DEFAULT_APP_LOCALE) {
    return `${(0, catalog_1.translate)(locale, "issue.command.search.empty")}${renderIssueSearchFilterSuffix(options)}`;
}
function renderIssueSearchResults(issues, options, locale = catalog_1.DEFAULT_APP_LOCALE, displayTimeZone = "UTC") {
    return options.format === types_1.ISSUE_REPORT_FORMATS.markdown
        ? renderMarkdownIssueSearchResults(issues, options, locale, displayTimeZone)
        : renderTextIssueSearchResults(issues, options, locale, displayTimeZone);
}
function renderTextIssueSearchResults(issues, options, locale, displayTimeZone) {
    const lines = [
        `${(0, catalog_1.translate)(locale, "issue.command.search.title", {
            count: issues.length,
        })}${renderIssueSearchFilterSuffix(options)}`,
    ];
    for (const [index, issue] of issues.entries()) {
        lines.push(`${index + 1}. ${issue.displayId} ${issue.title} | ${formatIssueSearchKind(issue, locale)} | ${formatIssueSearchState(issue, locale)} | ${(0, catalog_1.translate)(locale, "issue.command.report.item.updated", {
            updatedAt: (0, issue_report_1.formatIssueUpdatedAt)(issue.updatedAt, locale, displayTimeZone),
        })}`);
        lines.push(`   ${(0, catalog_1.translate)(locale, "issue.command.search.item.url", {
            value: issue.url,
        })}`);
    }
    return lines.join("\n").trimEnd();
}
function renderMarkdownIssueSearchResults(issues, options, locale, displayTimeZone) {
    const headers = [
        (0, catalog_1.translate)(locale, "issue.command.search.markdown.column.issue"),
        (0, catalog_1.translate)(locale, "issue.command.search.markdown.column.kind"),
        (0, catalog_1.translate)(locale, "issue.command.search.markdown.column.state"),
        (0, catalog_1.translate)(locale, "issue.command.search.markdown.column.updated"),
        (0, catalog_1.translate)(locale, "issue.command.search.markdown.column.title"),
    ];
    return [
        (0, catalog_1.translate)(locale, "issue.command.search.markdownTitle", {
            count: issues.length,
        }),
        "",
        `**${formatMarkdownIssueSearchCriteria(options, issues.length, locale)}**`,
        "",
        `| ${headers.join(" | ")} |`,
        `|${headers.map(() => "---").join("|")}|`,
        ...issues.map((issue) => [
            (0, format_1.formatIssueReference)(issue.displayId, issue.url, types_1.ISSUE_REPORT_FORMATS.markdown),
            (0, format_1.escapeMarkdownText)(formatIssueSearchKind(issue, locale)),
            (0, format_1.escapeMarkdownText)(formatIssueSearchState(issue, locale)),
            (0, format_1.escapeMarkdownText)((0, issue_report_1.formatIssueUpdatedAt)(issue.updatedAt, locale, displayTimeZone)),
            (0, format_1.escapeMarkdownTitle)(issue.title),
        ]
            .join(" | ")
            .replace(/^/u, "| ")
            .replace(/$/u, " |")),
    ]
        .join("\n")
        .trimEnd();
}
function formatMarkdownIssueSearchCriteria(options, count, locale) {
    return [
        (0, catalog_1.translate)(locale, `issue.command.search.criteria.mode.${options.mode}`, {
            text: (0, format_1.escapeMarkdownText)(options.text),
        }),
        (0, catalog_1.translate)(locale, `issue.command.report.criteria.state.${options.state ?? types_2.DEFAULT_ISSUE_STATE}`),
        (0, catalog_1.translate)(locale, "issue.command.report.criteria.limit", {
            limit: options.limit,
        }),
        options.includePullRequests
            ? (0, catalog_1.translate)(locale, "issue.command.search.criteria.includePrs")
            : (0, catalog_1.translate)(locale, "issue.command.search.criteria.excludePrs"),
        (0, catalog_1.translate)(locale, "issue.command.report.criteria.count", {
            count,
        }),
    ].join(" / ");
}
function renderIssueSearchFilterSuffix(options) {
    return ` [mode=${options.mode} text=${JSON.stringify(options.text)} state=${options.state ?? types_2.DEFAULT_ISSUE_STATE} limit=${options.limit} include-prs=${String(options.includePullRequests)}]`;
}
function formatIssueSearchKind(issue, locale) {
    return (0, catalog_1.translate)(locale, `issue.command.search.kind.${issue.kind}`);
}
function formatIssueSearchState(issue, locale) {
    return (0, catalog_1.translate)(locale, issue.isOpen
        ? "issue.command.search.state.open"
        : "issue.command.search.state.closed");
}
