"use strict";

exports.__esModule = true;
exports.default = void 0;
var _reactIntl = require("react-intl");
var _react = require("react");
var _NotificationContext = require("../components/MaterialUI/Feedback/NotificationContext");
var _useRequestState = _interopRequireDefault(require("./useRequestState"));
var _sharedMessages = _interopRequireDefault(require("../sharedMessages"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 the notification message for deletes and updates.
    It uses useRequestState internally to know when to display messages.

    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 useNotificationRequestState = function useNotificationRequestState(_ref) {
  var keys = _ref.keys,
    operation = _ref.operation,
    successMessageId = _ref.successMessageId,
    successMessageValues = _ref.successMessageValues,
    successAction = _ref.successAction,
    errorMessageId = _ref.errorMessageId,
    errorMessageValues = _ref.errorMessageValues,
    errorAction = _ref.errorAction;
  var _useIntl = (0, _reactIntl.useIntl)(),
    formatMessage = _useIntl.formatMessage;
  var _useContext = (0, _react.useContext)(_NotificationContext.NotificationContext),
    addNotification = _useContext.addNotification;
  var onSuccess = (0, _react.useCallback)(function () {
    var message = formatMessage(successMessageId, successMessageValues);
    addNotification(message, "success");
    if (successAction) {
      successAction();
    }
    // addNotification causes issues in the deps
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [formatMessage, successAction, successMessageId, successMessageValues]);
  var onError = (0, _react.useCallback)(function (errorResponse) {
    var _errorResponse$respon, _errorResponse$respon2;
    var errorMessage = (_errorResponse$respon = errorResponse == null || (_errorResponse$respon2 = errorResponse.response) == null || (_errorResponse$respon2 = _errorResponse$respon2.responseStatus) == null ? void 0 : _errorResponse$respon2.message) != null ? _errorResponse$respon : null;
    var message = formatMessage(errorMessageId, _objectSpread(_objectSpread({}, errorMessageValues), {}, {
      errorMessage: errorMessage != null ? errorMessage : formatMessage(_sharedMessages.default.errorUnknown)
    }));
    addNotification(message, "error");
    if (errorAction) {
      errorAction(errorResponse);
    }
  },
  // addNotification causes issues in the deps
  // eslint-disable-next-line react-hooks/exhaustive-deps
  [errorAction, errorMessageId, errorMessageValues, formatMessage]);
  var buildRequestState = (0, _useRequestState.default)({
    keys: keys,
    operation: operation,
    successAction: onSuccess,
    errorAction: onError
  });
  return buildRequestState;
};
__signature__(useNotificationRequestState, "useIntl{{ formatMessage }}\nuseContext{{ addNotification }}\nuseCallback{onSuccess}\nuseCallback{onError}\nuseRequestState{buildRequestState}", function () {
  return [_reactIntl.useIntl, _useRequestState.default];
});
__signature__(useNotificationRequestState, "useIntl{{ formatMessage }}\nuseContext{{ addNotification }}\nuseCallback{onSuccess}\nuseCallback{onError}\nuseRequestState{buildRequestState}", function () {
  return [_reactIntl.useIntl, _useRequestState.default];
});
var _default = useNotificationRequestState;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(useNotificationRequestState, "useNotificationRequestState", "/home/vsts/work/1/s/src/hooks/useNotificationRequestState.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/hooks/useNotificationRequestState.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(useNotificationRequestState, "useNotificationRequestState", "/home/vsts/work/1/s/src/hooks/useNotificationRequestState.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/hooks/useNotificationRequestState.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();