'use client'
import { useEffect } from 'react'
import { sound } from '@/lib/soundManager';

export default function AudioPrimer() {
  useEffect(() => {
    // Beim ersten User-Gesture: AudioContext entsperren & Sounds vorladen
    const onFirstGesture = async () => {
      await sound.prime();
      if (sound.isPrimed()) {
        await Promise.all([
          sound.load('ready',  '/assets/sounds/cs2_game_ready.wav'),
          sound.load('beep',   '/assets/sounds/beep.wav'),
          sound.load('loading', '/assets/sounds/loading.wav'),
          sound.load('menu_accept', '/assets/sounds/menu_accept.wav'),
        ]);
      }
      window.removeEventListener('pointerdown', onFirstGesture);
      window.removeEventListener('keydown', onFirstGesture);
      window.removeEventListener('touchstart', onFirstGesture);
    };

    window.addEventListener('pointerdown', onFirstGesture, { once: true });
    window.addEventListener('keydown', onFirstGesture, { once: true });
    window.addEventListener('touchstart', onFirstGesture, { once: true });

    return () => {
      window.removeEventListener('pointerdown', onFirstGesture);
      window.removeEventListener('keydown', onFirstGesture);
      window.removeEventListener('touchstart', onFirstGesture);
    };
  }, []);

  return null;
}
