/**
 * Feed Collector
 * Phase 3: Own the feed event list (ring buffer)
 */
import { eventBus } from '../core/eventBus.js';
// Ring buffer for feed events (in-memory, stable in-process)
const MAX_FEED_EVENTS = 50;
const feedBuffer = [];
// Subscribe to event bus
let unsubscribe = null;
/**
 * Initialize feed collector (subscribe to event bus)
 */
export function initFeedCollector() {
    if (unsubscribe) {
        unsubscribe();
    }
    unsubscribe = eventBus.subscribe((event) => {
        feedBuffer.push(event);
        // Keep only last MAX_FEED_EVENTS
        if (feedBuffer.length > MAX_FEED_EVENTS) {
            feedBuffer.shift();
        }
    });
}
/**
 * Get events (for collector)
 */
export function getEvents(limit = 12) {
    return feedBuffer.slice(-limit).reverse(); // Newest first
}
/**
 * Push event (for runtime execution)
 */
export function pushEvent(tag, msg) {
    eventBus.emit({
        tag,
        msg,
        ts: Date.now(),
        level: 'INFO', // Add required level field
    });
}
/**
 * Collect feed data
 */
export async function collectFeed() {
    try {
        // Filter events to only include compatible tags for FeedData
        const compatibleTags = ['RUN', 'NET', 'SEC', 'DB', 'GATEWAY', 'SYSTEM'];
        const events = getEvents(12)
            .filter((e) => compatibleTags.includes(e.tag))
            .map(e => ({
            ts: e.ts,
            tag: e.tag,
            msg: e.msg,
        }));
        const data = {
            events,
        };
        // Phase 9: Feed is always LIVE if we have events
        const now = Date.now();
        const status = 'LIVE';
        return {
            status,
            asOf: now,
            data,
            source: 'collector_feed',
            staleAfterMs: 0, // Feed updates instantly via event bus
        };
    }
    catch (error) {
        return {
            status: 'UNAVAILABLE',
            asOf: Date.now(),
            source: 'collector_feed',
            staleAfterMs: 0,
            reasonCode: 'COLLECTION_ERROR',
            message: error.message || 'Unknown error',
            action: 'system status',
        };
    }
}
//# sourceMappingURL=collector_feed.js.map