'use client';

import { useContext } from 'react';
import type { Context } from 'react';
import type { LevelsStore } from '../store';
import type { PlayerControls, PlayerState } from '../types';
import { AudioRefCtx } from './AudioRefContext';
import { ControlsCtx } from './ControlsContext';
import { LevelsCtx } from './LevelsContext';
import { MetaCtx, type PlayerMeta } from './MetaContext';
import { StateCtx } from './StateContext';

function useCtxOrThrow<T>(ctx: Context<T | null>, name: string): T {
  const value = useContext(ctx);
  if (value === null) {
    throw new Error(`${name} must be used inside <PlayerProvider>`);
  }
  return value;
}

export const usePlayerState = (): PlayerState => useCtxOrThrow(StateCtx, 'usePlayerState');
export const usePlayerControls = (): PlayerControls => useCtxOrThrow(ControlsCtx, 'usePlayerControls');
export const usePlayerLevels = (): LevelsStore => useCtxOrThrow(LevelsCtx, 'usePlayerLevels');
export const usePlayerMeta = (): PlayerMeta => useCtxOrThrow(MetaCtx, 'usePlayerMeta');
export const usePlayerAudio = (): HTMLAudioElement => useCtxOrThrow(AudioRefCtx, 'usePlayerAudio');

// Narrow slices.
export const usePlayerPaused = (): boolean => {
  const s = usePlayerState();
  return s.kind !== 'playing';
};

export const usePlayerDuration = (): number => {
  const s = usePlayerState();
  return 'duration' in s ? s.duration : 0;
};
