import { EventEmitter } from 'events';

class EventBus extends EventEmitter {}

export const eventBus = new EventBus();

interface DomainEvent {
  type: string;
  payload: any;
  timestamp: number;
  source: string;
  merchantId?: string;
}

export const emitEvent = (type: string, payload: any, source: string, merchantId?: string) => {
  const event: DomainEvent = {
    type,
    payload,
    timestamp: Date.now(),
    source,
    merchantId
  };
  
  eventBus.emit(type, event);
  console.log(`[EventBus] Emitted event: ${type}`, event);
};

export default eventBus;
