"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveWorkflowLevel = resolveWorkflowLevel;
exports.isWorkflowLevel = isWorkflowLevel;
const types_1 = require("./types");
const DEFAULT_WORKFLOW_LEVEL = "structured";
/** Resolves workflow policy without reading configuration or mutating repository state. */
function resolveWorkflowLevel(input = {}) {
    const recommendation = normalizeOptionalLevel(input.recommendation, "recommendation");
    const acceptedRecommendation = resolveAcceptedRecommendation(input, recommendation);
    const candidates = [];
    for (const source of types_1.WORKFLOW_LEVEL_SOURCE_PRECEDENCE) {
        if (source === "built-in")
            continue;
        const rawValue = source === "user"
            ? input.user ?? acceptedRecommendation
            : input[source];
        const level = normalizeOptionalLevel(rawValue, source);
        if (level !== null)
            candidates.push({ source, level });
    }
    const winner = candidates[0] ?? { source: "built-in", level: DEFAULT_WORKFLOW_LEVEL };
    assertCreationInputIsComplete(input, winner.level, winner.source);
    return {
        level: winner.level,
        source: winner.source,
        candidates,
        recommendation: recommendation === null
            ? null
            : { level: recommendation, accepted: input.recommendationAccepted === true }
    };
}
function isWorkflowLevel(value) {
    return typeof value === "string" && types_1.WORKFLOW_LEVELS.includes(value);
}
function normalizeOptionalLevel(value, source) {
    if (value === undefined || value === null)
        return null;
    if (!isWorkflowLevel(value)) {
        throw new types_1.WorkRoutingError("invalid-workflow-level", `Workflow level from '${source}' must be one of: ${types_1.WORKFLOW_LEVELS.join(", ")}.`);
    }
    return value;
}
function resolveAcceptedRecommendation(input, recommendation) {
    if (input.recommendationAccepted !== true)
        return null;
    if (recommendation === null) {
        throw new types_1.WorkRoutingError("invalid-recommendation-acceptance", "A workflow recommendation cannot be accepted when no recommendation was supplied.");
    }
    if (input.user !== undefined && input.user !== null && input.user !== recommendation) {
        throw new types_1.WorkRoutingError("invalid-recommendation-acceptance", "The accepted workflow recommendation conflicts with the explicit user selection.");
    }
    return recommendation;
}
function assertCreationInputIsComplete(input, level, source) {
    if (input.specificationCount === undefined)
        return;
    if (!Number.isInteger(input.specificationCount) || input.specificationCount < 0) {
        throw new types_1.WorkRoutingError("invalid-specification-count", "Specification count must be a non-negative integer when supplied.");
    }
    if (input.interactive !== false || input.specificationCount > 0)
        return;
    if (level === "basic" && source === "explicit")
        return;
    throw new types_1.WorkRoutingError("incomplete-specification-input", "Non-interactive creation without canonical specification input requires an explicit Basic selection.");
}
