// lib/accounting/core/event-registry.ts

import { AccountingEventSchema, AccountingEventType } from './accounting-event-registry';

/**
 * Central registry for all accounting event schemas.
 * Enforces event versioning, idempotency, and handler lookup.
 */
export class EventRegistry {
  private registry = new Map<AccountingEventType, AccountingEventSchema<any>>();

  register<T>(schema: AccountingEventSchema<T>) {
    if (this.registry.has(schema.type)) {
      throw new Error(`Event type ${schema.type} already registered`);
    }
    this.registry.set(schema.type, schema);
  }

  get<T>(type: AccountingEventType): AccountingEventSchema<T> {
    const schema = this.registry.get(type);
    if (!schema) throw new Error(`Event type ${type} not registered`);
    return schema as AccountingEventSchema<T>;
  }

  list(): AccountingEventType[] {
    return Array.from(this.registry.keys());
  }
}
