"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkerTemplateDraftPrompt = buildWorkerTemplateDraftPrompt;
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_draft_contract_1 = require("./template-draft-contract");
function buildWorkerTemplateDraftPrompt(workerName, input, repairContext) {
    const contract = (0, template_draft_contract_1.buildWorkerTemplateDraftContract)(input);
    const isTemplateSet = input.templateSet === true;
    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 draft run.",
            taskInstruction: isTemplateSet
                ? "Draft a cohesive workflow template set. Decide which reusable templates to create, update, or reuse from the existing catalog. Do not edit files directly."
                : "Choose whether to create a new workflow template, update an existing one, or reuse an existing one. Do not edit files directly.",
            returnFieldsInstruction: isTemplateSet
                ? "Do not include YAML frontmatter in body. Return { items: [...] }, where each item is one selected action object with only the fields for that action."
                : "Do not include YAML frontmatter in body. Return the selected action object and only the fields for that action.",
            ...(input.systemPrompts === undefined
                ? {}
                : { systemPrompts: input.systemPrompts }),
            operationInstructions: [
                "Treat the explicit CLI template overrides summary as authoritative. Those overrides are fixed inputs and win if they conflict with a draft choice.",
                "Before drafting, understand the user prompt and gather only the context needed to make a reusable workflow decision. Prefer the current repository, existing workflow catalog, repository workflow-common and workflow-create system prompt guidance, and local documentation. Use web research only when the workflow depends on external or current information that cannot be verified locally.",
                "Prefer reuse_existing when an existing template already expresses the needed how-to workflow. Prefer update_existing when a close template should be improved. Use create_new only when the catalog lacks a suitable reusable workflow type.",
                "When considering nested workflow modules, inspect the existing catalog first. Prefer reuse_existing for a fitting module, update_existing for a close module that should own the reusable responsibility, and create_new only for a missing reusable responsibility boundary.",
                "When adding a nested workflow module, include only one @workflow call site for that module in the owning template; describe when it applies in prose instead of repeating the directive.",
                ...(isTemplateSet
                    ? [
                        "For template-set mode, return the smallest cohesive set needed for the reusable workflow architecture. Do not create duplicates or one template per issue step, file, bullet, phase, or generic checklist item.",
                        "For template-set paths, prefer <domain>/<role>-<scope>.md. Reuse an existing domain folder for the same purpose before creating a new folder, and avoid synonym folders for the same domain.",
                        "For update_existing in template-set mode, set targetPath to the existing template path and path to the final template path. Use a new path only when moving the template improves catalog organization or reusable responsibility ownership.",
                        "For update template-set mode, use the anchor template as the starting point and split or tighten responsibilities only when doing so improves reusable workflow boundaries.",
                        "For update template-set mode, include the anchor template as update_existing or reuse_existing so the requested update target is explicitly represented.",
                        "Use create_new for missing reusable workflow modules, update_existing for close existing modules, and reuse_existing for existing modules that already fit.",
                    ]
                    : []),
                "Do not choose reuse_existing when explicit description, requires.skills, or requires.mcp overrides were provided; choose create_new or update_existing so the CLI can apply them.",
                "The CLI creates or updates files and applies explicit template overrides as the final step.",
                "When you set path, use a normalized relative .md path with no whitespace and do not use reserved selector values such as auto, auto_select, no_template, or default.",
                ...(0, template_prompt_support_1.renderWorkflowTemplateSkillAndSelectionInstructions)({
                    noSpecificSkillsInstruction: "the workflow needs no specific skills",
                    targetSelectionInstruction: "Set targets to the workflow run targets where the template can be selected. Set autoSelect false only for reference-only templates that should not be chosen automatically.",
                }),
            ],
        }),
        repairLines: repairContext
            ? (0, prompt_repair_1.renderTemplateDraftRepairSectionLines)(repairContext)
            : undefined,
        inputLines: [
            ...(0, dedicated_prompt_support_1.renderDedicatedPromptInputLines)({
                heading: "Workflow template draft input:",
                prompt: input.prompt,
                workspace: input,
            }),
            `- Template-set mode: ${isTemplateSet ? "yes" : "no"}`,
            ...(input.anchorTemplate !== undefined
                ? [
                    "- Anchor workflow template:",
                    `  path=${input.anchorTemplate.path}`,
                    `  digest=${input.anchorTemplate.digest}`,
                    `  description=${input.anchorTemplate.description ?? prompt_text_1.PROMPT_TEXT.none}`,
                    `  requires.skills=${input.anchorTemplate.requiredSkills.length === 0
                        ? prompt_text_1.PROMPT_TEXT.none
                        : input.anchorTemplate.requiredSkills.join(", ")}`,
                    `  requires.mcp=${input.anchorTemplate.requiredMcp.length === 0
                        ? prompt_text_1.PROMPT_TEXT.none
                        : input.anchorTemplate.requiredMcp.join(", ")}`,
                    `  targets=${input.anchorTemplate.targets.join(", ")}`,
                    `  autoSelect=${input.anchorTemplate.autoSelect ? "yes" : "no"}`,
                    "  Body:",
                    "```md",
                    input.anchorTemplate.body.trim() || prompt_text_1.PROMPT_TEXT.empty,
                    "```",
                ]
                : []),
            "- Existing workflow template paths:",
            ...(0, template_prompt_support_1.renderWorkflowTemplateExistingTemplatePaths)(input.existingWorkflowTemplatePaths),
            "- Existing workflow template summaries:",
            ...(0, template_prompt_support_1.renderWorkflowTemplateCatalogEntries)(input.existingWorkflowTemplates),
            `- Max nested workflow depth: ${input.maxNestedWorkflowDepth ?? prompt_text_1.PROMPT_TEXT.none}`,
            `- ${prompt_text_1.PROMPT_TEXT.fieldExplicitTemplateOverridesSummary}:`,
            ...(0, dedicated_prompt_support_1.renderStructuredSummary)(input.explicitTemplateOverridesSummary),
        ],
        contract: {
            heading: prompt_text_1.PROMPT_TEXT.sectionTemplateDraftContract,
            rules: contract.rules,
            promptSchema: contract.promptSchema,
            example: contract.example,
        },
    });
}
