'use client'; import { useEffect, useId, useMemo, useRef, useState } from 'react'; import type { ReactNode } from 'react'; import { unlockAudioContext } from '../audio/audioContext'; import { getPeaksFromCache, setPeaks } from '../audio/peaksCache'; import { registerPlayer, setActivePlayer } from '../store/activePlayerBus'; import { createLevelsStore } from '../store/createLevelsStore'; import type { LevelsStore } from '../store/createLevelsStore'; import { getPreferences, setStoredMuted, setStoredVolume, subscribePreferences, } from '../store/preferencesStore'; import { clamp } from '../utils/clamp'; import type { PlayerControls, PlayerErrorReason } from '../types'; import { AudioRefCtx } from './AudioRefContext'; import { ControlsCtx } from './ControlsContext'; import { LevelsCtx } from './LevelsContext'; import { MetaCtx, type PlayerMeta } from './MetaContext'; import { StateCtx } from './StateContext'; import { createAudioSnapshotSource, useAudioElementState } from '../hooks/useAudioElementEvents'; export type PlayerProviderProps = { src: string; peaks?: Float32Array; title?: string; artist?: string; album?: string; cover?: string; autoplay?: boolean; loop?: boolean; initialVolume?: number; muted?: boolean; preload?: 'none' | 'metadata' | 'auto'; exclusive?: boolean; onPrev?: () => void; onNext?: () => void; onPlay?: () => void; onPause?: () => void; onEnded?: () => void; onError?: (reason: PlayerErrorReason, e: unknown) => void; children: ReactNode; }; function createAudioElement(): HTMLAudioElement { const a = new Audio(); a.crossOrigin = 'anonymous'; a.preload = 'metadata'; return a; } export function PlayerProvider(props: PlayerProviderProps) { const { src, peaks: peaksProp, title, artist, album, cover, autoplay = false, loop = false, initialVolume, muted: mutedProp, preload = 'metadata', exclusive = true, onPrev, onNext, onPlay, onPause, onEnded, onError, children, } = props; // Explicit props win and opt the player out of the persisted preference sync. // Otherwise we read defaults from the shared store and stay subscribed so a // change in one player is reflected in every other player on the page. const volumeIsControlled = initialVolume !== undefined; const mutedIsControlled = mutedProp !== undefined; const playerId = useId(); //