"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

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

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

var _react = require("react");

var _ramda = require("ramda");

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

var useDebounce = function useDebounce(value, delay) {
  var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

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

  var _useState = (0, _react.useState)(value),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      state = _useState2[0],
      dispatch = _useState2[1];

  var _useDebouncedCallback = (0, _useDebouncedCallback3["default"])((0, _react.useCallback)(function (value) {
    return dispatch(value);
  }, []), delay, restOptions),
      _useDebouncedCallback2 = (0, _slicedToArray2["default"])(_useDebouncedCallback, 3),
      callback = _useDebouncedCallback2[0],
      cancel = _useDebouncedCallback2[1],
      callPending = _useDebouncedCallback2[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;