export interface Command {
  readonly type: string;
  readonly [key: string]: unknown;
}

type CommandHandler<T extends Command = Command> = (cmd: T) => void;

export class CommandBus {
  private handlers = new Map<string, CommandHandler>();

  register<T extends Command>(type: string, handler: CommandHandler<T>): void {
    this.handlers.set(type, handler as CommandHandler);
  }

  execute(cmd: Command): void {
    const handler = this.handlers.get(cmd.type);
    if (!handler) throw new Error(`No handler for command: ${cmd.type}`);
    handler(cmd);
  }

  has(type: string): boolean {
    return this.handlers.has(type);
  }
}
