{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "audio-player-with-waveform",
  "dependencies": [
    "lucide-react",
    "wavesurfer.js"
  ],
  "registryDependencies": [
    "select",
    "utils"
  ],
  "files": [
    {
      "path": "src/components/watermelon-ui/audio-player-with-waveform.tsx",
      "content": "\"use client\";\n\nimport React, { useState } from \"react\";\nimport { Play, Pause, SkipBack, SkipForward, Loader2 } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\nimport { useAudioWaveform } from \"@/hooks/use-audio-waveform\";\nimport {\n  Select,\n  SelectContent,\n  SelectItem,\n  SelectTrigger,\n  SelectValue,\n} from \"@/components/ui/select\";\n\n// Utility function to format time\nexport const formatTime = (seconds: number): string => {\n  if (!isFinite(seconds) || isNaN(seconds)) {\n    return \"0:00\";\n  }\n  const mins = Math.floor(seconds / 60);\n  const secs = Math.floor(seconds % 60);\n  return `${mins}:${secs.toString().padStart(2, \"0\")}`;\n};\n\n// Controls Component\ninterface ControlsProps {\n  isPlaying: boolean;\n  onPlayPause: () => void;\n  onStop?: () => void;\n  onSkipBackward?: () => void;\n  onSkipForward?: () => void;\n  disabled?: boolean;\n  size?: \"sm\" | \"md\" | \"lg\";\n  className?: string;\n}\n\nconst Controls: React.FC<ControlsProps> = ({\n  isPlaying,\n  onPlayPause,\n  onSkipBackward,\n  onSkipForward,\n  disabled = false,\n  size = \"md\",\n  className,\n}) => {\n  const sizeClasses = {\n    sm: \"h-8 w-8\",\n    md: \"h-10 w-10\",\n    lg: \"h-12 w-12\",\n  };\n\n  const iconSizes = {\n    sm: 16,\n    md: 20,\n    lg: 24,\n  };\n\n  return (\n    <div className={cn(\"flex items-center gap-2\", className)}>\n      {onSkipBackward && (\n        <button\n          onClick={onSkipBackward}\n          disabled={disabled}\n          className={cn(\n            \"flex items-center justify-center rounded-full transition-colors hover:bg-secondary disabled:opacity-50\",\n            sizeClasses[size]\n          )}\n        >\n          <SkipBack size={iconSizes[size]} />\n        </button>\n      )}\n\n      <button\n        onClick={onPlayPause}\n        disabled={disabled}\n        className={cn(\n          \"flex items-center justify-center rounded-full bg-primary text-primary-foreground transition-colors hover:bg-primary/90 disabled:opacity-50\",\n          sizeClasses[size]\n        )}\n      >\n        {isPlaying ? (\n          <Pause size={iconSizes[size]} />\n        ) : (\n          <Play size={iconSizes[size]} />\n        )}\n      </button>\n\n      {onSkipForward && (\n        <button\n          onClick={onSkipForward}\n          disabled={disabled}\n          className={cn(\n            \"flex items-center justify-center rounded-full transition-colors hover:bg-secondary disabled:opacity-50\",\n            sizeClasses[size]\n          )}\n        >\n          <SkipForward size={iconSizes[size]} />\n        </button>\n      )}\n    </div>\n  );\n};\n\n// Main Audio Player With Waveform Component\nexport interface AudioPlayerWithWaveformProps {\n  source: File | Blob | string;\n  title?: string;\n  artist?: string;\n  showTitle?: boolean;\n  showControls?: boolean;\n  showPlaybackSpeed?: boolean;\n  waveColor?: string;\n  progressColor?: string;\n  height?: number;\n  barWidth?: number;\n  barGap?: number;\n  barRadius?: number;\n  onPlay?: () => void;\n  onPause?: () => void;\n  onEnd?: () => void;\n  onError?: (error: Error) => void;\n  className?: string;\n  variant?: \"default\" | \"compact\";\n}\n\nexport const AudioPlayerWithWaveform: React.FC<\n  AudioPlayerWithWaveformProps\n> = ({\n  source,\n  title,\n  artist,\n  showTitle = true,\n  showControls = true,\n  showPlaybackSpeed = true,\n  waveColor = \"#e5e7eb\",\n  progressColor = \"#3b82f6\",\n  height = 64,\n  barWidth = 4,\n  barGap = 8,\n  barRadius = 30,\n  onPlay,\n  onPause,\n  onEnd,\n  onError,\n  className,\n  variant = \"default\",\n}) => {\n  const [audioUrl, setAudioUrl] = useState<string>(\"\");\n\n  // Convert source to URL\n  React.useEffect(() => {\n    if (source instanceof File || source instanceof Blob) {\n      const url = URL.createObjectURL(source);\n      setAudioUrl(url);\n      return () => URL.revokeObjectURL(url);\n    } else if (typeof source === \"string\") {\n      setAudioUrl(source);\n    }\n  }, [source]);\n\n  const {\n    waveformRef,\n    isPlaying,\n    isLoading,\n    duration,\n    currentTime,\n    playPause,\n    stop,\n    setPlaybackRate,\n  } = useAudioWaveform(audioUrl, {\n    waveColor,\n    progressColor,\n    height,\n    barWidth,\n    barGap,\n    barRadius,\n    onPlay,\n    onPause,\n    onEnd,\n    onError,\n  });\n\n  if (variant === \"compact\") {\n    return (\n      <div className={cn(\"rounded-lg border bg-card p-4\", className)}>\n        <div className=\"space-y-3\">\n          {showTitle && (title || artist) && (\n            <div>\n              {title && <p className=\"font-medium text-sm truncate\">{title}</p>}\n              {artist && (\n                <p className=\"text-xs text-muted-foreground truncate\">\n                  {artist}\n                </p>\n              )}\n            </div>\n          )}\n\n          <div className=\"relative\">\n            <div\n              ref={waveformRef}\n              className={cn(\"w-full rounded-lg\", isLoading && \"opacity-50\")}\n            />\n            {isLoading && (\n              <div className=\"absolute inset-0 flex items-center justify-center\">\n                <Loader2 className=\"h-5 w-5 animate-spin text-muted-foreground\" />\n              </div>\n            )}\n          </div>\n\n          <div className=\"flex items-center justify-between\">\n            <div className=\"flex items-center gap-3\">\n              {showControls && (\n                <Controls\n                  isPlaying={isPlaying}\n                  onPlayPause={playPause}\n                  onStop={stop}\n                  disabled={isLoading}\n                  size=\"sm\"\n                />\n              )}\n              <div className=\"text-xs text-muted-foreground font-mono\">\n                {formatTime(currentTime)} / {formatTime(duration)}\n              </div>\n            </div>\n\n            {showPlaybackSpeed && (\n              <Select\n                onValueChange={(value) => setPlaybackRate(parseFloat(value))}\n                defaultValue=\"1\"\n              >\n                <SelectTrigger className=\"w-20 h-7 text-xs\">\n                  <SelectValue />\n                </SelectTrigger>\n                <SelectContent>\n                  <SelectItem value=\"0.5\">0.5x</SelectItem>\n                  <SelectItem value=\"0.75\">0.75x</SelectItem>\n                  <SelectItem value=\"1\">1x</SelectItem>\n                  <SelectItem value=\"1.25\">1.25x</SelectItem>\n                  <SelectItem value=\"1.5\">1.5x</SelectItem>\n                  <SelectItem value=\"2\">2x</SelectItem>\n                </SelectContent>\n              </Select>\n            )}\n          </div>\n        </div>\n      </div>\n    );\n  }\n\n  // Default variant\n  return (\n    <div className={cn(\"rounded-lg border bg-card p-6\", className)}>\n      <div className=\"space-y-4\">\n        {showTitle && (title || artist) && (\n          <div className=\"text-center\">\n            {title && <h3 className=\"font-semibold text-lg\">{title}</h3>}\n            {artist && (\n              <p className=\"text-sm text-muted-foreground\">{artist}</p>\n            )}\n          </div>\n        )}\n\n        <div className=\"relative\">\n          <div\n            ref={waveformRef}\n            className={cn(\"w-full rounded-lg\", isLoading && \"opacity-50\")}\n          />\n          {isLoading && (\n            <div className=\"absolute inset-0 flex items-center justify-center\">\n              <div className=\"flex items-center gap-2 text-muted-foreground\">\n                <Loader2 className=\"h-5 w-5 animate-spin\" />\n                <span className=\"text-sm\">Loading waveform...</span>\n              </div>\n            </div>\n          )}\n        </div>\n\n        <div className=\"flex items-center justify-between\">\n          <div className=\"text-sm text-muted-foreground font-mono\">\n            {formatTime(currentTime)}\n          </div>\n          <div className=\"text-sm text-muted-foreground font-mono\">\n            {formatTime(duration)}\n          </div>\n        </div>\n\n        <div className=\"flex items-center justify-center gap-4\">\n          {showControls && (\n            <Controls\n              isPlaying={isPlaying}\n              onPlayPause={playPause}\n              onStop={stop}\n              disabled={isLoading}\n              size=\"lg\"\n            />\n          )}\n        </div>\n\n        {showPlaybackSpeed && (\n          <div className=\"flex items-center justify-center gap-2\">\n            <label className=\"text-sm text-muted-foreground\">\n              Playback Speed:\n            </label>\n            <Select\n              onValueChange={(value) => setPlaybackRate(parseFloat(value))}\n              defaultValue=\"1\"\n            >\n              <SelectTrigger className=\"w-24\">\n                <SelectValue />\n              </SelectTrigger>\n              <SelectContent>\n                <SelectItem value=\"0.5\">0.5x</SelectItem>\n                <SelectItem value=\"0.75\">0.75x</SelectItem>\n                <SelectItem value=\"1\">1x</SelectItem>\n                <SelectItem value=\"1.25\">1.25x</SelectItem>\n                <SelectItem value=\"1.5\">1.5x</SelectItem>\n                <SelectItem value=\"2\">2x</SelectItem>\n              </SelectContent>\n            </Select>\n          </div>\n        )}\n      </div>\n    </div>\n  );\n};",
      "type": "registry:ui",
      "target": "components/ui/audio-player-with-waveform.tsx"
    },
    {
      "path": "src/hooks/use-audio-waveform.ts",
      "content": "import { useEffect, useRef, useState } from \"react\";\nimport type WaveSurfer from \"wavesurfer.js\";\n\nexport interface UseAudioWaveformOptions {\n  waveColor?: string;\n  progressColor?: string;\n  height?: number;\n  barWidth?: number;\n  barGap?: number;\n  barRadius?: number;\n  normalize?: boolean;\n  onPlay?: () => void;\n  onPause?: () => void;\n  onEnd?: () => void;\n  onError?: (error: Error) => void;\n  onReady?: () => void;\n}\n\nexport interface UseAudioWaveformReturn {\n  waveformRef: React.RefObject<HTMLDivElement | null>;\n  isPlaying: boolean;\n  isLoading: boolean;\n  duration: number;\n  currentTime: number;\n  playPause: () => void;\n  stop: () => void;\n  seek: (time: number) => void;\n  setPlaybackRate: (rate: number) => void;\n}\n\nexport const useAudioWaveform = (\n  audioUrl: string | null,\n  options: UseAudioWaveformOptions = {}\n): UseAudioWaveformReturn => {\n  const {\n    waveColor = \"#e5e7eb\",\n    progressColor = \"#3b82f6\",\n    height = 64,\n    barWidth = 4,\n    barGap = 8,\n    barRadius = 30,\n    normalize = true,\n    onPlay,\n    onPause,\n    onEnd,\n    onError,\n    onReady,\n  } = options;\n\n  const waveformRef = useRef<HTMLDivElement>(null);\n  const wavesurferRef = useRef<WaveSurfer | null>(null);\n  const [isPlaying, setIsPlaying] = useState(false);\n  const [isLoading, setIsLoading] = useState(true);\n  const [duration, setDuration] = useState(0);\n  const [currentTime, setCurrentTime] = useState(0);\n\n  const callbacksRef = useRef({ onPlay, onPause, onEnd, onError, onReady });\n  callbacksRef.current = { onPlay, onPause, onEnd, onError, onReady };\n\n  useEffect(() => {\n    if (!waveformRef.current || !audioUrl) return;\n\n    let wavesurfer: WaveSurfer | null = null;\n\n    // Dynamic import of WaveSurfer\n    import(\"wavesurfer.js\").then((WaveSurferModule) => {\n      const WaveSurfer = WaveSurferModule.default;\n\n      if (!waveformRef.current) return;\n\n      wavesurfer = WaveSurfer.create({\n        container: waveformRef.current,\n        waveColor,\n        progressColor,\n        height,\n        barWidth,\n        barGap,\n        barRadius,\n        cursorWidth: 2,\n        cursorColor: progressColor,\n        normalize,\n        backend: \"WebAudio\",\n        mediaControls: false,\n      });\n\n      wavesurferRef.current = wavesurfer;\n\n      // Load audio\n      wavesurfer.load(audioUrl);\n\n      // Event listeners\n      wavesurfer.on(\"ready\", () => {\n        setIsLoading(false);\n        setDuration(wavesurfer!.getDuration());\n        callbacksRef.current.onReady?.();\n      });\n\n      wavesurfer.on(\"play\", () => {\n        setIsPlaying(true);\n        callbacksRef.current.onPlay?.();\n      });\n\n      wavesurfer.on(\"pause\", () => {\n        setIsPlaying(false);\n        callbacksRef.current.onPause?.();\n      });\n\n      wavesurfer.on(\"finish\", () => {\n        setIsPlaying(false);\n        setCurrentTime(0);\n        callbacksRef.current.onEnd?.();\n      });\n\n      wavesurfer.on(\"audioprocess\", () => {\n        setCurrentTime(wavesurfer!.getCurrentTime());\n      });\n\n      wavesurfer.on(\"seeking\", () => {\n        setCurrentTime(wavesurfer!.getCurrentTime());\n      });\n\n      wavesurfer.on(\"error\", (error: string | Error) => {\n        console.error(\"WaveSurfer error:\", error);\n        setIsLoading(false);\n        callbacksRef.current.onError?.(new Error(String(error)));\n      });\n    });\n\n    // Cleanup\n    return () => {\n      if (wavesurfer) {\n        wavesurfer.destroy();\n      }\n      wavesurferRef.current = null;\n    };\n  }, [\n    audioUrl,\n    waveColor,\n    progressColor,\n    height,\n    barWidth,\n    barGap,\n    barRadius,\n    normalize,\n  ]);\n\n  const playPause = () => {\n    if (wavesurferRef.current) {\n      wavesurferRef.current.playPause();\n    }\n  };\n\n  const stop = () => {\n    if (wavesurferRef.current) {\n      wavesurferRef.current.stop();\n      setCurrentTime(0);\n    }\n  };\n\n  const seek = (time: number) => {\n    if (wavesurferRef.current && duration > 0) {\n      const seekPosition = Math.max(0, Math.min(time / duration, 1));\n      wavesurferRef.current.seekTo(seekPosition);\n    }\n  };\n\n  const setPlaybackRate = (rate: number) => {\n    if (wavesurferRef.current) {\n      wavesurferRef.current.setPlaybackRate(rate);\n    }\n  };\n\n  return {\n    waveformRef,\n    isPlaying,\n    isLoading,\n    duration,\n    currentTime,\n    playPause,\n    stop,\n    seek,\n    setPlaybackRate,\n  };\n};",
      "type": "registry:hook",
      "target": "hooks/use-audio-waveform.ts"
    }
  ],
  "type": "registry:ui"
}
