import type { EventBus } from './EventBus';

// Placeholder beat authority: fixed-BPM beat:tick until LANE-JUICE's TechnoEngine
// replaces it (same event, same payload shape — consumers must not care which).
export class StubBeatClock {
  private accMs = 0;
  private beatIndex = 0;
  private totalMs = 0;
  private running = false;

  constructor(
    private readonly bus: EventBus,
    public readonly bpm: number = 128,
  ) {}

  get beatIntervalMs(): number {
    return 60_000 / this.bpm;
  }

  start(): void {
    this.running = true;
  }

  stop(): void {
    this.running = false;
  }

  update(deltaMs: number): void {
    if (!this.running) return;
    this.accMs += deltaMs;
    this.totalMs += deltaMs;
    while (this.accMs >= this.beatIntervalMs) {
      this.accMs -= this.beatIntervalMs;
      this.bus.emit('beat:tick', { beatIndex: this.beatIndex++, audioTimeMs: this.totalMs - this.accMs });
    }
  }
}
