import { useEffect, useState } from "react";
const DEFAULT_DELAY = 500;

const useDebouncedValue = (
  value = "",
  delay = DEFAULT_DELAY
) => {
  const [debouncedValue, setDebouncedValue] = useState(value);

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

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

  return debouncedValue;
};

export default useDebouncedValue;
