function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

import { useCallback } from 'react';
import { isFunction } from '../utils';
import useRefState from './use-ref-state';
import useUnmountedRef from './use-unmounted-ref';

var useSetState = function useSetState(initialState) {
  if (initialState === void 0) {
    initialState = {};
  }

  var unmountedRef = useUnmountedRef();

  var _useRefState = useRefState(initialState),
      state = _useRefState[0],
      setState = _useRefState[1],
      ref = _useRefState[2];

  var setMergeState = useCallback(function (patch) {
    if (unmountedRef.current) return;
    setState(function (prevState) {
      return _extends({}, prevState, isFunction(patch) ? patch(prevState) : patch);
    });
  }, []);
  return [state, setMergeState, ref];
};

export default useSetState;