import type { AnalyticsEvent } from "./types.js";

let events: AnalyticsEvent[] = [];

/**
 * Emit an analytics event to the test-observable sink.
 * In production, this is a no-op ring buffer (capped to prevent memory leaks).
 * In test, events are retained for assertion via getRecordedEvents().
 */
export function emitEvent(event: AnalyticsEvent): void {
  events.push(event);

  // Ring buffer: cap at 1000 events to prevent unbounded growth
  if (events.length > 1000) {
    events = events.slice(-500);
  }
}

/** Returns all recorded events (for test assertions). */
export function getRecordedEvents(): readonly AnalyticsEvent[] {
  return events;
}

/** Clears all recorded events (for test teardown). */
export function resetEvents(): void {
  events = [];
}
