import { logger } from '../utils/logger.js';
export { saveSession } from './session-persistence.js';
import { saveSession } from './session-persistence.js';
import { summarizeError } from '../utils/error-display.js';
// ── Startup lifecycle ────────────────────────────────────────────────────────
/**
 * Fire the session:start lifecycle hook.
 * Hook failures are logged and do not block startup.
 */
export function fireSessionStart(sessionId, hookDispatcher) {
    if (!hookDispatcher)
        return;
    try {
        hookDispatcher.fire({
            path: 'Lifecycle:session:start',
            phase: 'Lifecycle',
            category: 'session',
            specific: 'start',
            sessionId,
            timestamp: Date.now(),
            payload: { sessionId },
        }).catch((err) => {
            logger.warn('fireSessionStart hook failed', { error: summarizeError(err) });
        });
    }
    catch (err) {
        logger.warn('fireSessionStart hook dispatch failed', { error: summarizeError(err) });
    }
}
// ── Shutdown lifecycle ───────────────────────────────────────────────────────
/**
 * Ordered logical shutdown of all background runtime subsystems.
 *
 * Sequence:
 * 1. Persist conversation to sessions store
 * 2. Fire session:end and session:save lifecycle hooks
 * 3. Destroy ScheduleManager (cancels pending scheduled tasks)
 * 4. Stop provider registry file-watcher
 *
 * This function does NOT touch the terminal (alt-screen, raw mode, etc.) —
 * that remains the responsibility of main.ts.
 *
 * @param sessionId  - Active session identifier.
 * @param sessionData - Latest conversation to persist.
 * @param model      - Active model identifier.
 * @param provider   - Active provider identifier.
 * @param title      - Conversation title (may be empty string).
 */
export async function shutdownRuntime(sessionId, sessionData, model, provider, title = '', scheduleManager, hookDispatcher, providerRegistry, sessionOrchestration, persistenceOptions) {
    // Step 1: persist conversation
    let shutdownError = null;
    try {
        saveSession(sessionId, sessionData, model, provider, title, persistenceOptions);
    }
    catch (error) {
        const message = summarizeError(error);
        logger.warn('saveSession failed during shutdown', { error: message });
        shutdownError = new Error(`shutdownRuntime failed to persist session: ${message}`, {
            cause: error,
        });
    }
    // Step 2: lifecycle hooks are started without waiting for async handlers.
    const fireHook = (specific) => {
        if (!hookDispatcher)
            return;
        try {
            hookDispatcher.fire({
                path: `Lifecycle:session:${specific}`,
                phase: 'Lifecycle',
                category: 'session',
                specific,
                sessionId,
                timestamp: Date.now(),
                payload: { sessionId },
            }).catch((err) => {
                logger.warn('shutdownRuntime lifecycle hook failed', {
                    specific,
                    error: summarizeError(err),
                });
            });
        }
        catch (err) {
            logger.warn('shutdownRuntime lifecycle hook dispatch failed', {
                specific,
                error: summarizeError(err),
            });
        }
    };
    fireHook('end');
    fireHook('save');
    // Step 3: stop ScheduleManager
    try {
        scheduleManager?.destroy();
    }
    catch (err) {
        logger.warn('ScheduleManager.destroy failed during shutdown', {
            error: summarizeError(err),
        });
    }
    // Step 4: stop provider registry watcher
    try {
        providerRegistry?.stopWatching();
    }
    catch (err) {
        logger.warn('providerRegistry.stopWatching failed during shutdown', {
            error: summarizeError(err),
        });
    }
    // Step 5: dispose cross-session orchestration registry if it is app-owned in this runtime
    try {
        sessionOrchestration?.dispose();
    }
    catch (err) {
        logger.warn('sessionOrchestration.dispose failed during shutdown', {
            error: summarizeError(err),
        });
    }
    if (shutdownError)
        throw shutdownError;
}
