// Event bus for letramento activities.
// Activities run inside a sandboxed iframe and communicate via postMessage.
// AtividadeRuntimeFrame intercepts those messages and calls letramentoEventBus.dispatch()
// so React components can subscribe without coupling to window directly.

export const LETRAMENTO_EVENTS = {
  STARTED: 'started',
  RUNNING: 'running',
  SUCCESS: 'success',
  FAILURE: 'failure',
  COMPLETED: 'completed',
};

class LetramentoEventBus extends EventTarget {
  dispatch(eventName, detail = {}) {
    this.dispatchEvent(new CustomEvent(eventName, { detail }));
  }

  started(detail)   { this.dispatch(LETRAMENTO_EVENTS.STARTED,   detail); }
  running(detail)   { this.dispatch(LETRAMENTO_EVENTS.RUNNING,   detail); }
  success(detail)   { this.dispatch(LETRAMENTO_EVENTS.SUCCESS,   detail); }
  failure(detail)   { this.dispatch(LETRAMENTO_EVENTS.FAILURE,   detail); }
  completed(detail) { this.dispatch(LETRAMENTO_EVENTS.COMPLETED, detail); }
}

export const letramentoEventBus = new LetramentoEventBus();
