/**
 * Dashboard Event Stream
 *
 * Centralized event bus that captures live events from all UAP subsystems
 * and streams them to connected web dashboard clients via SSE.
 *
 * Events are categorized by type:
 *   - policy: enforcement checks, blocks, stage changes
 *   - memory: lookups, hits, misses, consolidation
 *   - deploy: queue, batch, execute, complete
 *   - agent: register, start, progress, complete, error
 *   - task: create, start, complete, fail
 *   - skill: match, activate, deactivate
 *   - pattern: match, activate
 *   - cost: token usage, cost tracking
 *   - system: errors, warnings, info
 */
const MAX_EVENT_HISTORY = 200;
class DashboardEventBus {
    handlers = [];
    history = [];
    nextId = 1;
    /**
     * Subscribe to all dashboard events.
     * Returns an unsubscribe function.
     */
    subscribe(handler) {
        this.handlers.push(handler);
        return () => {
            const idx = this.handlers.indexOf(handler);
            if (idx >= 0)
                this.handlers.splice(idx, 1);
        };
    }
    /**
     * Emit a new event to all subscribers.
     */
    emit(category, type, severity, title, detail, metadata) {
        const event = {
            id: this.nextId++,
            timestamp: new Date().toISOString(),
            category,
            type,
            severity,
            title,
            detail,
            metadata,
        };
        this.history.push(event);
        if (this.history.length > MAX_EVENT_HISTORY) {
            this.history = this.history.slice(-MAX_EVENT_HISTORY);
        }
        for (const handler of this.handlers) {
            try {
                handler(event);
            }
            catch {
                // Handler errors are non-fatal
            }
        }
    }
    /**
     * Get recent event history.
     */
    getHistory(limit = 50) {
        return this.history.slice(-limit);
    }
    /**
     * Get events since a specific event ID.
     */
    getEventsSince(lastId) {
        return this.history.filter((e) => e.id > lastId);
    }
    /**
     * Get subscriber count.
     */
    subscriberCount() {
        return this.handlers.length;
    }
    /**
     * Clear all history and handlers.
     */
    clear() {
        this.handlers = [];
        this.history = [];
        this.nextId = 1;
    }
}
// ── Singleton ──
let _globalEventBus = null;
export function getDashboardEventBus() {
    if (!_globalEventBus) {
        _globalEventBus = new DashboardEventBus();
    }
    return _globalEventBus;
}
export function resetDashboardEventBus() {
    _globalEventBus?.clear();
    _globalEventBus = null;
}
// ── Convenience emitters for each subsystem ──
export function emitPolicyEvent(type, title, allowed, detail, metadata) {
    getDashboardEventBus().emit('policy', type, allowed ? 'success' : 'error', title, detail, metadata);
}
export function emitMemoryEvent(type, title, hit, detail, metadata) {
    getDashboardEventBus().emit('memory', type, hit ? 'success' : 'info', title, detail, metadata);
}
export function emitDeployEvent(type, title, severity = 'info', detail, metadata) {
    getDashboardEventBus().emit('deploy', type, severity, title, detail, metadata);
}
export function emitAgentEvent(type, title, severity = 'info', detail, metadata) {
    getDashboardEventBus().emit('agent', type, severity, title, detail, metadata);
}
export function emitTaskEvent(type, title, severity = 'info', detail, metadata) {
    getDashboardEventBus().emit('task', type, severity, title, detail, metadata);
}
export function emitSystemEvent(type, title, severity = 'info', detail, metadata) {
    getDashboardEventBus().emit('system', type, severity, title, detail, metadata);
}
//# sourceMappingURL=event-stream.js.map