"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useThrottle = useThrottle;
var _throttle = _interopRequireDefault(require("lodash/throttle"));
var _react = require("react");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
 * Interfaces from Lodash
 * @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/ef8f9bb5ba32fb36ff978ac9898cc4f35cf7a9d6/types/lodash/common/function.d.ts#L1358-L1376
 */

/**
 * A hook which replaces imperative throttle from Lodash and works with React lifecycle
 * @param callback a function, **should** be wrapped in useCallback
 * @param delay in milliseconds
 * @param options check throttle options for description. Hook does not react to change of this object,
 * so it is not required to wrap it in useMemo or define outside component
 * @returns throttled function which has cancel and flush methods, always the same instance of a function.
 * @see https://lodash.com/docs/4.17.15#throttle
 */
function useThrottle(callback, delay, options) {
  const savedCallback = (0, _react.useRef)((0, _throttle.default)(callback, delay, options));
  (0, _react.useEffect)(() => {
    savedCallback.current?.cancel();
    savedCallback.current = (0, _throttle.default)(callback, delay, options);
    return () => savedCallback?.current?.cancel();
  }, [delay, callback]);
  const resultFunction = (0, _react.useCallback)((...args) => savedCallback.current(...args), []);
  if (!resultFunction.cancel) {
    resultFunction.cancel = () => savedCallback.current?.cancel?.();
    resultFunction.flush = () => savedCallback.current?.flush?.();
  }
  return resultFunction;
}