/**
 * KRA - Window Resize Hook
 * Monitor window resize events
 */
import { useState, useEffect, useCallback } from 'react';

interface WindowSize {
  width: number;
  height: number;
}

export function useWindowResize(callback?: (width: number, height: number) => void): WindowSize {
  const [size, setSize] = useState<WindowSize>({
    width: typeof window !== 'undefined' ? window.innerWidth : 0,
    height: typeof window !== 'undefined' ? window.innerHeight : 0,
  });

  const handleResize = useCallback(() => {
    const width = window.innerWidth;
    const height = window.innerHeight;
    setSize({ width, height });
    callback?.(width, height);
  }, [callback]);

  useEffect(() => {
    // Initial call
    handleResize();

    window.addEventListener('resize', handleResize, { passive: true });
    return () => window.removeEventListener('resize', handleResize);
  }, [handleResize]);

  return size;
}

export default useWindowResize;
