import { useCallback, useRef } from 'react';

/**
 * Edge 3.2: Hook for throttling callback functions
 * Limits execution to at most once per specified time period
 *
 * @param callback - The function to throttle
 * @param delay - Minimum time between executions in milliseconds (default: 300ms)
 * @returns Throttled callback function
 *
 * @example
 * ```typescript
 * const handleScroll = useThrottledCallback(() => {
 *   updateScrollPosition();
 * }, 100);
 *
 * // Usage in scroll event
 * window.addEventListener('scroll', handleScroll);
 * ```
 */
export function useThrottledCallback<T extends (...args: any[]) => any>(
  callback: T,
  delay: number = 300,
): T {
  const lastExecTimeRef = useRef<number>(0);
  const timeoutRef = useRef<NodeJS.Timeout | null>(null);

  const throttledCallback = useCallback(
    ((...args: Parameters<T>) => {
      const currentTime = Date.now();
      const timeSinceLastExec = currentTime - lastExecTimeRef.current;

      if (timeSinceLastExec >= delay) {
        // Execute immediately if enough time has passed
        callback(...args);
        lastExecTimeRef.current = currentTime;
      } else {
        // Schedule execution for the remaining time
        if (timeoutRef.current) {
          clearTimeout(timeoutRef.current);
        }

        timeoutRef.current = setTimeout(() => {
          callback(...args);
          lastExecTimeRef.current = Date.now();
        }, delay - timeSinceLastExec);
      }
    }) as T,
    [callback, delay],
  );

  return throttledCallback;
}
