import { logger } from '@pellux/goodvibes-sdk/platform/utils/logger';
export { saveSession } from './session-persistence.js';
import { saveSession } from './session-persistence.js';
import { summarizeError } from '@pellux/goodvibes-sdk/platform/utils/error-display';
// ── Startup lifecycle ────────────────────────────────────────────────────────
/**
 * Fire the session:start lifecycle hook.
 * Non-fatal: errors are logged and swallowed.
 */
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.debug('fireSessionStart hook error (non-fatal)', { error: summarizeError(err) });
        });
    }
    catch (err) {
        logger.debug('fireSessionStart sync error (non-fatal)', { 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
    saveSession(sessionId, sessionData, model, provider, title, persistenceOptions);
    // Step 2: lifecycle hooks (fire-and-forget, best-effort before process exit)
    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.debug('shutdownRuntime hook fire error (non-fatal)', { specific, error: summarizeError(err) }); });
        }
        catch (err) {
            logger.debug('shutdownRuntime hook sync error (non-fatal)', { specific, error: summarizeError(err) });
        }
    };
    fireHook('end');
    fireHook('save');
    // Step 3: stop ScheduleManager
    try {
        scheduleManager?.destroy();
    }
    catch (err) {
        logger.debug('ScheduleManager.destroy failed (non-fatal)', { error: summarizeError(err) });
    }
    // Step 4: stop provider registry watcher
    try {
        providerRegistry?.stopWatching();
    }
    catch (err) {
        logger.debug('providerRegistry.stopWatching failed (non-fatal)', { error: summarizeError(err) });
    }
    // Step 5: dispose cross-session orchestration registry if it is app-owned in this runtime
    try {
        sessionOrchestration?.dispose();
    }
    catch (err) {
        logger.debug('sessionOrchestration.dispose failed (non-fatal)', { error: summarizeError(err) });
    }
}
