"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkerTemplateUpdatePrompt = buildWorkerTemplateUpdatePrompt;
const branding_1 = require("../core/branding");
const dedicated_prompt_support_1 = require("./dedicated-prompt-support");
const prompt_repair_1 = require("./prompt-repair");
const prompt_text_1 = require("./prompt-text");
const template_prompt_support_1 = require("./template-prompt-support");
const template_update_contract_1 = require("./template-update-contract");
function buildWorkerTemplateUpdatePrompt(workerName, input, repairContext) {
    const contract = (0, template_update_contract_1.buildWorkerTemplateUpdateContract)(input);
    return (0, dedicated_prompt_support_1.renderDedicatedPromptEnvelope)({
        preambleLines: (0, template_prompt_support_1.renderWorkflowTemplatePromptPreamble)({
            workerName,
            outputLanguage: input.outputLanguage,
            runDescription: "This is a dedicated workflow template update run.",
            taskInstruction: "Update only the requested workflow template content at the fixed target path. Do not create, rename, move, or delete workflow template files, and do not change workflow selection.",
            returnFieldsInstruction: "Do not include YAML frontmatter in body. Return complete replacement description, requires, targets, autoSelect, and body fields; do not return a diff or patch.",
            ...(input.systemPrompts === undefined
                ? {}
                : { systemPrompts: input.systemPrompts }),
            operationInstructions: [
                "Use the current template as the baseline. Preserve correct existing intent, constraints, nested workflow references, and wording unless the user prompt calls for changing them.",
                "Use the incoming/outgoing workflow reference context to avoid breaking workflows that call this template and to preserve valid child workflow calls.",
                `For non-template-set updates, preserve valid @workflow calls unless the requested improvement clearly changes the reusable responsibility boundary. Do not add a new @workflow call for reference-only guidance; use a plain ${branding_1.WORKFLOW_TEMPLATES_DIRECTORY}/foo.md path in prose when the current template should only read or follow another document.`,
                "The target path is fixed by the update command. Return complete replacement values only; do not return path or workflow selection changes.",
                "Do not edit repository files directly; only return the JSON update.",
                ...(0, template_prompt_support_1.renderWorkflowTemplateSkillAndSelectionInstructions)({
                    noSpecificSkillsInstruction: "no specific skills are required",
                    targetSelectionInstruction: "Set targets to the full replacement list of workflow run targets where this template can be selected. Set autoSelect false only for reference-only templates that should not be chosen automatically.",
                }),
            ],
        }),
        repairLines: repairContext
            ? (0, prompt_repair_1.renderTemplateUpdateRepairSectionLines)(repairContext)
            : undefined,
        inputLines: [
            ...(0, dedicated_prompt_support_1.renderDedicatedPromptInputLines)({
                heading: "Workflow template update input:",
                prompt: input.prompt,
                workspace: input,
            }),
            `- Target workflow template path: ${input.currentTemplate.path}`,
            `- Target digest: ${input.currentTemplate.digest}`,
            `- Current description: ${input.currentTemplate.description ?? prompt_text_1.PROMPT_TEXT.none}`,
            "- Current requires.skills:",
            ...(0, template_prompt_support_1.renderWorkflowTemplateRequiredSkills)(input.currentTemplate.requiredSkills),
            "- Current requires.mcp:",
            ...(0, template_prompt_support_1.renderWorkflowTemplateRequiredMcp)(input.currentTemplate.requiredMcp),
            `- Current targets: ${input.currentTemplate.targets.join(", ")}`,
            `- Current auto select: ${input.currentTemplate.autoSelect ? "yes" : "no"}`,
            "- Current body:",
            "```md",
            input.currentTemplate.body.trim() || prompt_text_1.PROMPT_TEXT.empty,
            "```",
            "- Existing workflow template paths:",
            ...(0, template_prompt_support_1.renderWorkflowTemplateExistingTemplatePaths)(input.existingWorkflowTemplatePaths),
            "- Incoming workflow references to this template:",
            ...(0, template_prompt_support_1.renderWorkflowTemplateReferenceContext)(input.workflowTemplateContext?.incomingReferences),
            "- Outgoing workflow references from this template:",
            ...(0, template_prompt_support_1.renderWorkflowTemplateReferenceContext)(input.workflowTemplateContext?.outgoingReferences),
            `- Max nested workflow depth: ${input.maxNestedWorkflowDepth ?? prompt_text_1.PROMPT_TEXT.none}`,
        ],
        contract: {
            heading: "Template update contract:",
            rules: contract.rules,
            promptSchema: contract.promptSchema,
            example: contract.example,
        },
    });
}
