"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildRequiredSkillsPreflightResolution = buildRequiredSkillsPreflightResolution;
exports.buildRequiredMcpPreflightResolution = buildRequiredMcpPreflightResolution;
exports.buildMissingWorkflowTemplatePreflightResolution = buildMissingWorkflowTemplatePreflightResolution;
exports.buildWorkflowTemplateFitPreflightResolution = buildWorkflowTemplateFitPreflightResolution;
const markdown_1 = require("../../core/markdown");
const catalog_1 = require("../../i18n/catalog");
const resolution_issue_1 = require("../../mcp/resolution-issue");
const resolution_issue_2 = require("../../skills/resolution-issue");
const preflight_resolution_marker_1 = require("./preflight-resolution-marker");
function buildRequiredSkillsPreflightResolution(input) {
    const resolutionKey = input.requiredSkillRefs.join(",");
    return {
        resolutionKind: "required-skills",
        resolutionKey,
        title: (0, catalog_1.translate)(input.locale, "run.preflight.requiredSkills.title", {
            displayId: input.parentIssue.displayId,
            skillRefs: input.requiredSkillRefs.join(", "),
        }),
        body: buildRequiredSkillsPreflightResolutionBody({
            parentIssue: input.parentIssue,
            workflowTemplateName: input.workflowTemplateName,
            requiredSkillRefs: input.requiredSkillRefs,
            resolutionKey,
            locale: input.locale,
        }),
        summary: input.summary,
        next: (0, catalog_1.translate)(input.locale, "run.preflight.requiredSkills.next"),
    };
}
function buildRequiredMcpPreflightResolution(input) {
    const resolutionKey = input.requiredMcpRefs.join(",");
    return {
        resolutionKind: "required-mcp",
        resolutionKey,
        title: (0, catalog_1.translate)(input.locale, "run.preflight.requiredMcp.title", {
            displayId: input.parentIssue.displayId,
            mcpRefs: input.requiredMcpRefs.join(", "),
        }),
        body: buildRequiredMcpPreflightResolutionBody({
            parentIssue: input.parentIssue,
            workflowTemplateName: input.workflowTemplateName,
            requiredMcpRefs: input.requiredMcpRefs,
            resolutionKey,
            locale: input.locale,
        }),
        summary: input.summary,
        next: (0, catalog_1.translate)(input.locale, "run.preflight.requiredMcp.next"),
    };
}
function buildMissingWorkflowTemplatePreflightResolution(input) {
    const resolutionKey = `workflow-template:${input.target}`;
    return {
        resolutionKind: "missing-workflow-template",
        resolutionKey,
        title: (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.title", {
            displayId: input.parentIssue.displayId,
            target: input.target,
            targetLabel: resolveMissingWorkflowTemplateTargetLabel(input.locale, input.target),
        }),
        body: buildMissingWorkflowTemplatePreflightResolutionBody({
            parentIssue: input.parentIssue,
            target: input.target,
            resolutionKey,
            locale: input.locale,
        }),
        summary: input.summary,
        next: (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.next"),
    };
}
function buildWorkflowTemplateFitPreflightResolution(input) {
    const resolutionKey = [
        "workflow-template-fit",
        input.workflowSurface,
        input.action,
        input.selectedTemplatePath,
        input.selectedTemplateDigest,
    ].join(":");
    return {
        resolutionKind: "workflow-template-fit",
        resolutionKey,
        title: (0, catalog_1.translate)(input.locale, "run.preflight.workflowFit.title", {
            displayId: input.parentIssue.displayId,
            template: input.selectedTemplatePath,
        }),
        body: buildWorkflowTemplateFitPreflightResolutionBody({
            ...input,
            resolutionKey,
        }),
        summary: input.summary,
        next: (0, catalog_1.translate)(input.locale, "run.preflight.workflowFit.next"),
    };
}
function resolveMissingWorkflowTemplateTargetLabel(locale, target) {
    switch (target) {
        case "issue":
        case "pull_request":
        case "unplanned":
        case "followup":
        case "merge":
            return (0, catalog_1.translate)(locale, `run.preflight.missingWorkflow.targetLabel.${target}`);
        default:
            return target;
    }
}
function buildMissingWorkflowTemplatePreflightResolutionBody(input) {
    const parentRequestSnapshot = formatParentRequestSnapshot({
        parentIssue: input.parentIssue,
        locale: input.locale,
    });
    const prompt = buildMissingWorkflowTemplatePrompt({
        target: input.target,
        parentRequestSnapshot,
        locale: input.locale,
    });
    const promptPlaceholder = (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.prompt.placeholder");
    return [
        (0, preflight_resolution_marker_1.buildPreflightResolutionMarkerBlock)({
            resolutionKind: "missing-workflow-template",
            parentIssueId: input.parentIssue.id,
            resolutionKey: input.resolutionKey,
        }),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.parentIssue"),
        input.parentIssue.url,
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.parentSnapshot"),
        ...(0, markdown_1.renderMarkdownDetails)({
            summary: (0, catalog_1.translate)(input.locale, "run.preflight.common.details.show"),
            lines: (0, markdown_1.renderMarkdownCodeBlock)(parentRequestSnapshot, "text"),
        }),
        "",
        sectionHeading(input.locale, "run.preflight.missingWorkflow.heading.target"),
        ...(0, markdown_1.renderMarkdownCodeBlock)(input.target, "text"),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.requiredAction"),
        (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.action.intro"),
        "",
        (0, catalog_1.translate)(input.locale, "run.preflight.workflowTemplateMaintenance.action.commandOnly"),
        "",
        (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.action.stepList"),
        "",
        ...(0, markdown_1.renderMarkdownCodeBlock)(`alpaca workflow list --target ${input.target}`, "sh"),
        "",
        (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.action.stepCreate", {
            placeholder: promptPlaceholder,
        }),
        "",
        ...(0, markdown_1.renderMarkdownCodeBlock)([
            `alpaca workflow create --template-set --prompt "<${promptPlaceholder}>" --save-draft`,
        ].join("\n"), "sh"),
        "",
        (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.action.stepUpdate", {
            placeholder: promptPlaceholder,
        }),
        "",
        ...(0, markdown_1.renderMarkdownCodeBlock)([
            `alpaca workflow update --template-set --path <existing.md> --prompt "<${promptPlaceholder}>" --save-draft`,
        ].join("\n"), "sh"),
        "",
        (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.action.stepApply"),
        "",
        ...(0, markdown_1.renderMarkdownCodeBlock)([
            "alpaca workflow create --apply-draft <path>",
            "alpaca workflow update --apply-draft <path>",
            "alpaca workflow lint",
        ].join("\n"), "sh"),
        "",
        sectionHeading(input.locale, "run.preflight.missingWorkflow.heading.prompt"),
        ...(0, markdown_1.renderMarkdownDetails)({
            summary: (0, catalog_1.translate)(input.locale, "run.preflight.common.details.show"),
            lines: (0, markdown_1.renderMarkdownCodeBlock)(prompt, "text"),
        }),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.doneWhen"),
        ...translateList(input.locale, [
            [
                "run.preflight.missingWorkflow.doneWhen.templateAvailable",
                { target: input.target },
            ],
            ["run.preflight.missingWorkflow.doneWhen.goalMatches", {}],
            ["run.preflight.missingWorkflow.doneWhen.reusable", {}],
            ["run.preflight.common.doneWhen.workflowLint", {}],
            ["run.preflight.common.doneWhen.parentUnblocked", {}],
        ]),
    ].join("\n");
}
function buildWorkflowTemplateFitPreflightResolutionBody(input) {
    const parentRequestSnapshot = formatParentRequestSnapshot({
        parentIssue: input.parentIssue,
        locale: input.locale,
    });
    const promptPlaceholder = (0, catalog_1.translate)(input.locale, "run.preflight.workflowFit.prompt.placeholder");
    return [
        (0, preflight_resolution_marker_1.buildPreflightResolutionMarkerBlock)({
            resolutionKind: "workflow-template-fit",
            parentIssueId: input.parentIssue.id,
            resolutionKey: input.resolutionKey,
        }),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.parentIssue"),
        input.parentIssue.url,
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.parentSnapshot"),
        ...(0, markdown_1.renderMarkdownDetails)({
            summary: (0, catalog_1.translate)(input.locale, "run.preflight.common.details.show"),
            lines: (0, markdown_1.renderMarkdownCodeBlock)(parentRequestSnapshot, "text"),
        }),
        "",
        sectionHeading(input.locale, "run.preflight.workflowFit.heading.review"),
        (0, catalog_1.translate)(input.locale, "run.preflight.workflowFit.review.action", {
            action: input.action,
        }),
        (0, catalog_1.translate)(input.locale, "run.preflight.workflowFit.review.reason", {
            reason: input.reason,
        }),
        ...input.evidence.map((item) => `- ${item}`),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.workflowTemplate"),
        ...(0, markdown_1.renderMarkdownCodeBlock)([
            `path: ${input.selectedTemplatePath}`,
            `digest: ${input.selectedTemplateDigest}`,
            `surface: ${input.workflowSurface}`,
        ].join("\n"), "text"),
        ...(0, markdown_1.renderMarkdownDetails)({
            summary: (0, catalog_1.translate)(input.locale, "run.preflight.common.details.show"),
            lines: (0, markdown_1.renderMarkdownCodeBlock)(input.selectedTemplateBody, "markdown"),
        }),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.requiredAction"),
        (0, catalog_1.translate)(input.locale, input.action === "create_new"
            ? "run.preflight.workflowFit.action.create"
            : "run.preflight.workflowFit.action.update"),
        "",
        (0, catalog_1.translate)(input.locale, "run.preflight.workflowTemplateMaintenance.action.commandOnly"),
        "",
        ...(0, markdown_1.renderMarkdownCodeBlock)([`alpaca workflow list --target ${input.workflowSurface}`].join("\n"), "sh"),
        "",
        ...(0, markdown_1.renderMarkdownCodeBlock)(input.action === "create_new"
            ? `alpaca workflow create --template-set --prompt "<${promptPlaceholder}>" --save-draft`
            : `alpaca workflow update --template-set --path ${input.selectedTemplatePath} --prompt "<${promptPlaceholder}>" --save-draft`, "sh"),
        "",
        ...(0, markdown_1.renderMarkdownCodeBlock)([
            "alpaca workflow create --apply-draft <path>",
            "alpaca workflow update --apply-draft <path>",
            "alpaca workflow lint",
        ].join("\n"), "sh"),
        "",
        sectionHeading(input.locale, "run.preflight.workflowFit.heading.prompt"),
        ...(0, markdown_1.renderMarkdownDetails)({
            summary: (0, catalog_1.translate)(input.locale, "run.preflight.common.details.show"),
            lines: (0, markdown_1.renderMarkdownCodeBlock)(input.recommendedPrompt, "text"),
        }),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.doneWhen"),
        ...translateList(input.locale, [
            ["run.preflight.workflowFit.doneWhen.fitResolved", {}],
            ["run.preflight.workflowFit.doneWhen.reselectable", {}],
            ["run.preflight.common.doneWhen.workflowLint", {}],
            ["run.preflight.common.doneWhen.parentUnblocked", {}],
        ]),
    ].join("\n");
}
function buildRequiredSkillsPreflightResolutionBody(input) {
    return [
        (0, preflight_resolution_marker_1.buildPreflightResolutionMarkerBlock)({
            resolutionKind: "required-skills",
            parentIssueId: input.parentIssue.id,
            resolutionKey: input.resolutionKey,
        }),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.parentIssue"),
        input.parentIssue.url,
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.workflowTemplate"),
        ...(0, markdown_1.renderMarkdownCodeBlock)(input.workflowTemplateName ??
            (0, catalog_1.translate)(input.locale, "run.preflight.common.placeholder.none"), "text"),
        "",
        sectionHeading(input.locale, "run.preflight.requiredSkills.heading.refs"),
        ...(0, markdown_1.renderMarkdownCodeBlock)(input.requiredSkillRefs.join("\n"), "text"),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.requiredAction"),
        (0, catalog_1.translate)(input.locale, "run.preflight.requiredSkills.action.intro"),
        "",
        ...(0, markdown_1.renderMarkdownCodeBlock)([
            "alpaca capability list",
            'alpaca capability search "<required skill capability>" --installable-only',
            "alpaca skill list",
            "alpaca skill catalog list",
            'alpaca skill search "<required skill capability>" --installable-only',
            'alpaca skill install --search "<required skill capability>" --for <worker-id>',
            "alpaca workflow lint",
        ].join("\n"), "sh"),
        "",
        ...(0, markdown_1.renderMarkdownDetails)({
            summary: (0, catalog_1.translate)(input.locale, "run.preflight.common.details.checklist"),
            lines: [
                ...(0, resolution_issue_2.buildSkillResolutionProcessLines)({ locale: input.locale }),
                (0, catalog_1.translate)(input.locale, "run.preflight.requiredSkills.process.bareRefs"),
                (0, catalog_1.translate)(input.locale, "run.preflight.requiredSkills.process.providerRefs"),
                (0, catalog_1.translate)(input.locale, "run.preflight.requiredSkills.process.incompatibleRefs"),
            ],
        }),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.doneWhen"),
        ...(0, resolution_issue_2.buildSkillResolutionDoneWhenLines)({ locale: input.locale }),
        (0, catalog_1.translate)(input.locale, "run.preflight.requiredSkills.doneWhen.refsUsable"),
        (0, catalog_1.translate)(input.locale, "run.preflight.common.doneWhen.parentUnblocked"),
    ].join("\n");
}
function buildRequiredMcpPreflightResolutionBody(input) {
    return [
        (0, preflight_resolution_marker_1.buildPreflightResolutionMarkerBlock)({
            resolutionKind: "required-mcp",
            parentIssueId: input.parentIssue.id,
            resolutionKey: input.resolutionKey,
        }),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.parentIssue"),
        input.parentIssue.url,
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.workflowTemplate"),
        ...(0, markdown_1.renderMarkdownCodeBlock)(input.workflowTemplateName ??
            (0, catalog_1.translate)(input.locale, "run.preflight.common.placeholder.none"), "text"),
        "",
        sectionHeading(input.locale, "run.preflight.requiredMcp.heading.refs"),
        ...(0, markdown_1.renderMarkdownCodeBlock)(input.requiredMcpRefs.join("\n"), "text"),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.requiredAction"),
        (0, catalog_1.translate)(input.locale, "run.preflight.requiredMcp.action.intro"),
        "",
        ...(0, markdown_1.renderMarkdownCodeBlock)([
            "alpaca capability list",
            'alpaca capability search "<capability>"',
            "alpaca mcp list",
            "alpaca mcp catalog list --include-system",
            'alpaca mcp search "<mcp capability>"',
            "alpaca mcp verify --ref <ref>",
            "alpaca workflow lint",
        ].join("\n"), "sh"),
        "",
        ...(0, markdown_1.renderMarkdownDetails)({
            summary: (0, catalog_1.translate)(input.locale, "run.preflight.common.details.checklist"),
            lines: (0, resolution_issue_1.buildMcpResolutionProcessLines)({ locale: input.locale }),
        }),
        "",
        sectionHeading(input.locale, "run.preflight.common.heading.doneWhen"),
        ...(0, resolution_issue_1.buildMcpResolutionDoneWhenLines)({ locale: input.locale }),
        (0, catalog_1.translate)(input.locale, "run.preflight.requiredMcp.doneWhen.refsVisible"),
        (0, catalog_1.translate)(input.locale, "run.preflight.requiredMcp.doneWhen.humanSetup"),
        (0, catalog_1.translate)(input.locale, "run.preflight.common.doneWhen.workflowLint"),
        (0, catalog_1.translate)(input.locale, "run.preflight.common.doneWhen.parentUnblocked"),
    ].join("\n");
}
function buildMissingWorkflowTemplatePrompt(input) {
    return [
        (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.prompt.goal", {
            target: input.target,
        }),
        (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.prompt.source"),
        (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.prompt.abstract"),
        (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.prompt.notThisIssue"),
        (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.prompt.preferReuse"),
        "",
        (0, catalog_1.translate)(input.locale, "run.preflight.missingWorkflow.prompt.snapshot"),
        input.parentRequestSnapshot,
    ].join("\n");
}
function formatParentRequestSnapshot(input) {
    return [
        (0, catalog_1.translate)(input.locale, "run.preflight.parentSnapshot.title", {
            displayId: input.parentIssue.displayId,
            title: input.parentIssue.title,
        }),
        (0, catalog_1.translate)(input.locale, "run.preflight.parentSnapshot.url", {
            url: input.parentIssue.url,
        }),
        "",
        (0, catalog_1.translate)(input.locale, "run.preflight.parentSnapshot.body"),
        input.parentIssue.body.trim() ||
            (0, catalog_1.translate)(input.locale, "run.preflight.parentSnapshot.emptyBody"),
    ].join("\n");
}
function sectionHeading(locale, key) {
    return `## ${(0, catalog_1.translate)(locale, key)}`;
}
function translateList(locale, items) {
    return items.map(([key, variables]) => (0, catalog_1.translate)(locale, key, variables));
}
