"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.showValidationNotification = exports.showNotification = exports.removeValidationNotification = exports.removeNotification = exports.openModal = exports.closeModal = exports.NOTIFICATION_ID_VALIDATION = void 0;
var _validationHelpers = require("../helpers/validationHelpers");
var _reducers = require("../reducers");
var _notificationStatusCodes = require("../constants/notificationStatusCodes");
var _actionCodes = require("../constants/actionCodes");
/* global window */

const NOTIFICATION_ID_VALIDATION = 'NOTIFICATION_ID_VALIDATION';
exports.NOTIFICATION_ID_VALIDATION = NOTIFICATION_ID_VALIDATION;
const modalCloseCallbacks = {};
const showNotification = (notificationDescriptor, notificationID) => ({
  type: _actionCodes.SHOW_NOTIFICATION,
  payload: notificationDescriptor,
  meta: {
    notificationID
  }
});
exports.showNotification = showNotification;
const removeNotification = notificationID => ({
  type: _actionCodes.REMOVE_NOTIFICATION,
  meta: {
    notificationID
  }
});
exports.removeNotification = removeNotification;
const showValidationNotification = (recordType, csid) => (dispatch, getState) => dispatch(showNotification({
  recordType,
  csid,
  type: 'validation',
  date: new Date(),
  status: (0, _validationHelpers.hasBlockingError)((0, _reducers.getRecordValidationErrors)(getState(), csid)) ? _notificationStatusCodes.STATUS_ERROR : _notificationStatusCodes.STATUS_WARNING
}, NOTIFICATION_ID_VALIDATION));
exports.showValidationNotification = showValidationNotification;
const removeValidationNotification = () => removeNotification(NOTIFICATION_ID_VALIDATION);
exports.removeValidationNotification = removeValidationNotification;
const openModal = (name, onClose) => (dispatch, getState) => {
  const openModalName = (0, _reducers.getOpenModalName)(getState());
  if (openModalName !== name) {
    if (openModalName) {
      // There's another modal open. Make sure its callback gets called when this modal is closed.
      // This lets us transfer control from one modal to another simply by opening the new one
      // without closing the previous one, since all onClose callbacks will get called at the end.
      // This is used when navigating away from movement records with unsaved changes -- first the
      // confirm navigation modal is shown, and if save is selected, control is transferred to the
      // lock modal.

      let composedOnClose;
      const pendingOnClose = modalCloseCallbacks[openModalName];
      delete modalCloseCallbacks[openModalName];
      if (pendingOnClose && onClose) {
        composedOnClose = () => {
          onClose();
          pendingOnClose();
        };
      } else if (pendingOnClose) {
        composedOnClose = pendingOnClose;
      } else {
        composedOnClose = onClose;
      }
      modalCloseCallbacks[name] = composedOnClose;
    } else {
      modalCloseCallbacks[name] = onClose;
    }
    dispatch({
      type: _actionCodes.OPEN_MODAL,
      meta: {
        name
      }
    });
  }
};
exports.openModal = openModal;
const closeModal = result => (dispatch, getState) => {
  const modalName = (0, _reducers.getOpenModalName)(getState());
  dispatch({
    type: _actionCodes.CLOSE_MODAL
  });
  const onClose = modalCloseCallbacks[modalName];
  delete modalCloseCallbacks[modalName];
  if (onClose) {
    window.setTimeout(() => {
      onClose(result);
    }, 0);
  }
};
exports.closeModal = closeModal;