'use client'; import { useState, useRef, useEffect } 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; autoPlay?: boolean; } export default function AudioPlayer({ src, unlockedSeconds, startTime = 0, onPlay, autoPlay = false }: AudioPlayerProps) { const audioRef = useRef(null); const [isPlaying, setIsPlaying] = useState(false); const [progress, setProgress] = useState(0); useEffect(() => { if (audioRef.current) { audioRef.current.pause(); audioRef.current.currentTime = startTime; setIsPlaying(false); setProgress(0); if (autoPlay) { const playPromise = audioRef.current.play(); if (playPromise !== undefined) { playPromise .then(() => { setIsPlaying(true); onPlay?.(); }) .catch(error => { console.log("Autoplay prevented:", error); setIsPlaying(false); }); } } } }, [src, unlockedSeconds, startTime, autoPlay]); const togglePlay = () => { if (!audioRef.current) return; if (isPlaying) { audioRef.current.pause(); } else { audioRef.current.play(); onPlay?.(); } 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 (