"use strict";

exports.__esModule = true;
exports.useDispatchWithErrorHandling = exports.executeDispatchWithErrorHandling = exports.default = void 0;
var _reactRedux = require("react-redux");
var _react = require("react");
var _responseProcessingHelper = require("../utils/responseProcessingHelper");
var _globalErrorMessages = require("../actions/globalErrorMessages");
var _reactIntl = require("react-intl");
(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;
};
var executeDispatchWithErrorHandling = exports.executeDispatchWithErrorHandling = function executeDispatchWithErrorHandling(_ref) {
  var dispatch = _ref.dispatch,
    action = _ref.action,
    errorTitle = _ref.errorTitle,
    errorDescription = _ref.errorDescription,
    validationLookupModule = _ref.validationLookupModule,
    validationLookupName = _ref.validationLookupName,
    formatMessage = _ref.formatMessage,
    formatDate = _ref.formatDate,
    formatTime = _ref.formatTime,
    _ref$lookupKeyCustomi = _ref.lookupKeyCustomizer,
    lookupKeyCustomizer = _ref$lookupKeyCustomi === void 0 ? null : _ref$lookupKeyCustomi;
  return dispatch(action).then(function (data) {
    var extractedMessages = (0, _responseProcessingHelper.extractStandardErrorMessagesFromResponse)(data, validationLookupModule, validationLookupName, formatMessage, formatDate, formatTime, lookupKeyCustomizer);
    if (extractedMessages.hasErrors) {
      var newMsg = {
        messages: extractedMessages.messages
      };
      if (extractedMessages.messages.length > 0) {
        newMsg.title = errorDescription;
      } else {
        newMsg.title = errorTitle;
        newMsg.description = errorDescription;
      }
      dispatch((0, _globalErrorMessages.pushGlobalErrorMessage)(newMsg));
    }
    return data;
  });
};
var useDispatchWithErrorHandling = exports.useDispatchWithErrorHandling = function useDispatchWithErrorHandling() {
  var dispatch = (0, _reactRedux.useDispatch)();
  var _useIntl = (0, _reactIntl.useIntl)(),
    formatMessage = _useIntl.formatMessage,
    formatDate = _useIntl.formatDate,
    formatTime = _useIntl.formatTime;
  return (0, _react.useCallback)(function (_ref2) {
    var action = _ref2.action,
      errorTitle = _ref2.errorTitle,
      errorDescription = _ref2.errorDescription,
      validationLookupModule = _ref2.validationLookupModule,
      validationLookupName = _ref2.validationLookupName,
      lookupKeyCustomizer = _ref2.lookupKeyCustomizer;
    return executeDispatchWithErrorHandling({
      dispatch: dispatch,
      action: action,
      errorTitle: errorTitle,
      errorDescription: errorDescription,
      validationLookupModule: validationLookupModule,
      validationLookupName: validationLookupName,
      formatMessage: formatMessage,
      formatDate: formatDate,
      formatTime: formatTime,
      lookupKeyCustomizer: lookupKeyCustomizer
    });
  }, [dispatch, formatDate, formatMessage, formatTime]);
};
__signature__(useDispatchWithErrorHandling, "useDispatch{dispatch}\nuseIntl{{ formatMessage, formatDate, formatTime }}\nuseCallback{}", function () {
  return [_reactRedux.useDispatch, _reactIntl.useIntl];
});
__signature__(useDispatchWithErrorHandling, "useDispatch{dispatch}\nuseIntl{{ formatMessage, formatDate, formatTime }}\nuseCallback{}", function () {
  return [_reactRedux.useDispatch, _reactIntl.useIntl];
});
var _default = useDispatchWithErrorHandling;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(executeDispatchWithErrorHandling, "executeDispatchWithErrorHandling", "/home/vsts/work/1/s/src/hooks/useDispatchWithErrorHandling.js");
  reactHotLoader.register(useDispatchWithErrorHandling, "useDispatchWithErrorHandling", "/home/vsts/work/1/s/src/hooks/useDispatchWithErrorHandling.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/hooks/useDispatchWithErrorHandling.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(executeDispatchWithErrorHandling, "executeDispatchWithErrorHandling", "/home/vsts/work/1/s/src/hooks/useDispatchWithErrorHandling.js");
  reactHotLoader.register(useDispatchWithErrorHandling, "useDispatchWithErrorHandling", "/home/vsts/work/1/s/src/hooks/useDispatchWithErrorHandling.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/hooks/useDispatchWithErrorHandling.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();