/** Minimal type declarations for the YouTube IFrame Player API. */

declare namespace YT {
  enum PlayerState {
    UNSTARTED = -1,
    ENDED = 0,
    PLAYING = 1,
    PAUSED = 2,
    BUFFERING = 3,
    CUED = 5,
  }

  interface OnStateChangeEvent {
    data: PlayerState
    target: Player
  }

  interface PlayerVars {
    autoplay?: 0 | 1
    controls?: 0 | 1
    disablekb?: 0 | 1
    fs?: 0 | 1
    modestbranding?: 0 | 1
    rel?: 0 | 1
    playsinline?: 0 | 1
    start?: number
  }

  interface Events {
    onReady?: () => void
    onStateChange?: (event: OnStateChangeEvent) => void
    onError?: (event: { data: number }) => void
  }

  interface PlayerOptions {
    videoId?: string
    width?: number | string
    height?: number | string
    playerVars?: PlayerVars
    events?: Events
  }

  class Player {
    constructor(element: HTMLElement | string, options: PlayerOptions)
    playVideo(): void
    pauseVideo(): void
    stopVideo(): void
    seekTo(seconds: number, allowSeekAhead: boolean): void
    getCurrentTime(): number
    getDuration(): number
    getPlayerState(): PlayerState
    setVolume(volume: number): void
    getVolume(): number
    destroy(): void
  }
}

interface Window {
  YT?: typeof YT & { Player?: typeof YT.Player }
  onYouTubeIframeAPIReady?: (() => void) | null
}
