import { useState, useEffect, useCallback } from 'react';

export const useZenMode = () => {
  const [isZenMode, setIsZenMode] = useState(false);

  const toggleZenMode = useCallback(() => {
    setIsZenMode(prev => !prev);
  }, []);

  const exitZenMode = useCallback(() => {
    setIsZenMode(false);
  }, []);

  useEffect(() => {
    const handleKeyDown = (e: KeyboardEvent) => {
      // Toggle with 'Z' key (not in input fields)
      if (e.key.toLowerCase() === 'z' && 
          !['INPUT', 'TEXTAREA'].includes((e.target as HTMLElement).tagName) &&
          !e.ctrlKey && !e.metaKey && !e.altKey) {
        toggleZenMode();
      }
      // Exit with Escape
      if (e.key === 'Escape' && isZenMode) {
        exitZenMode();
      }
    };

    window.addEventListener('keydown', handleKeyDown);
    return () => window.removeEventListener('keydown', handleKeyDown);
  }, [isZenMode, toggleZenMode, exitZenMode]);

  return { isZenMode, toggleZenMode, exitZenMode };
};
