/**
 * Plugin lifecycle state machine.
 *
 * Defines the valid state transitions for the 8-state plugin lifecycle
 * and provides pure guard/transition functions with no side effects.
 *
 * States:
 *   discovered → loading → loaded → active ↔ degraded
 *                                          ↓
 *                                        error → (unloading | disabled | loading)
 *                    loading/loaded/active/degraded → error
 *                    active/loaded/degraded/error → unloading → disabled
 *                    disabled → loading (re-enable)
 *
 * Recoverable plugin errors are recorded by the lifecycle manager without a state
 * transition; only fatal/load failures enter the `error` state.
 */
/**
 * Adjacency map: from state → set of valid target states.
 *
 * Encoded as a plain Record so it is safe to evaluate at module load time
 * with no external dependencies.
 */
export const VALID_TRANSITIONS = {
    discovered: ['loading'],
    loading: ['loaded', 'error'],
    loaded: ['active', 'error', 'unloading'],
    active: ['degraded', 'error', 'unloading'],
    degraded: ['active', 'error', 'unloading'],
    error: ['unloading', 'disabled', 'loading'],
    unloading: ['disabled'],
    disabled: ['loading'],
};
/**
 * Returns whether a transition from `from` → `to` is permitted by the
 * state machine.
 */
export function canTransition(from, to) {
    return VALID_TRANSITIONS[from].includes(to);
}
/**
 * Attempts a state transition and returns a typed result.
 *
 * This is a pure function — it has no side effects. The caller is responsible
 * for updating the plugin record and emitting events.
 *
 * @param from - Current state.
 * @param to   - Desired target state.
 * @returns `{ ok: true, from, to }` on success or `{ ok: false, reason }` on
 *          invalid transition.
 */
export function applyTransition(from, to) {
    if (canTransition(from, to)) {
        return { ok: true, from, to };
    }
    return {
        ok: false,
        reason: `Invalid transition: ${from} → ${to} (allowed from ${from}: [${VALID_TRANSITIONS[from].join(', ')}])`,
    };
}
/**
 * Returns whether a plugin in the given state is considered "operational"
 * (i.e. actively servicing requests, possibly in a degraded capacity).
 */
export function isOperational(state) {
    return state === 'active' || state === 'degraded';
}
/**
 * Returns whether a plugin in the given state can be safely hot-reloaded.
 * A hot reload requires the plugin to be quiesceable.
 */
export function isReloadable(state) {
    return state === 'active' || state === 'loaded' || state === 'degraded' || state === 'error';
}
/**
 * Returns whether the plugin's current state represents a terminal lifecycle
 * stop. The `error` state is recoverable via retry or unload.
 */
export function isTerminal(state) {
    return state === 'disabled';
}
