"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.overlayModalsHideAll = exports.OVERLAY_MODAL_HIDE_ALL = exports.modalToggle = exports.MODAL_TOGGLE = void 0;

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, 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 MODAL_TOGGLE = 'MODAL_TOGGLE';
exports.MODAL_TOGGLE = MODAL_TOGGLE;

var modalToggle = function modalToggle(_ref) {
  var selectedModalId = _ref.selectedModalId,
      hasOverlay = _ref.hasOverlay;
  return {
    type: MODAL_TOGGLE,
    selectedModalId: selectedModalId,
    hasOverlay: hasOverlay
  };
};

exports.modalToggle = modalToggle;
var OVERLAY_MODAL_HIDE_ALL = 'OVERLAY_MODAL_HIDE_ALL';
exports.OVERLAY_MODAL_HIDE_ALL = OVERLAY_MODAL_HIDE_ALL;

var overlayModalsHideAll = function overlayModalsHideAll() {
  return {
    type: OVERLAY_MODAL_HIDE_ALL
  };
};

exports.overlayModalsHideAll = overlayModalsHideAll;
var INITIAL_STATE = {
  activeModalIds: [],
  activeOverlayModalIds: []
};

var _default = function _default() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : INITIAL_STATE;
  var action = arguments.length > 1 ? arguments[1] : undefined;

  switch (action.type) {
    case MODAL_TOGGLE:
      {
        var activeModalIds = state.activeModalIds,
            activeOverlayModalIds = state.activeOverlayModalIds;
        var selectedModalId = action.selectedModalId,
            hasOverlay = action.hasOverlay;
        return _objectSpread({}, state, {
          activeModalIds: !hasOverlay ? !activeModalIds.includes(selectedModalId) ? [].concat(_toConsumableArray(activeModalIds), [selectedModalId]) : activeModalIds.filter(function (id) {
            return id !== selectedModalId;
          }) : activeModalIds,
          activeOverlayModalIds: !hasOverlay ? activeOverlayModalIds : !activeOverlayModalIds.includes(selectedModalId) ? [].concat(_toConsumableArray(activeOverlayModalIds), [selectedModalId]) : activeOverlayModalIds.filter(function (id) {
            return id !== selectedModalId;
          })
        });
      }

    case OVERLAY_MODAL_HIDE_ALL:
      {
        return _objectSpread({}, state, {
          activeOverlayModalIds: []
        });
      }

    default:
      return state;
  }
};

exports.default = _default;