import { useState, useCallback } from 'react';

const METRO_SOUND_LOOKUP = {
  0: "BuiltIn",
  1: "SineWave",
  2: "Beep",
  3: "Click",
  4: "Kick",
  5: "Snare",
  6: "MetroFile"
};

const useMetronomeState = () => {
  const [metronomeState, setMetronomeState] = useState({
    isOpen: false,
    bpm: 120,
    cricket: false,
    meter: 1,
    playback: 1,
    sound: 2,
    soundName: "Beep"
  });

  const updateMetronomeState = useCallback((updates) => {
    setMetronomeState(prev => {
      const newState = { ...prev, ...updates };

      // Update sound name based on sound number
      if (updates.sound !== undefined) {
        newState.soundName = METRO_SOUND_LOOKUP[updates.sound] || "Beep";
      }

      return newState;
    });
  }, []);

  const openMetronome = useCallback((settings = {}) => {
    updateMetronomeState({
      isOpen: true,
      ...settings
    });
  }, [updateMetronomeState]);

  const closeMetronome = useCallback(() => {
    setMetronomeState(prev => ({
      ...prev,
      isOpen: false
    }));
  }, []);

  const resetMetronome = useCallback(() => {
    setMetronomeState({
      isOpen: false,
      bpm: 120,
      cricket: false,
      meter: 1,
      playback: 1,
      sound: 2,
      soundName: "Beep"
    });
  }, []);

  return {
    metronomeState,
    updateMetronomeState,
    openMetronome,
    closeMetronome,
    resetMetronome
  };
};

export default useMetronomeState;
