"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.appendParentIssueTitleSuffix = appendParentIssueTitleSuffix;
exports.appendParentIssueTitleSuffixWithinLimit = appendParentIssueTitleSuffixWithinLimit;
exports.hasParentIssueTitleSuffix = hasParentIssueTitleSuffix;
exports.replaceRecurringChildIssueTitleCoreWithinLimit = replaceRecurringChildIssueTitleCoreWithinLimit;
exports.parseParentIssueIdTitleSuffix = parseParentIssueIdTitleSuffix;
exports.renderParentIssueTitleSuffix = renderParentIssueTitleSuffix;
const catalog_1 = require("../../i18n/catalog");
const title_progress_1 = require("../../platform/ticket-tracker/title-progress");
const RECURRING_CHILD_TITLE_PREFIX_KEY = "run.recurring.child.titlePrefix";
const RECURRING_CHILD_EXECUTED_AT_SUFFIX_PATTERN = /\s(\(\d{4}-\d{2}-\d{2} \d{2}:\d{2} JST\))$/u;
function appendParentIssueTitleSuffix(title, parentDisplayId) {
    const normalizedTitle = title.trimEnd();
    const suffix = renderParentIssueTitleSuffix(parentDisplayId);
    if (normalizedTitle.length === 0 || suffix.length === 0) {
        return normalizedTitle;
    }
    const displaySuffix = ` ${suffix}`;
    return normalizedTitle.endsWith(displaySuffix)
        ? normalizedTitle
        : `${normalizedTitle}${displaySuffix}`;
}
function appendParentIssueTitleSuffixWithinLimit(title, parentDisplayId, maxLength) {
    if (maxLength <= 0) {
        return "";
    }
    const suffix = renderParentIssueTitleSuffix(parentDisplayId);
    const normalizedTitle = title.trimEnd();
    if (suffix.length === 0) {
        return truncateTitle(normalizedTitle, maxLength);
    }
    const displaySuffix = ` ${suffix}`;
    if (displaySuffix.length > maxLength) {
        return renderParentIssueTitleSuffixWithinLimit(parentDisplayId, maxLength);
    }
    const titleWithoutSuffix = normalizedTitle.endsWith(displaySuffix)
        ? normalizedTitle.slice(0, -displaySuffix.length).trimEnd()
        : normalizedTitle;
    const maxTitleLength = Math.max(0, maxLength - displaySuffix.length);
    const truncatedTitle = truncateTitle(titleWithoutSuffix, maxTitleLength);
    return truncatedTitle.length === 0
        ? suffix
        : `${truncatedTitle}${displaySuffix}`;
}
function hasParentIssueTitleSuffix(title, parentDisplayId) {
    const suffix = renderParentIssueTitleSuffix(parentDisplayId);
    return (suffix.length > 0 &&
        (0, title_progress_1.stripIssueTitleProgressPrefix)(title).trimEnd().endsWith(` ${suffix}`));
}
function replaceRecurringChildIssueTitleCoreWithinLimit(title, plannedIssueTitle, parentDisplayId, maxLength) {
    if (maxLength <= 0) {
        return null;
    }
    const normalizedPlannedIssueTitle = plannedIssueTitle.trim();
    if (normalizedPlannedIssueTitle.length === 0) {
        return null;
    }
    const suffix = renderParentIssueTitleSuffix(parentDisplayId);
    if (suffix.length === 0) {
        return null;
    }
    const displaySuffix = ` ${suffix}`;
    const baseTitle = (0, title_progress_1.stripIssueTitleProgressPrefix)(title).trimEnd();
    if (!baseTitle.endsWith(displaySuffix)) {
        return null;
    }
    const titleWithoutSuffix = baseTitle
        .slice(0, -displaySuffix.length)
        .trimEnd();
    const prefix = (0, catalog_1.translateAllLocales)(RECURRING_CHILD_TITLE_PREFIX_KEY).find((candidate) => titleWithoutSuffix.startsWith(`${candidate} `));
    if (prefix === undefined) {
        return null;
    }
    const titleAfterPrefix = titleWithoutSuffix.slice(prefix.length).trimStart();
    const executedAtMatch = titleAfterPrefix.match(RECURRING_CHILD_EXECUTED_AT_SUFFIX_PATTERN);
    const executedAtSuffix = executedAtMatch?.[1];
    if (executedAtSuffix === undefined) {
        return null;
    }
    const reservedLength = prefix.length + 1 + 1 + executedAtSuffix.length + displaySuffix.length;
    const maxPlannedIssueTitleLength = maxLength - reservedLength;
    if (maxPlannedIssueTitleLength <= 0) {
        return null;
    }
    const truncatedPlannedIssueTitle = truncateTitle(normalizedPlannedIssueTitle, maxPlannedIssueTitleLength);
    return `${prefix} ${truncatedPlannedIssueTitle} ${executedAtSuffix}${displaySuffix}`;
}
function parseParentIssueIdTitleSuffix(title) {
    const match = (0, title_progress_1.stripIssueTitleProgressPrefix)(title)
        .trimEnd()
        .match(/\s\(#([^()]+)\)$/u);
    const issueId = match?.[1]?.trim() ?? "";
    return issueId.length > 0 ? issueId : null;
}
function renderParentIssueTitleSuffix(parentDisplayId) {
    const normalizedParentDisplayId = normalizeParentDisplayId(parentDisplayId);
    return normalizedParentDisplayId.length === 0
        ? ""
        : `(${normalizedParentDisplayId})`;
}
function renderParentIssueTitleSuffixWithinLimit(parentDisplayId, maxLength) {
    const normalizedParentDisplayId = normalizeParentDisplayId(parentDisplayId);
    if (normalizedParentDisplayId.length === 0 || maxLength <= 0) {
        return "";
    }
    if (maxLength <= 2) {
        return truncateTitle(`(${normalizedParentDisplayId})`, maxLength);
    }
    return `(${truncateTitle(normalizedParentDisplayId, maxLength - 2)})`;
}
function normalizeParentDisplayId(parentDisplayId) {
    const normalized = parentDisplayId.trim();
    if (normalized.length === 0) {
        return "";
    }
    return normalized.startsWith("#") ? normalized : `#${normalized}`;
}
function truncateTitle(title, maxLength) {
    if (title.length <= maxLength) {
        return title;
    }
    if (maxLength <= 3) {
        return title.slice(0, maxLength);
    }
    return `${title.slice(0, maxLength - 3).trimEnd()}...`;
}
