import { useEffect, useState } from 'preact/hooks';

/**
 * useDebouncedValue (core hook)
 * Debounces a rapidly changing value to avoid spamming network requests.
 */
export function useDebouncedValue<T>(value: T, delayMs: number): T {
  const [debounced, setDebounced] = useState<T>(value);

  useEffect(() => {
    const t = window.setTimeout(() => setDebounced(value), Math.max(0, delayMs || 0));
    return () => window.clearTimeout(t);
  }, [value, delayMs]);

  return debounced;
}
