"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _react = require("react");

var _ramda = require("ramda");

var _useDebouncedCallback2 = _interopRequireDefault(require("./useDebouncedCallback"));

var useDebounce = function useDebounce(value, delay, _ref) {
  if (_ref === void 0) {
    _ref = {};
  }

  var _ref2 = _ref,
      _ref2$equalityFn = _ref2.equalityFn,
      equalityFn = _ref2$equalityFn === void 0 ? _ramda.equals : _ref2$equalityFn,
      restOptions = (0, _objectWithoutPropertiesLoose2.default)(_ref2, ["equalityFn"]);

  var _useState = (0, _react.useState)(value),
      state = _useState[0],
      dispatch = _useState[1];

  var _useDebouncedCallback = (0, _useDebouncedCallback2.default)((0, _react.useCallback)(function (value) {
    return dispatch(value);
  }, []), delay, restOptions),
      callback = _useDebouncedCallback[0],
      cancel = _useDebouncedCallback[1],
      callPending = _useDebouncedCallback[2];

  var previousValue = (0, _react.useRef)(value);
  (0, _react.useEffect)(function () {
    if (!equalityFn(previousValue.current, value)) {
      callback(value);
      previousValue.current = value;
    }
  }, [value, callback, equalityFn]);
  return [state, cancel, callPending];
};

var _default = useDebounce;
exports.default = _default;