'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) {
    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);
    }
  };
};