"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkerTemplateUpdateContract = buildWorkerTemplateUpdateContract;
exports.validateWorkerTemplateUpdateSemanticRules = validateWorkerTemplateUpdateSemanticRules;
const zod_1 = require("zod");
const template_contract_support_1 = require("./template-contract-support");
const workerTemplateUpdateSchema = zod_1.z
    .object(template_contract_support_1.WORKER_TEMPLATE_CONTENT_SCHEMA_SHAPE)
    .strict()
    .transform((update) => ({
    ...(0, template_contract_support_1.normalizeWorkerTemplateContentInput)(update),
}))
    .describe("Workflow template update.");
function buildWorkerTemplateUpdateContract(input) {
    return (0, template_contract_support_1.buildWorkerTemplateContract)({
        schema: workerTemplateUpdateSchema,
        example: buildWorkerTemplateUpdateExample(input),
        rules: buildWorkerTemplateUpdateRules(),
    });
}
function validateWorkerTemplateUpdateSemanticRules(update, input) {
    const issues = (0, template_contract_support_1.validateWorkerTemplateContentSemanticRules)({
        body: update.body,
        requiredSkills: update.requiredSkills,
        requiredMcp: update.requiredMcp,
        frontmatterMessage: "body must not include YAML frontmatter. Keep description, requires, targets, and autoSelect in the top-level update fields.",
        validateBodyStructure: () => validateWorkerTemplateUpdateBodyStructure(update, input),
    });
    return issues;
}
function validateWorkerTemplateUpdateBodyStructure(update, input) {
    return (0, template_contract_support_1.validateWorkerTemplateBodyStructure)({
        path: input.currentTemplate.path,
        description: update.description,
        requiredSkills: update.requiredSkills,
        requiredMcp: update.requiredMcp,
        targets: update.targets,
        autoSelect: update.autoSelect,
        body: update.body,
        digest: input.currentTemplate.digest,
        existingWorkflowTemplates: input.existingWorkflowTemplates,
        replacedTemplatePath: input.currentTemplate.path,
        maxNestedWorkflowDepth: input.maxNestedWorkflowDepth,
    });
}
function buildWorkerTemplateUpdateExample(input) {
    const exampleContent = (0, template_contract_support_1.buildLocalizedWorkerTemplateExampleContent)(input.outputLanguage);
    return {
        description: exampleContent.description,
        requires: {
            skills: [],
            mcp: [],
        },
        targets: ["issue"],
        autoSelect: true,
        body: exampleContent.body,
    };
}
function buildWorkerTemplateUpdateRules() {
    return [
        "Return exactly one JSON object in the decision block.",
        "description, requires, targets, autoSelect, and body are required.",
        "Return complete replacement values for description, requires, targets, autoSelect, and body. Do not return a diff, patch, or partial body.",
        "Use the current template as the baseline and preserve correct existing intent, constraints, nested workflow references, and wording unless the user prompt calls for changing them.",
        "Do not return path. The target workflow template path is fixed by the update command.",
        "Do not rename, move, delete, or change workflow selection for any workflow template.",
        "Do not edit repository files directly; only return the JSON update.",
        ...template_contract_support_1.WORKER_TEMPLATE_REQUIRED_CAPABILITY_RULES,
        template_contract_support_1.WORKER_TEMPLATE_LANGUAGE_RULE,
        template_contract_support_1.WORKFLOW_TEMPLATE_SOURCE_REQUEST_RULE,
        "Abstract the concrete user prompt into a reusable work type. Workflow body must define how to perform that kind of work, not the one-off task target, requested deliverable, exact issue success criteria, or concrete repository object.",
        "Use the provided incoming and outgoing workflow reference context to preserve compatibility with templates that call this workflow and templates this workflow calls.",
        template_contract_support_1.WORKER_TEMPLATE_DESCRIPTION_RULE,
        template_contract_support_1.WORKER_TEMPLATE_BODY_MARKDOWN_RULE,
        ...template_contract_support_1.WORKER_TEMPLATE_AUTHORING_GUIDE_RULES,
    ];
}
