'use client'; import { useState, useRef, useEffect, forwardRef, useImperativeHandle } from 'react'; interface AudioPlayerProps { src: string; unlockedSeconds: number; // 2, 4, 7, 11, 16, 30 (or full length) startTime?: number; // Start offset in seconds (for curated specials) onPlay?: () => void; onReplay?: () => void; autoPlay?: boolean; onHasPlayedChange?: (hasPlayed: boolean) => void; } export interface AudioPlayerRef { play: () => void; } const AudioPlayer = forwardRef(({ src, unlockedSeconds, startTime = 0, onPlay, onReplay, autoPlay = false, onHasPlayedChange }, ref) => { const audioRef = useRef(null); const [isPlaying, setIsPlaying] = useState(false); const [progress, setProgress] = useState(0); const [hasPlayedOnce, setHasPlayedOnce] = useState(false); useEffect(() => { if (audioRef.current) { audioRef.current.pause(); audioRef.current.currentTime = startTime; setIsPlaying(false); setProgress(0); setHasPlayedOnce(false); // Reset for new segment onHasPlayedChange?.(false); // Notify parent if (autoPlay) { const playPromise = audioRef.current.play(); if (playPromise !== undefined) { playPromise .then(() => { setIsPlaying(true); onPlay?.(); setHasPlayedOnce(true); onHasPlayedChange?.(true); // Notify parent }) .catch(error => { console.log("Autoplay prevented:", error); setIsPlaying(false); }); } } } }, [src, unlockedSeconds, startTime, autoPlay]); // Expose play method to parent component useImperativeHandle(ref, () => ({ play: () => { if (!audioRef.current) return; const playPromise = audioRef.current.play(); if (playPromise !== undefined) { playPromise .then(() => { setIsPlaying(true); onPlay?.(); if (!hasPlayedOnce) { setHasPlayedOnce(true); onHasPlayedChange?.(true); } }) .catch(error => { console.error("Play failed:", error); setIsPlaying(false); }); } } })); const togglePlay = () => { if (!audioRef.current) return; if (isPlaying) { audioRef.current.pause(); } else { audioRef.current.play(); onPlay?.(); if (hasPlayedOnce) { onReplay?.(); } else { setHasPlayedOnce(true); onHasPlayedChange?.(true); // Notify parent } } setIsPlaying(!isPlaying); }; const handleTimeUpdate = () => { if (!audioRef.current) return; const current = audioRef.current.currentTime; const elapsed = current - startTime; const percent = (elapsed / unlockedSeconds) * 100; setProgress(Math.min(percent, 100)); if (elapsed >= unlockedSeconds) { audioRef.current.pause(); audioRef.current.currentTime = startTime; setIsPlaying(false); setProgress(0); } }; return (