"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isManagedComment = isManagedComment;
exports.formatManagedCommandFeedbackComment = formatManagedCommandFeedbackComment;
exports.isManagedCommandFeedbackComment = isManagedCommandFeedbackComment;
const catalog_1 = require("../i18n/catalog");
const canonical_workflow_state_1 = require("./canonical-workflow-state");
const workflow_update_1 = require("./workflow-update");
const WORKFLOW_UPDATE_COMMAND_FEEDBACK_HEADING_KEY = "managedComment.commandFeedbackHeading";
const MANAGED_COMMAND_FEEDBACK_HEADING_KEYS = [
    WORKFLOW_UPDATE_COMMAND_FEEDBACK_HEADING_KEY,
    "run.followup.feedback.heading",
];
function isManagedComment(comment) {
    return ((0, canonical_workflow_state_1.isCanonicalWorkflowStateComment)(comment.body) ||
        (0, workflow_update_1.isWorkflowUpdateComment)(comment.body) ||
        isLegacyWorkflowUpdateComment(comment.body) ||
        isManagedCommandFeedbackComment(comment.body));
}
function formatManagedCommandFeedbackComment(body, language = catalog_1.DEFAULT_APP_LOCALE, headingKey = WORKFLOW_UPDATE_COMMAND_FEEDBACK_HEADING_KEY) {
    return formatManagedMarkdown(headingKey, body, language);
}
function isManagedCommandFeedbackComment(markdown) {
    return MANAGED_COMMAND_FEEDBACK_HEADING_KEYS.some((headingKey) => hasManagedCommandFeedbackHeadingPrefix(markdown, headingKey));
}
function isLegacyWorkflowUpdateComment(markdown) {
    const firstLine = markdown.trimStart().split("\n", 1)[0]?.trim() ?? "";
    if (firstLine !== "## Progress update" &&
        firstLine !== "## 進捗報告" &&
        firstLine !== "## 进度报告") {
        return false;
    }
    return /(^|\n)\s*(?:-\s*)?(?:Event|イベント|事件)\s*:/u.test(markdown);
}
function formatManagedMarkdown(headingKey, body, language) {
    const heading = renderManagedCommandFeedbackHeading((0, catalog_1.translate)(language, headingKey));
    const trimmedBody = body.trim();
    return trimmedBody.length === 0 ? heading : `${heading}\n\n${trimmedBody}`;
}
function hasManagedCommandFeedbackHeadingPrefix(markdown, headingKey) {
    const trimmed = markdown.trimStart();
    return (0, catalog_1.translateAllLocales)(headingKey).some((heading) => trimmed.startsWith(renderManagedCommandFeedbackHeading(heading)));
}
function renderManagedCommandFeedbackHeading(heading) {
    const trimmed = heading.trim();
    return /^#{1,6}\s/u.test(trimmed) ? trimmed : `## ${trimmed}`;
}
