"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkerWorkflowTemplateFitReviewPrompt = buildWorkerWorkflowTemplateFitReviewPrompt;
const dedicated_prompt_support_1 = require("./dedicated-prompt-support");
const prompt_repair_1 = require("./prompt-repair");
const prompt_text_1 = require("./prompt-text");
const system_prompt_names_1 = require("./system-prompt-names");
const workflow_template_fit_review_contract_1 = require("./workflow-template-fit-review-contract");
function buildWorkerWorkflowTemplateFitReviewPrompt(workerName, input, repairContext) {
    const contract = (0, workflow_template_fit_review_contract_1.buildWorkerWorkflowTemplateFitReviewContract)(input);
    return (0, dedicated_prompt_support_1.renderDedicatedPromptEnvelope)({
        preambleLines: (0, dedicated_prompt_support_1.renderDedicatedPromptHeader)({
            workerName,
            runDescription: "This is a dedicated workflow template fit review run.",
            instructionLines: [
                prompt_text_1.PROMPT_TEXT.introInstructions,
                (0, prompt_text_1.buildResponseLanguageInstruction)(input.outputLanguage),
                "Review whether the selected workflow template is a safe reusable fit for the parent issue before planning begins.",
                "Do not solve the parent issue and do not draft or edit a workflow template in this run.",
                "Return use_selected only when the selected template can satisfy the parent request without narrowing, contradicting, or redirecting the requested outcome.",
                "Treat candidate requires.skills / requires.mcp as part of fit: prefer update/create when a candidate depends on unavailable, excluded, or overbroad capabilities for the requested work.",
            ],
            systemPrompts: input.systemPrompts,
            systemPromptNames: system_prompt_names_1.WORKER_COMMON_SYSTEM_PROMPT_NAMES,
        }),
        repairLines: repairContext
            ? (0, prompt_repair_1.renderWorkflowTemplateFitReviewRepairSectionLines)(repairContext)
            : undefined,
        inputLines: [
            "Workflow template fit review input:",
            ...(0, dedicated_prompt_support_1.renderDedicatedWorkspaceFields)(input),
            `- Workflow surface: ${input.workflowSurface}`,
            "- Execution issue:",
            `  - Title: ${input.issue.title}`,
            "  - Body:",
            "```text",
            input.issue.body || prompt_text_1.PROMPT_TEXT.empty,
            "```",
            ...renderOptionalIssueSnapshot("Prompt issue", input.promptIssue),
            ...renderOptionalIssueSnapshot("Linked issue", input.linkedIssue),
            "- Latest issue comments:",
            ...renderWorkflowTemplateFitReviewComments(input.latestComments),
            ...renderPromptComments(input),
            "- Selected workflow template:",
            ...renderWorkflowTemplateFitReviewSelectedTemplate(input),
            "- Candidate workflow templates:",
            ...renderWorkflowTemplateFitReviewCandidates(input),
            ...renderCapabilityAvailability(input),
        ],
        contract: {
            heading: "Workflow template fit review contract:",
            rules: contract.rules,
            promptSchema: contract.promptSchema,
            example: contract.example,
        },
    });
}
function renderOptionalIssueSnapshot(label, issue) {
    if (issue === null) {
        return [];
    }
    return [
        `- ${label}:`,
        `  - Title: ${issue.title}`,
        "  - Body:",
        "```text",
        issue.body || prompt_text_1.PROMPT_TEXT.empty,
        "```",
    ];
}
function renderPromptComments(input) {
    if (areCommentSnapshotsEqual(input.latestComments, input.promptComments)) {
        return [];
    }
    return [
        "- Prompt comments:",
        ...renderWorkflowTemplateFitReviewComments(input.promptComments),
    ];
}
function areCommentSnapshotsEqual(left, right) {
    if (left.length !== right.length) {
        return false;
    }
    return left.every((comment, index) => {
        const other = right[index];
        return (other !== undefined &&
            comment.author === other.author &&
            comment.createdAt === other.createdAt &&
            comment.body === other.body);
    });
}
function renderWorkflowTemplateFitReviewComments(comments) {
    if (comments.length === 0) {
        return [`  ${prompt_text_1.PROMPT_TEXT.none}`];
    }
    return comments.flatMap((comment) => [
        `  - ${comment.createdAt} ${comment.author}:`,
        "```text",
        comment.body || prompt_text_1.PROMPT_TEXT.empty,
        "```",
    ]);
}
function renderWorkflowTemplateFitReviewSelectedTemplate(input) {
    const template = input.selectedTemplate;
    return [
        `  - path: ${template.path}`,
        `  - description: ${renderNullableText(template.description)}`,
        `  - targets: ${template.targets.join(", ") || prompt_text_1.PROMPT_TEXT.none}`,
        `  - autoSelect: ${String(template.autoSelect)}`,
        `  - digest: ${template.digest}`,
        `  - requires.skills: ${template.requiredSkills.join(", ") || prompt_text_1.PROMPT_TEXT.none}`,
        `  - requires.mcp: ${template.requiredMcp.join(", ") || prompt_text_1.PROMPT_TEXT.none}`,
        "  - body:",
        "```markdown",
        template.body || prompt_text_1.PROMPT_TEXT.empty,
        "```",
    ];
}
function renderWorkflowTemplateFitReviewCandidates(input) {
    if (input.candidateTemplates.length === 0) {
        return [`  ${prompt_text_1.PROMPT_TEXT.none}`];
    }
    return input.candidateTemplates.map((template) => [
        `  - ${template.path}`,
        `description=${renderNullableText(template.description)}`,
        `targets=${template.targets.join(",") || prompt_text_1.PROMPT_TEXT.none}`,
        `autoSelect=${String(template.autoSelect)}`,
        `skills=${template.requiredSkills.join(",") || prompt_text_1.PROMPT_TEXT.none}`,
        `mcp=${template.requiredMcp.join(",") || prompt_text_1.PROMPT_TEXT.none}`,
    ].join(" | "));
}
function renderCapabilityAvailability(input) {
    if (input.capabilityAvailability === undefined) {
        return [];
    }
    return [
        "- Capability availability for selected and candidate requires:",
        "  - skills:",
        ...input.capabilityAvailability.skills
            .split("\n")
            .map((line) => `    ${line}`),
        "  - mcp:",
        ...input.capabilityAvailability.mcp
            .split("\n")
            .map((line) => `    ${line}`),
    ];
}
function renderNullableText(value) {
    return value === undefined || value === null ? prompt_text_1.PROMPT_TEXT.none : value;
}
