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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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 (errorMessage) {
    var message = formatMessage(errorMessageId, _objectSpread(_objectSpread({}, errorMessageValues), {}, {
      errorMessage: errorMessage != null ? errorMessage : formatMessage(_sharedMessages.default.errorUnknown)
    }));
    addNotification(message, "error");

    if (errorAction) {
      errorAction(errorMessage);
    }
  }, // 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 = _default2;
exports.default = _default3;
;

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