import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import { useCallback, useEffect, useRef, useState } from 'react';
import { equals } from 'ramda';
import useDebouncedCallback from './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 ? equals : _ref$equalityFn,
      restOptions = _objectWithoutProperties(_ref, ["equalityFn"]);

  var _useState = useState(value),
      _useState2 = _slicedToArray(_useState, 2),
      state = _useState2[0],
      dispatch = _useState2[1];

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

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

export default useDebounce;