"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useThrottleValue = useThrottleValue;
var _react = require("react");
var _throttle = _interopRequireDefault(require("lodash/throttle"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
 * Use this hook for debounced parameters, for example for useQuery
 * @param value
 * @param delay
 * @return debounced value
 * @see https://github.com/uidotdev/usehooks/blob/90fbbb4cc085e74e50c36a62a5759a40c62bb98e/index.js#L239-L253
 */
function useThrottleValue(value, delay) {
  const [debouncedValue, setDebouncedValue] = (0, _react.useState)(value);
  const onChangeThrottled = (0, _react.useRef)((0, _throttle.default)(setDebouncedValue, delay));
  (0, _react.useEffect)(() => {
    onChangeThrottled.current?.cancel?.();
    onChangeThrottled.current = (0, _throttle.default)(setDebouncedValue, delay);
    return () => onChangeThrottled.current?.cancel?.();
  }, [delay]);
  (0, _react.useEffect)(() => {
    onChangeThrottled.current?.(value);
  }, [value]);
  return debouncedValue;
}