/**
 * useDebouncedValue Hook
 * 
 * Returns a debounced value that updates after the specified delay.
 * Useful for search inputs, filters, and other inputs that trigger expensive operations.
 * 
 * @param value - The value to debounce
 * @param delay - Delay in milliseconds (default: 300)
 * @returns Debounced value
 * 
 * @example
 * const [searchTerm, setSearchTerm] = useState('');
 * const debouncedSearchTerm = useDebouncedValue(searchTerm, 300);
 * 
 * useEffect(() => {
 *   // This will only run when debouncedSearchTerm changes (after 300ms delay)
 *   performSearch(debouncedSearchTerm);
 * }, [debouncedSearchTerm]);
 */
import { useState, useEffect } from 'react';

export function useDebouncedValue<T>(value: T, delay: number = 300): T {
  const [debouncedValue, setDebouncedValue] = useState<T>(value);

  useEffect(() => {
    const handler = setTimeout(() => {
      setDebouncedValue(value);
    }, delay);

    return () => {
      clearTimeout(handler);
    };
  }, [value, delay]);

  return debouncedValue;
}
