"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkflowSelectionContext = buildWorkflowSelectionContext;
exports.buildWorkflowSelectResult = buildWorkflowSelectResult;
exports.buildWorkflowPlanResult = buildWorkflowPlanResult;
exports.buildWorkflowExecuteResult = buildWorkflowExecuteResult;
const types_1 = require("../worker/types");
const run_context_prepare_1 = require("./run-context-prepare");
const template_bindings_1 = require("./template-bindings");
const template_inventory_1 = require("./template-inventory");
function buildWorkflowSelectionContext(input) {
    const workflowSurface = input.workflowSurface;
    const templateSlot = input.resolution.binding.selector.mode === "auto"
        ? "auto"
        : input.resolution.binding.selector.mode === "auto_select"
            ? "auto_select"
            : "deterministic";
    const selectionContext = {
        workflowSurface,
        templateSlot,
    };
    if (templateSlot === "auto" || templateSlot === "auto_select") {
        selectionContext.candidateTemplates =
            buildWorkerWorkflowSelectionTemplateCandidates({
                catalog: input.catalog,
                surface: workflowSurface,
            });
    }
    const resolvedTemplateName = resolveWorkflowSelectionExplicitTemplateName({
        selector: input.resolution.binding.selector,
        resolvedTemplateName: (0, template_bindings_1.getWorkflowTemplateName)(input.resolution.binding),
    });
    if (resolvedTemplateName !== undefined) {
        selectionContext.explicitTemplateName = resolvedTemplateName;
    }
    return selectionContext;
}
function buildWorkflowSelectResult(input) {
    const workflow = buildWorkerWorkflowContext({
        mode: types_1.WORKER_RUN_MODES.workflowSelect,
        templateName: null,
        workflowSurface: input.prepared.workflowSurface,
        workflowState: input.workflowState,
        issueCompletion: input.issueCompletion,
        issueNotesPath: input.issueNotesPath,
        followupMaxIssuesPerSource: input.followupMaxIssuesPerSource,
        ...(input.followupPreferredWorkflowTemplates !== undefined
            ? {
                followupPreferredWorkflowTemplates: input.followupPreferredWorkflowTemplates,
            }
            : {}),
        ...(input.followupAutoMaintainTemplates !== undefined
            ? { followupAutoMaintainTemplates: input.followupAutoMaintainTemplates }
            : {}),
    });
    workflow.selection = buildWorkflowSelectionContext({
        catalog: input.prepared.catalog,
        workflowSurface: input.prepared.workflowSurface,
        resolution: input.prepared.resolution,
    });
    workflow.templateBody = input.artifacts?.templateBody ?? null;
    workflow.requiredSkills = input.artifacts?.requiredSkills ?? null;
    workflow.requiredMcp = input.artifacts?.requiredMcp ?? null;
    workflow.directChildWorkflows = input.artifacts?.directChildWorkflows ?? null;
    workflow.templateReferences = input.artifacts?.templateReferences ?? null;
    workflow.commandContext = input.prepared.appliedCommandState.commandContext;
    return {
        kind: "select",
        workflowTemplateName: (0, template_bindings_1.getWorkflowTemplateName)(input.prepared.resolution.binding),
        workflowState: input.workflowState,
        planningIntent: input.planningIntent,
        workflow,
    };
}
function buildWorkflowPlanResult(input) {
    const workflow = buildWorkerWorkflowContext({
        mode: types_1.WORKER_RUN_MODES.workflowPlan,
        templateName: input.templateName,
        planningIntent: input.planningIntent,
        workflowSurface: input.workflowSurface,
        workflowState: input.workflowState,
        issueCompletion: input.issueCompletion,
        issueNotesPath: input.issueNotesPath,
        followupMaxIssuesPerSource: input.followupMaxIssuesPerSource,
        ...(input.followupPreferredWorkflowTemplates !== undefined
            ? {
                followupPreferredWorkflowTemplates: input.followupPreferredWorkflowTemplates,
            }
            : {}),
        ...(input.followupAutoMaintainTemplates !== undefined
            ? { followupAutoMaintainTemplates: input.followupAutoMaintainTemplates }
            : {}),
        includeMergeContext: true,
    });
    workflow.templateBody = input.artifacts?.templateBody ?? null;
    workflow.requiredSkills = input.artifacts?.requiredSkills ?? null;
    workflow.requiredMcp = input.artifacts?.requiredMcp ?? null;
    workflow.commandContext = input.commandContext;
    if (input.startApprovalMode !== undefined) {
        workflow.startApprovalMode = input.startApprovalMode;
    }
    if (input.artifacts?.directChildWorkflows !== undefined) {
        workflow.directChildWorkflows = input.artifacts.directChildWorkflows;
    }
    if (input.artifacts?.templateReferences !== undefined) {
        workflow.templateReferences = input.artifacts.templateReferences;
    }
    return {
        kind: "plan",
        workflowTemplateName: input.workflowTemplateName,
        workflowState: input.workflowState,
        planningIntent: input.planningIntent,
        requiresPreflightReplan: input.requiresPreflightReplan,
        workflow,
    };
}
function buildWorkflowExecuteResult(input) {
    const workflow = buildWorkerWorkflowContext({
        mode: types_1.WORKER_RUN_MODES.workflowExecute,
        templateName: input.templateName,
        workflowSurface: input.workflowSurface,
        workflowState: input.workflowState,
        issueCompletion: input.issueCompletion,
        issueNotesPath: input.issueNotesPath,
        followupMaxIssuesPerSource: input.followupMaxIssuesPerSource,
        ...(input.followupPreferredWorkflowTemplates !== undefined
            ? {
                followupPreferredWorkflowTemplates: input.followupPreferredWorkflowTemplates,
            }
            : {}),
        ...(input.followupAutoMaintainTemplates !== undefined
            ? { followupAutoMaintainTemplates: input.followupAutoMaintainTemplates }
            : {}),
        includeMergeContext: true,
    });
    workflow.requiredSkills = input.requiredSkills
        ? [...input.requiredSkills]
        : null;
    workflow.requiredMcp = input.requiredMcp ? [...input.requiredMcp] : null;
    return {
        kind: "execute",
        workflowTemplateName: input.workflowTemplateName,
        workflowState: input.workflowState,
        workflow,
    };
}
function buildWorkerWorkflowContext(input) {
    const workflow = {
        mode: input.mode,
        templateName: input.templateName,
        planningIntent: input.planningIntent ?? null,
        workflowSurface: input.workflowSurface,
        state: (0, run_context_prepare_1.sanitizeWorkerVisibleWorkflowState)(input.workflowState, {
            includeDependencySyncState: input.mode === types_1.WORKER_RUN_MODES.workflowPlan,
        }),
        followupMaxIssuesPerSource: input.followupMaxIssuesPerSource,
        followupPreferredWorkflowTemplates: [
            ...(input.followupPreferredWorkflowTemplates ?? []),
        ],
        followupAutoMaintainTemplates: input.followupAutoMaintainTemplates ?? false,
        issueNotesPath: input.issueNotesPath,
    };
    if (input.issueCompletion !== undefined) {
        workflow.issueCompletion = input.issueCompletion;
    }
    if (input.includeMergeContext === true &&
        input.workflowState.mergeContext !== undefined) {
        workflow.mergeContext = input.workflowState.mergeContext;
    }
    return workflow;
}
function resolveWorkflowSelectionExplicitTemplateName(input) {
    switch (input.selector.mode) {
        case "auto":
        case "auto_select":
            return undefined;
        case "no_template":
            return null;
        case "template":
            return input.selector.templatePath;
    }
}
function buildWorkerWorkflowSelectionTemplateCandidates(input) {
    if (!(0, template_bindings_1.isWorkflowTemplateBindingSurface)(input.surface)) {
        return [];
    }
    return (0, template_inventory_1.buildWorkflowTemplateCandidates)(input.catalog, {
        target: input.surface,
        autoOnly: true,
    }).map((candidate) => ({
        templateName: candidate.templatePath,
        description: candidate.description,
        requiredSkills: candidate.requiredSkills,
        requiredMcp: candidate.requiredMcp,
    }));
}
