/**
 * Minimal replay cache for msgId dedup.
 *
 * Implemented as a bounded FIFO set. For production you would likely want an LRU.
 */
export class ReplayCache {
  private readonly max: number;
  private queue: string[] = [];
  private set = new Set<string>();

  constructor(maxEntries: number = 10000) {
    if (maxEntries <= 0) throw new Error('maxEntries must be > 0');
    this.max = maxEntries;
  }

  has(msgId: string): boolean {
    return this.set.has(msgId);
  }

  add(msgId: string): void {
    if (this.set.has(msgId)) return;
    this.set.add(msgId);
    this.queue.push(msgId);
    while (this.queue.length > this.max) {
      const old = this.queue.shift();
      if (old) this.set.delete(old);
    }
  }
}
