import type { EventDefinition } from './types.js';

export class EventRegistry {
  private definitions = new Map<string, EventDefinition>();

  constructor(definitions: EventDefinition[]) {
    for (const definition of definitions) {
      if (this.definitions.has(definition.id)) {
        throw new Error(`Duplicate event id detected: ${definition.id}`);
      }
      this.definitions.set(definition.id, definition);
    }
  }

  get(id: string): EventDefinition | undefined {
    return this.definitions.get(id);
  }

  list(): EventDefinition[] {
    return Array.from(this.definitions.values());
  }

  count(): number {
    return this.definitions.size;
  }
}

export function createEventRegistry(definitions: EventDefinition[]): EventRegistry {
  return new EventRegistry(definitions);
}

export function mergeCatalogs(...catalogs: EventDefinition[][]): EventDefinition[] {
  return catalogs.flat();
}
