"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkerAiWorkflowOptionAutofillContract = buildWorkerAiWorkflowOptionAutofillContract;
exports.validateWorkerAiWorkflowOptionAutofillSemanticRules = validateWorkerAiWorkflowOptionAutofillSemanticRules;
const zod_1 = require("zod");
const workflow_option_contract_support_1 = require("./workflow-option-contract-support");
const workflow_schedule_contract_support_1 = require("./workflow-schedule-contract-support");
const structured_contract_support_1 = require("./structured-contract-support");
const workflow_option_normalizer_1 = require("../workflows/workflow-option-normalizer");
const validation_issues_1 = require("./validation-issues");
const workerAiWorkflowOptionAutofillWorkflowSchema = zod_1.z
    .object(workflow_option_contract_support_1.workerWorkflowOptionalMetadataShape)
    .strict()
    .describe("Workflow metadata autofill. Template selection fields are intentionally excluded.");
const workerAiWorkflowOptionAutofillSchema = zod_1.z
    .object({
    workflow: workerAiWorkflowOptionAutofillWorkflowSchema
        .nullable()
        .describe("Autofilled workflow options, or null when none are requested."),
})
    .strict()
    .describe("AI workflow option autofill result.");
function buildWorkerAiWorkflowOptionAutofillContract(input) {
    return (0, structured_contract_support_1.buildWorkerStructuredContract)({
        schema: workerAiWorkflowOptionAutofillSchema,
        example: buildWorkerAiWorkflowOptionAutofillExample(input),
        rules: buildWorkerAiWorkflowOptionAutofillRules(),
    });
}
function validateWorkerAiWorkflowOptionAutofillSemanticRules(autofill, input) {
    if (autofill.workflow === null) {
        return [];
    }
    const issues = (0, workflow_option_contract_support_1.validateWorkerWorkflowOptionSemanticRules)(autofill.workflow, input);
    if (issues.length === 0 &&
        !(0, workflow_option_normalizer_1.hasWorkflowCommandUpdateFieldValue)({
            schedule: autofill.workflow.schedule,
            scheduledRun: autofill.workflow.scheduledRun,
            firstRun: autofill.workflow.firstRun,
            recurringAutoStop: autofill.workflow.recurringAutoStop,
            configOverridePatch: (0, workflow_option_contract_support_1.buildWorkerWorkflowConfigOverridePatch)(autofill.workflow.overrides),
            baseBranch: autofill.workflow.overrides?.baseBranch === undefined ||
                autofill.workflow.overrides.baseBranch === null
                ? undefined
                : { mode: "set", branch: autofill.workflow.overrides.baseBranch },
        })) {
        (0, validation_issues_1.addWorkerSemanticValidationIssue)(issues, {
            path: ["workflow"],
            message: "workflow must be null or include at least one effective workflow option.",
        });
    }
    return issues;
}
function buildWorkerAiWorkflowOptionAutofillExample(_input) {
    return {
        workflow: {
            overrides: {
                followup: {
                    proposalPolicy: "if_needed",
                },
            },
        },
    };
}
function buildWorkerAiWorkflowOptionAutofillRules() {
    const rules = [
        "Return exactly one JSON object in the decision block.",
        "workflow is required and must be null when the prompt does not request workflow options.",
        "When workflow is an object, include only workflow options that are requested or strongly implied by the prompt.",
        "When workflow is an object, it must include at least one effective workflow option.",
        "Do not emit workflow template selector fields; the selected workflow template is read-only context for this autofill pass.",
        ...(0, workflow_schedule_contract_support_1.buildWorkerWorkflowAutomationIntentPromptRules)({
            includeIssueBodyUnknownsRule: false,
        }),
        ...(0, workflow_schedule_contract_support_1.buildWorkerWorkflowSchedulePromptRules)({
            includeScheduledRunRules: true,
            includeFirstRunRules: true,
            includeAcceptedAtRule: true,
        }),
        ...(0, workflow_option_contract_support_1.buildWorkerWorkflowOptionPromptRules)([
            "workflow.overrides is optional and must be an object or null. Omit override fields or use null when no override is needed.",
        ]),
    ];
    return rules;
}
