"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveWorkerWorkflowSelectionDecision = resolveWorkerWorkflowSelectionDecision;
exports.applyWorkerWorkflowSelectionToState = applyWorkerWorkflowSelectionToState;
exports.toWorkflowTemplateSelector = toWorkflowTemplateSelector;
const types_1 = require("./types");
const workflow_selection_fields_1 = require("./workflow-selection-fields");
const workflow_selection_validation_1 = require("./workflow-selection-validation");
const state_1 = require("../workflows/state");
const pre_plan_state_1 = require("../workflows/pre-plan-state");
const template_bindings_1 = require("../workflows/template-bindings");
function resolveWorkerWorkflowSelectionDecision(input) {
    const selectionContext = input.workflow.selection;
    const selection = input.decision.selection;
    if (input.workflow.mode !== "workflow_select" ||
        selectionContext === null ||
        selectionContext === undefined ||
        selection === undefined) {
        throw new Error("Workflow selection decision requires workflow_select context.");
    }
    assertWorkflowSelectionDecision({ selectionContext, selection });
    return {
        templateName: (0, workflow_selection_fields_1.getActiveWorkflowSelectionTemplateName)(selection),
        selection,
        requestedTemplate: (0, workflow_selection_fields_1.getActiveWorkflowSelectionAction)(selection) ===
            types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.requestTemplate,
    };
}
function applyWorkerWorkflowSelectionToState(input) {
    const workflowState = input.workflowState;
    if ((0, workflow_selection_fields_1.getActiveWorkflowSelectionAction)(input.selection) ===
        types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.requestTemplate) {
        throw new Error("request_template selection cannot be applied to state.");
    }
    let workflowTemplates = workflowState?.workflowTemplates ??
        (0, template_bindings_1.createUnresolvedWorkflowTemplateBindings)();
    const templateName = (0, workflow_selection_fields_1.getActiveWorkflowSelectionTemplateName)(input.selection);
    workflowTemplates = setSelectedWorkflowTemplateBindingForAutoSlot({
        workflowTemplates,
        surface: input.workflowSurface,
        slotMode: (0, workflow_selection_fields_1.getActiveWorkflowSelectionSlotMode)(input.selectionContext),
        templateName: templateName ?? null,
    });
    return (0, pre_plan_state_1.buildSelectionTransitionWorkflowState)({
        previousState: workflowState,
        workflowSurface: input.workflowSurface,
        workflowTemplates,
        schedule: workflowState?.schedule,
        configOverrides: workflowState?.configOverrides,
        foreignInputApproval: workflowState?.foreignInputApproval,
        pendingWorkflowStartApprovalIntent: (0, state_1.hasPendingWorkflowStartApproval)(workflowState)
            ? workflowState?.pendingWorkflowStartApprovalIntent
            : undefined,
        pendingPlanningIntent: workflowState?.pendingPlanningIntent,
        pendingPlanningReason: workflowState?.pendingPlanningReason,
        pendingPlanningSource: workflowState?.pendingPlanningSource,
    });
}
function toWorkflowTemplateSelector(templateName) {
    return templateName === null
        ? { mode: "no_template" }
        : { mode: "template", templatePath: templateName };
}
function assertWorkflowSelectionDecision(input) {
    const { selectionContext, selection } = input;
    for (const spec of workflow_selection_fields_1.ACTIVE_WORKFLOW_SELECTION_FIELD_SPECS) {
        const analysis = analyzeRuntimeWorkflowSelectionField({
            selectionContext,
            selection,
            spec,
        });
        if (analysis.autoActionMissing) {
            throw new Error("Workflow selection action was omitted for an auto/auto_select slot.");
        }
        if (analysis.useTemplateNameMissing) {
            throw new Error("Workflow selection templateName was omitted for use_template.");
        }
        if (analysis.deterministicActionConflict) {
            throw new Error(spec.actionConflictMessage);
        }
        if (analysis.candidateIssue !== null) {
            throw new Error(spec.candidateMessage);
        }
        if (selection.action === types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.requestTemplate &&
            (0, workflow_selection_fields_1.getActiveWorkflowSelectionSlotMode)(selectionContext) !== "auto") {
            throw new Error("request_template selection requires an auto slot.");
        }
        if (selection.action === types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.noTemplate &&
            (0, workflow_selection_fields_1.getActiveWorkflowSelectionSlotMode)(selectionContext) !== "auto" &&
            (0, workflow_selection_fields_1.getActiveWorkflowSelectionSlotMode)(selectionContext) !== "auto_select" &&
            (0, workflow_selection_fields_1.getActiveWorkflowSelectionExplicitTemplateName)(selectionContext) !== null) {
            throw new Error("no_template selection requires an auto/auto_select slot or explicit no_template lock.");
        }
    }
}
function analyzeRuntimeWorkflowSelectionField(input) {
    const candidates = (0, workflow_selection_fields_1.getActiveWorkflowSelectionCandidates)(input.selectionContext);
    return (0, workflow_selection_validation_1.analyzeWorkflowSelectionField)({
        spec: input.spec,
        action: (0, workflow_selection_fields_1.getActiveWorkflowSelectionAction)(input.selection),
        value: (0, workflow_selection_fields_1.getActiveWorkflowSelectionTemplateName)(input.selection),
        explicitTemplateName: (0, workflow_selection_fields_1.getActiveWorkflowSelectionExplicitTemplateName)(input.selectionContext),
        slotMode: (0, workflow_selection_fields_1.getActiveWorkflowSelectionSlotMode)(input.selectionContext),
        candidates,
        candidateSetProvided: candidates !== undefined && candidates !== null,
        skipCandidateValidationForExplicitSelection: false,
    });
}
function setSelectedWorkflowTemplateBindingForAutoSlot(input) {
    if (!(0, template_bindings_1.isWorkflowTemplateBindingSurface)(input.surface)) {
        return input.workflowTemplates;
    }
    const currentBinding = (0, template_bindings_1.getWorkflowTemplateBinding)({
        workflowTemplates: input.workflowTemplates,
        surface: input.surface,
    });
    if (input.slotMode !== "auto" &&
        input.slotMode !== "auto_select" &&
        currentBinding.selector.mode !== "auto" &&
        currentBinding.selector.mode !== "auto_select") {
        return input.workflowTemplates;
    }
    return (0, template_bindings_1.setWorkflowTemplateBinding)({
        workflowTemplates: input.workflowTemplates,
        surface: input.surface,
        binding: (0, template_bindings_1.createWorkflowTemplateBinding)({
            selector: toWorkflowTemplateSelector(input.templateName),
        }),
    });
}
