"use strict";

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

var _useSelectorAndUnwrap2 = _interopRequireDefault(require("./useSelectorAndUnwrap"));

var _requestStates = require("../selectors/requestStates");

var _react = require("react");

var _reactRedux = require("react-redux");

var _requestState = require("../actions/requestState");

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

(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();

(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();

var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};

var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};

/*
    This hook is used to handle custom action after deletes and updates requests. We can add additional operation if required.
    We have a reducer scanning every request for a special payload (meta.requestState).
    If this payload is detected we set some flags (inProgress, value, error) depending on the request type (_REQUEST, _SUCCESS, _FAILURE):
        * inProgress: set to true while the request is executing
        * value: set to false when starting the request and to true when the request has been executed successfully
        * error: set to false when starting the request and to true when the request has not been executed successfully

    This hook returns a method used to build a requestState object that is used in the meta data property of the dispatched action.
    The expected format for the action is as follow:
    {
        type: 'some type',
        ... // other properties
        meta: {
            requestState: {
                keys: [],
                operation: requestStateOperations.delete
            }
        }
    }
*/
var useRequestState = function useRequestState(_ref) {
  var keys = _ref.keys,
      operation = _ref.operation,
      successAction = _ref.successAction,
      errorAction = _ref.errorAction;

  var _useSelectorAndUnwrap = (0, _useSelectorAndUnwrap2.default)((0, _requestStates.getRequestStateInfo)(operation, keys)),
      inProgress = _useSelectorAndUnwrap.inProgress,
      value = _useSelectorAndUnwrap.value,
      error = _useSelectorAndUnwrap.error,
      errorMessage = _useSelectorAndUnwrap.errorMessage;

  var dispatch = (0, _reactRedux.useDispatch)();
  (0, _react.useEffect)(function () {
    if (value && !inProgress && !error) {
      dispatch((0, _requestState.resetRequestState)(keys, operation));

      if (successAction) {
        successAction();
      }
    } else if (!value && !inProgress && error) {
      dispatch((0, _requestState.resetRequestState)(keys, operation));

      if (errorAction) {
        errorAction(errorMessage);
      }
    }
  }, [dispatch, keys, operation, successAction, errorAction, errorMessage, inProgress, value, error]);

  var buildRequestState = function buildRequestState() {
    return {
      keys: keys,
      operation: operation
    };
  };

  return [buildRequestState];
};

__signature__(useRequestState, "useSelectorAndUnwrap{{ inProgress, value, error, errorMessage }}\nuseDispatch{dispatch}\nuseEffect{}", function () {
  return [_useSelectorAndUnwrap2.default, _reactRedux.useDispatch];
});

__signature__(useRequestState, "useSelectorAndUnwrap{{ inProgress, value, error, errorMessage }}\nuseDispatch{dispatch}\nuseEffect{}", function () {
  return [_useSelectorAndUnwrap2.default, _reactRedux.useDispatch];
});

var _default = useRequestState;
var _default2 = _default;
var _default3 = _default2;
exports.default = _default3;
;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(useRequestState, "useRequestState", "/home/vsts/work/1/s/src/hooks/useRequestState.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/hooks/useRequestState.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();

;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(useRequestState, "useRequestState", "/home/vsts/work/1/s/src/hooks/useRequestState.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/hooks/useRequestState.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();