"use client";
import React, { createContext, useCallback } from 'react';

const SoundContext = createContext(undefined);

export function SoundProvider({ children }) {
  const [enabled, setEnabled] = React.useState(true);

  const playSound = useCallback(() => {
    if (!enabled) return;
    const audio = new Audio();
    audio.play().catch((err) => console.error('Failed to play sound:', err));
  }, [enabled]);

  return (
    <SoundContext.Provider value={{ playSound, setEnabled }}>
      {children}
    </SoundContext.Provider>
  );
}

export function useSound() {
  const context = React.useContext(SoundContext);
  if (!context) {
    throw new Error('useSound must be used within SoundProvider');
  }
  return context;
}
