"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stripIssueTitleProgressPrefix = stripIssueTitleProgressPrefix;
exports.renderTitleWithProgress = renderTitleWithProgress;
const catalog_1 = require("../../i18n/catalog");
const TITLE_PROGRESS_DONE_KEY = "ticketTracker.titleProgress.done";
const LINKED_ISSUE_IN_PR_PREFIX = "PR";
const STRIPPABLE_DONE_WORDS = (0, catalog_1.translateAllLocales)(TITLE_PROGRESS_DONE_KEY)
    .map((value) => value.trim())
    .filter((value) => value.length > 0)
    .map(escapeRegExp);
const STRIPPABLE_DONE_PROGRESS = `(?:(?:${STRIPPABLE_DONE_WORDS.join("|")}) )?\\d+/\\d+`;
const TITLE_PROGRESS_PREFIX = new RegExp(`^\\[(?:${LINKED_ISSUE_IN_PR_PREFIX} \\d+/\\d+|${STRIPPABLE_DONE_PROGRESS})\\]\\s+`, "iu");
function stripIssueTitleProgressPrefix(title) {
    return title.replace(TITLE_PROGRESS_PREFIX, "");
}
function renderTitleWithProgress(baseTitle, progress, options = {}) {
    if (!progress || progress.stepCount <= 0) {
        return baseTitle;
    }
    const locale = options.locale ?? catalog_1.DEFAULT_APP_LOCALE;
    const surface = options.surface ?? "issue";
    if (surface === "pull_request" && progress.isDone) {
        return baseTitle;
    }
    const showDoneLabel = surface === "issue" && progress.isDone && !options.suppressDoneLabel;
    const progressLabel = showDoneLabel
        ? `${(0, catalog_1.translate)(locale, TITLE_PROGRESS_DONE_KEY)} ${progress.stepCount}/${progress.stepCount}`
        : `${Math.max(0, Math.min(progress.completedStepCount, progress.stepCount))}/${progress.stepCount}`;
    const prefix = surface === "linked_issue_in_pr"
        ? `[${LINKED_ISSUE_IN_PR_PREFIX} ${progressLabel}]`
        : `[${progressLabel}]`;
    return `${prefix} ${baseTitle}`;
}
function escapeRegExp(value) {
    return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
