// ── Valid transitions ─────────────────────────────────────────────────────────
/**
 * Adjacency map for the MCP lifecycle state machine.
 *
 * Keys are source states; values are the set of reachable target states.
 */
const VALID_TRANSITIONS = new Map([
    ['configured', new Set(['connecting', 'disconnected'])],
    ['connecting', new Set(['connected', 'auth_required', 'reconnecting', 'disconnected'])],
    ['connected', new Set(['degraded', 'auth_required', 'reconnecting', 'disconnected'])],
    ['degraded', new Set(['connected', 'reconnecting', 'disconnected'])],
    ['auth_required', new Set(['connecting', 'disconnected'])],
    ['reconnecting', new Set(['connecting', 'connected', 'disconnected'])],
    ['disconnected', new Set(['connecting'])],
]);
// ── Guards ────────────────────────────────────────────────────────────────────
/**
 * Returns `true` if transitioning from `from` to `to` is permitted by the
 * state machine definition.
 *
 * @param from - Current state
 * @param to   - Proposed next state
 */
export function canTransition(from, to) {
    return VALID_TRANSITIONS.get(from)?.has(to) ?? false;
}
/**
 * Returns all states reachable from the given state.
 *
 * @param from - Current state
 */
export function reachableFrom(from) {
    return VALID_TRANSITIONS.get(from) ?? new Set();
}
/**
 * Attempt to apply a state transition.
 *
 * Returns `{ success: true, previous, next }` if the transition is valid,
 * or `{ success: false, reason }` if it is not.
 *
 * This function is pure — callers are responsible for updating the entry.
 *
 * @param current  - The current state of the server
 * @param target   - The desired next state
 */
export function applyTransition(current, target) {
    if (current === target) {
        return { success: false, reason: `already in state '${current}'` };
    }
    if (!canTransition(current, target)) {
        return {
            success: false,
            reason: `transition '${current}' → '${target}' is not permitted`,
        };
    }
    return { success: true, previous: current, next: target };
}
/**
 * Returns `true` if the state represents an active/usable server.
 *
 * Both `connected` and `degraded` allow tool calls to proceed.
 */
export function isOperational(state) {
    return state === 'connected' || state === 'degraded';
}
/**
 * Returns `true` if the state is terminal for a reconnect cycle
 * (i.e., the manager should not attempt further reconnects automatically).
 */
export function isTerminal(state) {
    return state === 'disconnected';
}
