'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 */

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

var NOTIFICATION_ID_VALIDATION = exports.NOTIFICATION_ID_VALIDATION = 'NOTIFICATION_ID_VALIDATION';

var modalCloseCallbacks = {};

var showNotification = exports.showNotification = function showNotification(notificationDescriptor, notificationID) {
  return {
    type: SHOW_NOTIFICATION,
    payload: notificationDescriptor,
    meta: {
      notificationID: notificationID
    }
  };
};

var removeNotification = exports.removeNotification = function removeNotification(notificationID) {
  return {
    type: REMOVE_NOTIFICATION,
    meta: {
      notificationID: notificationID
    }
  };
};

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

var removeValidationNotification = exports.removeValidationNotification = function removeValidationNotification() {
  return removeNotification(NOTIFICATION_ID_VALIDATION);
};

var openModal = exports.openModal = function openModal(name, onClose) {
  return function (dispatch, getState) {
    var 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.

        var composedOnClose = void 0;

        var pendingOnClose = modalCloseCallbacks[openModalName];

        delete modalCloseCallbacks[openModalName];

        if (pendingOnClose && onClose) {
          composedOnClose = function composedOnClose() {
            onClose();
            pendingOnClose();
          };
        } else if (pendingOnClose) {
          composedOnClose = pendingOnClose;
        } else {
          composedOnClose = onClose;
        }

        modalCloseCallbacks[name] = composedOnClose;
      } else {
        modalCloseCallbacks[name] = onClose;
      }

      dispatch({
        type: OPEN_MODAL,
        meta: {
          name: name
        }
      });
    }
  };
};

var closeModal = exports.closeModal = function closeModal(result) {
  return function (dispatch, getState) {
    var modalName = (0, _reducers.getOpenModalName)(getState());

    dispatch({
      type: CLOSE_MODAL
    });

    var onClose = modalCloseCallbacks[modalName];

    delete modalCloseCallbacks[modalName];

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