import { useEffect } from 'react'

export const useKeyboardShortcuts = (handlers: {
  onOpenSpotlight: () => void
  onCloseSpotlight: () => void
  onNavigateUp: () => void
  onNavigateDown: () => void
  onNavigateLeft: () => void
  onNavigateRight: () => void
  onSelect: () => void
}) => {
  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      if (e.ctrlKey || e.metaKey) return

      switch (e.key) {
        case 'Escape':
          handlers.onCloseSpotlight()
          break
        case 'k':
          handlers.onNavigateUp()
          break
        case 'j':
          handlers.onNavigateDown()
          break
        case 'h':
          handlers.onNavigateLeft()
          break
        case 'l':
          handlers.onNavigateRight()
          break
        case 'Enter':
          handlers.onSelect()
          break
      }
    }

    window.addEventListener('keydown', handleKeyDown)
    return () => window.removeEventListener('keydown', handleKeyDown)
  }, [handlers])
}
