"use strict";

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

var _react = require("react");

var _utils = require("../utils");

var _useRefState2 = _interopRequireDefault(require("./use-ref-state"));

var _useUnmountedRef = _interopRequireDefault(require("./use-unmounted-ref"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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); }

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

  var unmountedRef = (0, _useUnmountedRef.default)();

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

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

var _default = useSetState;
exports.default = _default;