import { applyTransition as applyCompactionTransition, canTransition as canCompactionTransition, isTerminal as isTerminalCompactionState, reachableFrom as reachableFromCompactionState, } from './compaction/index.js';
import { applyTransition as applyMcpTransition, canTransition as canMcpTransition, isOperational as isOperationalMcpState, isTerminal as isTerminalMcpState, reachableFrom as reachableFromMcpState, } from './mcp/index.js';
import { applyTransition as applyPluginTransition, canTransition as canPluginTransition, isOperational as isOperationalPluginState, isReloadable as isReloadablePluginState, isTerminal as isTerminalPluginState, } from './plugins/index.js';
import { canTransition as canTaskTransition, getValidTransitions as getValidTaskTransitions, isTerminalStatus, } from './tasks/index.js';
const TASK_STATES = new Set([
    'queued',
    'running',
    'blocked',
    'completed',
    'failed',
    'cancelled',
]);
const PLUGIN_STATES = new Set([
    'discovered',
    'loading',
    'loaded',
    'active',
    'degraded',
    'error',
    'unloading',
    'disabled',
]);
const MCP_STATES = new Set([
    'configured',
    'connecting',
    'connected',
    'degraded',
    'auth_required',
    'reconnecting',
    'disconnected',
]);
const COMPACTION_STATES = new Set([
    'idle',
    'checking_threshold',
    'microcompact',
    'collapse',
    'autocompact',
    'reactive_compact',
    'boundary_commit',
    'done',
    'failed',
]);
function isTaskState(state) {
    return TASK_STATES.has(state);
}
function isPluginState(state) {
    return PLUGIN_STATES.has(state);
}
function isMcpState(state) {
    return MCP_STATES.has(state);
}
function isCompactionState(state) {
    return COMPACTION_STATES.has(state);
}
export function canTransition(from, to) {
    if (isPluginState(from) && isPluginState(to))
        return canPluginTransition(from, to);
    if (isMcpState(from) && isMcpState(to))
        return canMcpTransition(from, to);
    if (isCompactionState(from) && isCompactionState(to))
        return canCompactionTransition(from, to);
    if (isTaskState(from) && isTaskState(to))
        return canTaskTransition(from, to);
    return false;
}
export function applyTransition(from, to) {
    if (isPluginState(from) && isPluginState(to))
        return applyPluginTransition(from, to);
    if (isMcpState(from) && isMcpState(to)) {
        const result = applyMcpTransition(from, to);
        if (result.success) {
            return {
                ...result,
                ok: true,
                from: result.previous,
                to: result.next,
            };
        }
        return {
            ...result,
            ok: false,
        };
    }
    if (isCompactionState(from) && isCompactionState(to))
        return applyCompactionTransition(from, to);
    if (isTaskState(from) && isTaskState(to)) {
        if (canTaskTransition(from, to))
            return { ok: true, from, to };
        return {
            ok: false,
            reason: `Invalid task transition: ${from} -> ${to}`,
        };
    }
    return {
        ok: false,
        reason: `No runtime lifecycle state machine accepts transition ${String(from)} -> ${String(to)}`,
    };
}
export function reachableFrom(from) {
    if (isMcpState(from))
        return reachableFromMcpState(from);
    if (isCompactionState(from))
        return reachableFromCompactionState(from);
    if (isTaskState(from))
        return new Set(getValidTaskTransitions(from));
    if (isPluginState(from)) {
        return new Set([...PLUGIN_STATES].filter((target) => canPluginTransition(from, target)));
    }
    return new Set();
}
export function isOperational(state) {
    if (isPluginState(state))
        return isOperationalPluginState(state);
    if (isMcpState(state))
        return isOperationalMcpState(state);
    return false;
}
export function isReloadable(state) {
    return isReloadablePluginState(state);
}
export function isTerminal(state) {
    if (isPluginState(state))
        return isTerminalPluginState(state);
    if (isMcpState(state))
        return isTerminalMcpState(state);
    if (isCompactionState(state))
        return isTerminalCompactionState(state);
    if (isTaskState(state))
        return isTerminalStatus(state);
    return false;
}
