import type { ChadClient, Consumer, Producer } from '#shared/types'
import type { EventType } from 'mitt'
import mitt from 'mitt'

export interface AppEvents extends Record<EventType, unknown> {
  'socket:connected': void
  'socket:disconnected': void
  'socket:authenticated': { socketId: string }

  'client:added': ChadClient
  'client:removed': ChadClient
  'client:updated': { socketId: string, oldClient: ChadClient, updatedClient: Partial<ChadClient> }

  'consumer:added': Consumer
  'consumer:removed': Consumer
  'consumer:paused': Consumer
  'consumer:resumed': Consumer

  'producer:added': Producer
  'producer:removed': Producer
  'producer:paused': Producer
  'producer:resumed': Producer

  'audio:muted': void
  'audio:unmuted': void
  'output:muted': void
  'output:unmuted': void
  'video:enabled': void
  'video:disabled': void
  'share:enabled': void
  'share:disabled': void

  'chat:new-message': void
}

const emitter = mitt<AppEvents>()

export function useEventBus() {
  return {
    emit: emitter.emit,
    on: emitter.on,
    off: emitter.off,
  }
}
