'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.closeModal = exports.openModal = exports.removeValidationNotification = exports.showValidationNotification = exports.removeNotification = exports.showNotification = exports.NOTIFICATION_ID_VALIDATION = exports.OPEN_MODAL = exports.CLOSE_MODAL = exports.REMOVE_NOTIFICATION = exports.SHOW_NOTIFICATION = undefined;

var _reducers = require('../reducers');

var _notificationStatusCodes = require('../constants/notificationStatusCodes');

/* global window */

const SHOW_NOTIFICATION = exports.SHOW_NOTIFICATION = 'SHOW_NOTIFICATION';
const REMOVE_NOTIFICATION = exports.REMOVE_NOTIFICATION = 'REMOVE_NOTIFICATION';
const CLOSE_MODAL = exports.CLOSE_MODAL = 'CLOSE_MODAL';
const OPEN_MODAL = exports.OPEN_MODAL = 'OPEN_MODAL';

const NOTIFICATION_ID_VALIDATION = exports.NOTIFICATION_ID_VALIDATION = 'NOTIFICATION_ID_VALIDATION';

const modalCloseCallbacks = {};

const showNotification = exports.showNotification = (notificationDescriptor, notificationID) => ({
  type: SHOW_NOTIFICATION,
  payload: notificationDescriptor,
  meta: {
    notificationID
  }
});

const removeNotification = exports.removeNotification = notificationID => ({
  type: REMOVE_NOTIFICATION,
  meta: {
    notificationID
  }
});

const showValidationNotification = exports.showValidationNotification = (recordType, csid) => showNotification({
  recordType,
  csid,
  type: 'validation',
  date: new Date(),
  status: _notificationStatusCodes.STATUS_ERROR
}, NOTIFICATION_ID_VALIDATION);

const removeValidationNotification = exports.removeValidationNotification = () => removeNotification(NOTIFICATION_ID_VALIDATION);

const openModal = exports.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: OPEN_MODAL,
      meta: {
        name
      }
    });
  }
};

const closeModal = exports.closeModal = result => (dispatch, getState) => {
  const modalName = (0, _reducers.getOpenModalName)(getState());

  dispatch({
    type: CLOSE_MODAL
  });

  const onClose = modalCloseCallbacks[modalName];

  delete modalCloseCallbacks[modalName];

  if (onClose) {
    window.setTimeout(() => {
      onClose(result);
    }, 0);
  }
};