"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.analyzeWorkflowSelectionField = analyzeWorkflowSelectionField;
const types_1 = require("./types");
function analyzeWorkflowSelectionField(input) {
    return {
        autoActionMissing: (input.slotMode === "auto" || input.slotMode === "auto_select") &&
            input.action === undefined,
        useTemplateNameMissing: input.action === types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.useTemplate &&
            input.value === undefined,
        deterministicActionConflict: isDeterministicActionConflict(input),
        candidateIssue: analyzeWorkflowSelectionCandidate(input),
    };
}
function analyzeWorkflowSelectionCandidate(input) {
    if (input.candidateSetProvided !== true) {
        return null;
    }
    if (input.skipCandidateValidationForExplicitSelection &&
        input.explicitTemplateName !== undefined) {
        return null;
    }
    if (input.action !== types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.useTemplate) {
        return null;
    }
    const candidateTemplateNames = new Set(input.candidates?.map((candidate) => candidate.templateName) ?? []);
    if (candidateTemplateNames.size === 0) {
        return "empty-candidates";
    }
    return isInvalidWorkflowSelectionCandidate(input.spec, input.value, candidateTemplateNames)
        ? "invalid-candidate"
        : null;
}
function isInvalidWorkflowSelectionCandidate(spec, selectedTemplateName, candidateTemplateNames) {
    if (spec.selectionField === "templateName") {
        return (selectedTemplateName === null ||
            selectedTemplateName === undefined ||
            !candidateTemplateNames.has(selectedTemplateName));
    }
    return (selectedTemplateName !== undefined &&
        selectedTemplateName !== null &&
        !candidateTemplateNames.has(selectedTemplateName));
}
function isDeterministicActionConflict(input) {
    if (input.explicitTemplateName === undefined || input.action === undefined) {
        return false;
    }
    if (input.explicitTemplateName === null) {
        return input.action !== types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.noTemplate;
    }
    return (input.action !== types_1.WORKER_WORKFLOW_SELECTION_ACTIONS.useTemplate ||
        input.value !== input.explicitTemplateName);
}
