'use strict';

const utils = require('../../utils.cjs');
const React = require('react');

function useDebounce(value, delay) {
  const [debouncedValue, setDebouncedValue] = React.useState(value);
  React.useEffect(() => {
    const timer = setTimeout(() => setDebouncedValue(value), delay || 500);
    return () => {
      clearTimeout(timer);
    };
  }, [value, delay]);
  return debouncedValue;
}
function useThrottle(value, wait) {
  const [throttledValue, setThrottledValue] = React.useState(value);
  const valueRef = React.useRef(value);
  valueRef.current = value;
  const throttledSetter = React.useMemo(() => {
    return utils.throttle((next) => {
      setThrottledValue(next);
    }, wait ?? 500);
  }, [wait]);
  React.useEffect(() => {
    throttledSetter(valueRef.current);
    return () => {
      throttledSetter.cancel();
    };
  }, [throttledSetter]);
  React.useEffect(() => {
    throttledSetter(value);
  }, [value, throttledSetter]);
  return throttledValue;
}

exports.useDebounce = useDebounce;
exports.useThrottle = useThrottle;
