// components/useDebounceEffect.ts
import { useEffect, useRef } from "react";

export function useDebounceEffect(
  fn: () => void,
  waitTime: number,
  deps?: any[]
) {
  const timeoutRef = useRef<number>(0);

  useEffect(() => {
    if (timeoutRef.current) {
      clearTimeout(timeoutRef.current);
    }

    timeoutRef.current = window.setTimeout(() => {
      fn();
    }, waitTime);

    return () => {
      if (timeoutRef.current) {
        clearTimeout(timeoutRef.current);
      }
    };
  }, deps);
}
