"use strict";
/**
 * Active-session registry — a tiny module-level slot the TUI startup
 * fills with the live `WizardSession` so process-level handlers
 * (`safety-net.ts`, signal handlers) can find the session without
 * pulling in the whole TUI module graph.
 *
 * Lives in `utils/` (not `ui/tui/`) because it must be importable from
 * very early code paths — including before the TUI mounts. Keeping it
 * standalone also avoids circular imports between safety-net and the
 * store.
 *
 * Lifetime: registered when the TUI store boots, cleared when the
 * wizard exits (best-effort). Production runs are one-shot processes
 * so a single slot is sufficient.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.setActiveSession = setActiveSession;
exports.tryGetWizardStoreSession = tryGetWizardStoreSession;
exports._resetActiveSessionForTests = _resetActiveSessionForTests;
let _getActiveSession = null;
/**
 * Register a getter for the live session. Called by the TUI startup once
 * the store is constructed. The getter is invoked every time
 * `tryGetWizardStoreSession()` is called, so the safety-net always sees
 * the user's latest progress, not the state at registration time.
 *
 * Idempotent — last write wins. Pass `null`/`undefined` to clear.
 */
function setActiveSession(getter) {
    _getActiveSession = typeof getter === 'function' ? getter : null;
}
/**
 * Best-effort accessor for the active session. Returns null when no
 * session is registered (e.g. agent / CI mode, or a fatal during
 * pre-TUI bootstrap), or when the registered getter throws.
 */
function tryGetWizardStoreSession() {
    if (!_getActiveSession)
        return null;
    try {
        return _getActiveSession() ?? null;
    }
    catch {
        // A throwing getter must not bring down the safety-net — fall back
        // to "no session" so the abort path can still run.
        return null;
    }
}
/**
 * Test-only — clear the slot between assertions. Production runs
 * exit at the end of `wizardAbort` and don't need this.
 */
function _resetActiveSessionForTests() {
    _getActiveSession = null;
}
