'use client';

/**
 * `@djangocfg/ui-tools/audio-player` subpath entrypoint.
 *
 * `LazyPlayer` is the only heavy export — it dynamically imports the full
 * `Player` tree (PlayerShell + Layout + Cover + Waveform + Controls + audio
 * decoding helpers). Everything else listed here is light:
 *   - types are erased,
 *   - the Zustand store, context hooks, and selectors carry no UI,
 *   - the slot components (`Cover`, `Title`, `PlayButton`, `Waveform`, …)
 *     are presentational React components that read from PlayerContext —
 *     they only become meaningful inside a `<PlayerProvider>` (which only
 *     exists inside `LazyPlayer` once loaded).
 *
 * Consumers building a fully custom layout: render `<LazyPlayer>` once to
 * get the provider, then arrange slot components inside via render-children
 * pattern, or use the bare `Player` re-exported from the root barrel.
 */

import { createLazyComponent } from '../../components';
import type { PlayerProps } from './types';

// ============================================================================
// Lazy heavy component
// ============================================================================

export const LazyPlayer = createLazyComponent<PlayerProps>(
  () => import('./Player').then((mod) => ({ default: mod.Player })),
  {
    displayName: 'LazyAudioPlayer',
    fallback: (
      <div className="rounded-lg border border-border/60 bg-card px-4 py-6 text-sm text-muted-foreground">
        Loading audio player…
      </div>
    ),
  },
);

// ============================================================================
// Light surface — types, store, context, slot components, hooks
// ============================================================================

// Types
export type {
  PlayerProps,
  PlayerState,
  PlayerStateKind,
  PlayerControls,
  PlayerHandle,
  WaveformConfig,
  WaveformMode,
  ReactiveCoverMode,
  PlayerVariant,
  PlayerErrorReason,
} from './types';

// Context provider + selector hooks (no UI)
export {
  PlayerProvider,
  usePlayerAudio,
  usePlayerControls,
  usePlayerDuration,
  usePlayerLevels,
  usePlayerMeta,
  usePlayerPaused,
  usePlayerState,
} from './context';

// Cross-instance store (active player, preferences)
export {
  setActivePlayer,
  getActivePlayer,
  getLastActivePlayer,
  subscribeActivePlayer,
  getPreferences,
  setStoredVolume,
  setStoredMuted,
  subscribePreferences,
  type PlayerPreferences,
} from './store';

// Store-backed hooks
export {
  useActivePlayer,
  useLastActivePlayer,
  useIsActivePlayer,
} from './hooks/useActivePlayer';
export { usePlayerPreferences } from './hooks/usePlayerPreferences';

// Peak cache helpers
export { clearPeaksCache, setPeaks } from './audio';

// Slot components — presentational, read from PlayerContext. Safe to
// re-export synchronously: they don't import the heavy Player tree
// (audio decoding, layouts, shell) — only context selectors and types.
export { Cover, CoverPlaceholder, ReactivePulse } from './parts/Cover';
export { Title, Artist, TimeDisplay } from './parts/Meta';
export {
  PlayButton,
  SkipButton,
  VolumeControl,
  LoopButton,
  ControlsRow,
  IconButton,
} from './parts/Controls';
export {
  Waveform,
  PeaksWaveform,
  LiveWaveform,
  BarsWaveform,
  ProgressBar,
  WaveformSkeleton,
} from './parts/Waveform';
export { ErrorState } from './parts/ErrorState';
export { DefaultLayout, CompactLayout } from './parts/Layout';
