"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SKILL_RESOLUTION_ISSUE_MARKER = exports.SKILL_RESOLUTION_CONTINUATION_MARKER = exports.SKILL_RESOLUTION_SYSTEM_WORKFLOW_TEMPLATE = void 0;
exports.buildSkillResolutionIssueBody = buildSkillResolutionIssueBody;
exports.isSkillResolutionIssueBody = isSkillResolutionIssueBody;
exports.isSkillResolutionIssueDraft = isSkillResolutionIssueDraft;
exports.buildSkillResolutionProcessSection = buildSkillResolutionProcessSection;
exports.buildSkillResolutionProcessLines = buildSkillResolutionProcessLines;
exports.buildSkillResolutionDoneWhenSection = buildSkillResolutionDoneWhenSection;
exports.buildSkillResolutionDoneWhenLines = buildSkillResolutionDoneWhenLines;
exports.isSkillResolutionContinuationReason = isSkillResolutionContinuationReason;
const markdown_1 = require("../core/markdown");
const catalog_1 = require("../i18n/catalog");
exports.SKILL_RESOLUTION_SYSTEM_WORKFLOW_TEMPLATE = "__system/skill-resolution.md";
exports.SKILL_RESOLUTION_CONTINUATION_MARKER = "Skill resolution required:";
exports.SKILL_RESOLUTION_ISSUE_MARKER = "<!-- alpaca-skill-resolution-issue -->";
function buildSkillResolutionIssueBody(input) {
    return [
        exports.SKILL_RESOLUTION_ISSUE_MARKER,
        "",
        `## ${(0, catalog_1.translate)(input.locale, "skill.resolution.heading.capability")}`,
        ...(0, markdown_1.renderMarkdownCodeBlock)(input.capability, "text"),
        ...(input.originalRequest === undefined
            ? []
            : [
                "",
                `## ${(0, catalog_1.translate)(input.locale, "skill.resolution.heading.originalRequest")}`,
                ...(0, markdown_1.renderMarkdownCodeBlock)(input.originalRequest, "text"),
            ]),
        "",
        `## ${(0, catalog_1.translate)(input.locale, "skill.resolution.heading.targetWorkers")}`,
        ...formatSkillResolutionTargets(input).map((target) => `- ${target}`),
        ...(input.origin === undefined
            ? []
            : [
                "",
                `## ${(0, catalog_1.translate)(input.locale, "skill.resolution.heading.origin")}`,
                input.origin,
            ]),
        ...(input.declaredRequiredSkills === undefined ||
            input.declaredRequiredSkills.length === 0
            ? []
            : [
                "",
                `## ${(0, catalog_1.translate)(input.locale, "skill.resolution.heading.declaredRequiredSkills")}`,
                ...(0, markdown_1.renderMarkdownCodeBlock)(input.declaredRequiredSkills.join("\n"), "text"),
            ]),
        "",
        buildSkillResolutionProcessSection({ locale: input.locale }),
        "",
        buildSkillResolutionDoneWhenSection({
            locale: input.locale,
            includeParentUnblock: input.includeParentUnblock === true,
        }),
    ].join("\n");
}
function isSkillResolutionIssueBody(body) {
    return body.trimStart().startsWith(exports.SKILL_RESOLUTION_ISSUE_MARKER);
}
function isSkillResolutionIssueDraft(input) {
    const workflowSelection = input.workflowOptions?.workflowSelection;
    return (isSkillResolutionIssueBody(input.body) &&
        workflowSelection?.mode === "template" &&
        workflowSelection.templatePath === exports.SKILL_RESOLUTION_SYSTEM_WORKFLOW_TEMPLATE);
}
function buildSkillResolutionProcessSection(input) {
    return [
        `## ${(0, catalog_1.translate)(input.locale, "skill.resolution.heading.requiredAction")}`,
        ...buildSkillResolutionProcessLines({ locale: input.locale }),
    ].join("\n");
}
function buildSkillResolutionProcessLines(input) {
    return translateList(input.locale, SKILL_RESOLUTION_PROCESS_KEYS);
}
function buildSkillResolutionDoneWhenSection(options) {
    return [
        `## ${(0, catalog_1.translate)(options.locale, "skill.resolution.heading.doneWhen")}`,
        ...buildSkillResolutionDoneWhenLines(options),
    ].join("\n");
}
function buildSkillResolutionDoneWhenLines(options) {
    const lines = translateList(options.locale, SKILL_RESOLUTION_DONE_WHEN_KEYS);
    if (options.includeParentUnblock === true) {
        lines.push((0, catalog_1.translate)(options.locale, "skill.resolution.doneWhen.parentUnblocked"));
    }
    return lines;
}
const SKILL_RESOLUTION_PROCESS_KEYS = [
    "skill.resolution.process.inspectAvailable",
    "skill.resolution.process.inspectCatalog",
    "skill.resolution.process.search",
    "skill.resolution.process.installEnable",
    "skill.resolution.process.preferSmallest",
    "skill.resolution.process.askHuman",
    "skill.resolution.process.inspectDocs",
    "skill.resolution.process.installDependencies",
    "skill.resolution.process.authHandoff",
    "skill.resolution.process.verify",
    "skill.resolution.process.createLocal",
    "skill.resolution.process.updateWorkflow",
    "skill.resolution.process.lint",
];
const SKILL_RESOLUTION_DONE_WHEN_KEYS = [
    "skill.resolution.doneWhen.capabilitySatisfied",
    "skill.resolution.doneWhen.dependencies",
    "skill.resolution.doneWhen.verified",
    "skill.resolution.doneWhen.workflowRefs",
    "skill.resolution.doneWhen.lint",
];
function translateList(locale, keys) {
    return keys.map((key) => (0, catalog_1.translate)(locale, key));
}
function isSkillResolutionContinuationReason(reason) {
    return reason.trimStart().startsWith(exports.SKILL_RESOLUTION_CONTINUATION_MARKER);
}
function formatSkillResolutionTargets(input) {
    return input.target === "all" ? [...input.configuredWorkers] : [input.target];
}
