"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SIDE_WORKFLOW_SELECTOR_FIELD_SPECS = exports.ACTIVE_WORKFLOW_SELECTION_FIELD_SPECS = exports.MAIN_WORKFLOW_SELECTION_FIELD_SPEC = void 0;
exports.getActiveWorkflowSelectionAction = getActiveWorkflowSelectionAction;
exports.getActiveWorkflowSelectionTemplateName = getActiveWorkflowSelectionTemplateName;
exports.getActiveWorkflowSelectionSlotMode = getActiveWorkflowSelectionSlotMode;
exports.getActiveWorkflowSelectionExplicitTemplateName = getActiveWorkflowSelectionExplicitTemplateName;
exports.getActiveWorkflowSelectionCandidates = getActiveWorkflowSelectionCandidates;
exports.buildActiveWorkflowSelectionDecision = buildActiveWorkflowSelectionDecision;
exports.createSideWorkflowSurfaceRecord = createSideWorkflowSurfaceRecord;
const types_1 = require("./types");
exports.MAIN_WORKFLOW_SELECTION_FIELD_SPEC = {
    actionField: "action",
    selectionField: "templateName",
    slotField: "templateSlot",
    explicitField: "explicitTemplateName",
    candidatesField: "candidateTemplates",
    actionConflictMessage: "Workflow selection action conflicted with the explicit template.",
    candidateMessage: "Workflow selection template was not in the candidate set.",
    invalidCandidateMessage: "selection.templateName must match one of the provided candidate workflow templates.",
};
exports.ACTIVE_WORKFLOW_SELECTION_FIELD_SPECS = [
    exports.MAIN_WORKFLOW_SELECTION_FIELD_SPEC,
];
exports.SIDE_WORKFLOW_SELECTOR_FIELD_SPECS = [
    {
        surface: "pull_request",
        workflowOption: {
            availableTemplatesPromptLabel: "fieldAvailablePullRequestWorkflowTemplates",
            selectorDescription: "PR workflow selector.",
            selectorLabel: "PR workflow template",
        },
    },
    {
        surface: "unplanned",
        workflowOption: {
            availableTemplatesPromptLabel: "fieldAvailableUnplannedWorkflowTemplates",
            selectorDescription: "Unplanned workflow selector.",
            selectorLabel: "unplanned workflow template",
        },
    },
    {
        surface: "followup",
        workflowOption: {
            availableTemplatesPromptLabel: "fieldAvailableFollowupWorkflowTemplates",
            selectorDescription: "Follow-up workflow selector.",
            selectorLabel: "follow-up workflow template",
        },
    },
];
function getActiveWorkflowSelectionAction(selection) {
    return selection.action;
}
function getActiveWorkflowSelectionTemplateName(selection) {
    if (selection.action === types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.useTemplate) {
        return selection.templateName;
    }
    if (selection.action === types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.noTemplate ||
        selection.action === types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.requestTemplate) {
        return null;
    }
    return undefined;
}
function getActiveWorkflowSelectionSlotMode(selectionContext) {
    return selectionContext?.[exports.MAIN_WORKFLOW_SELECTION_FIELD_SPEC.slotField];
}
function getActiveWorkflowSelectionExplicitTemplateName(selectionContext) {
    return selectionContext?.[exports.MAIN_WORKFLOW_SELECTION_FIELD_SPEC.explicitField];
}
function getActiveWorkflowSelectionCandidates(selectionContext) {
    return selectionContext?.[exports.MAIN_WORKFLOW_SELECTION_FIELD_SPEC.candidatesField];
}
function buildActiveWorkflowSelectionDecision(selection) {
    if (selection.action === undefined) {
        throw new Error("Workflow selection action is required.");
    }
    if (selection.action === types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.useTemplate) {
        const templateName = selection.templateName;
        if (templateName === undefined || templateName === null) {
            throw new Error("use_template selection requires templateName.");
        }
        return {
            action: types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.useTemplate,
            templateName,
        };
    }
    if (selection.action === types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.requestTemplate) {
        return { action: types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.requestTemplate };
    }
    if (selection.action === types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.noTemplate) {
        return { action: types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.noTemplate };
    }
    throw new Error(`Unsupported workflow selection action: ${selection.action}`);
}
function createSideWorkflowSurfaceRecord(resolveValue) {
    const record = {};
    for (const spec of exports.SIDE_WORKFLOW_SELECTOR_FIELD_SPECS) {
        record[spec.surface] = resolveValue(spec);
    }
    return record;
}
