"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.promise.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = require("react");
const useUpdateEffect = (effect, deps) => {
  const isMounted = (0, _react.useRef)(false);
  (0, _react.useEffect)(() => {
    if (!isMounted.current) {
      isMounted.current = true;
    } else {
      return effect();
    }
    return () => undefined;
  }, deps);
};
function useDebounceFn(fn, deps, wait) {
  // eslint-disable-next-line no-underscore-dangle
  const hooksDeps = Array.isArray(deps) ? deps : [];
  // eslint-disable-next-line no-underscore-dangle
  const hookWait = typeof deps === 'number' ? deps : wait || 0;
  const timer = (0, _react.useRef)();
  const fnRef = (0, _react.useRef)(fn);
  fnRef.current = fn;
  const cancel = (0, _react.useCallback)(() => {
    if (timer.current) {
      clearTimeout(timer.current);
    }
  }, []);
  const run = (0, _react.useCallback)(async (...args) => {
    return new Promise(resolve => {
      cancel();
      timer.current = setTimeout(async () => {
        await fnRef.current(...args);
        resolve();
      }, hookWait);
    });
  }, [hookWait, cancel]);
  useUpdateEffect(() => {
    run();
    return cancel;
  }, [...hooksDeps, run]);
  (0, _react.useEffect)(() => cancel, []);
  return {
    run,
    cancel
  };
}
var _default = useDebounceFn;
exports.default = _default;