import * as React from "react";
import { useDidUpdateEffect } from "./useDidUpdateEffect.js";
function useDebounce(throttleTime = 500, hardValue, onChange) {
  const [actualValue, setActualValue] = React.useState(hardValue);
  const [throttledValue, setThrottledValue] = React.useState(hardValue);
  const setValue = React.useCallback(() => {
    setThrottledValue(actualValue);
    onChange == null ? void 0 : onChange(actualValue);
  }, [actualValue, setThrottledValue, onChange]);
  const resetToHardValue = React.useCallback(() => {
    setActualValue(hardValue);
    setThrottledValue(hardValue);
  }, [hardValue, setActualValue, setThrottledValue]);
  React.useEffect(() => {
    resetToHardValue();
  }, [hardValue]);
  React.useEffect(() => {
    const throttleTimer = setTimeout(setValue, throttleTime);
    return () => clearTimeout(throttleTimer);
  }, [actualValue]);
  return [actualValue, setActualValue, throttledValue, resetToHardValue];
}
function useDebounceEffect(fn, ms, deps) {
  const throttleRef = React.useRef();
  useDidUpdateEffect(() => {
    if (throttleRef.current) {
      clearTimeout(throttleRef.current);
    }
    throttleRef.current = setTimeout(() => {
      fn();
    }, ms);
  }, deps);
}
export {
  useDebounce,
  useDebounceEffect
};
//# sourceMappingURL=useDebounce.js.map
