/**
 * UiState Scheduler
 * Phase 3: Rebuild UiState on steady cadence, handle expensive checks slower
 */
import { buildUiState } from './uiStateBuilder.js';
import { eventBus } from '../core/eventBus.js';
import { historyStore } from './historyStore.js';
class UiStateScheduler {
    interval = null;
    handler = null;
    getAppState = null;
    refreshIntervalMs;
    networkIntervalMs;
    constructor(refreshIntervalMs = 1500, networkIntervalMs = 30000) {
        this.refreshIntervalMs = refreshIntervalMs;
        this.networkIntervalMs = networkIntervalMs;
    }
    /**
     * Start scheduler
     * getAppState is a function that returns current AppState (allows dynamic updates)
     */
    start(getAppState, handler) {
        this.getAppState = getAppState;
        this.handler = handler;
        // Initial build
        this.refresh();
        // Regular refresh loop
        this.interval = setInterval(() => {
            this.refresh();
        }, this.refreshIntervalMs);
        // Subscribe to feed events for instant updates
        eventBus.subscribe(() => {
            // Feed event occurred - rebuild UiState immediately
            this.refresh();
        });
    }
    /**
     * Refresh UiState (rebuild from collectors)
     */
    async refresh() {
        if (!this.getAppState || !this.handler) {
            return;
        }
        try {
            const appState = this.getAppState(); // Get fresh AppState
            const uiState = await buildUiState(appState);
            // Update history store for sparklines
            if (typeof uiState.resources.cpuPercent === 'number') {
                historyStore.addSample('cpu', uiState.resources.cpuPercent);
            }
            else {
                historyStore.addSample('cpu', null);
            }
            if (typeof uiState.resources.ramUsedMb === 'number' && typeof uiState.resources.ramTotalMb === 'number') {
                const ramPercent = (uiState.resources.ramUsedMb / uiState.resources.ramTotalMb) * 100;
                historyStore.addSample('ram', ramPercent);
            }
            else {
                historyStore.addSample('ram', null);
            }
            if (typeof uiState.network.latencyMs === 'number') {
                historyStore.addSample('latency', uiState.network.latencyMs);
            }
            else {
                historyStore.addSample('latency', null);
            }
            this.handler(uiState);
        }
        catch (error) {
            // Silently fail - don't break scheduler
            console.error('[UiStateScheduler] Build error:', error);
        }
    }
    /**
     * Stop scheduler
     */
    stop() {
        if (this.interval) {
            clearInterval(this.interval);
            this.interval = null;
        }
        this.handler = null;
        this.getAppState = null;
    }
}
/**
 * Global scheduler instance
 */
let scheduler = null;
/**
 * Get or create scheduler
 */
export function getScheduler() {
    if (!scheduler) {
        const refreshMs = parseInt(process.env.UI_REFRESH_MS || '1500', 10);
        const networkMs = parseInt(process.env.NETWORK_REFRESH_MS || '30000', 10);
        scheduler = new UiStateScheduler(refreshMs, networkMs);
    }
    return scheduler;
}
/**
 * Start scheduler
 * getAppState is a function that returns current AppState (allows dynamic updates)
 */
export function startScheduler(getAppState, handler) {
    const sched = getScheduler();
    sched.start(getAppState, handler);
}
/**
 * Stop scheduler
 */
export function stopScheduler() {
    if (scheduler) {
        scheduler.stop();
        scheduler = null;
    }
}
//# sourceMappingURL=uiStateScheduler.js.map