"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.initialModalsState = exports.initialModalState = exports.getModalState = exports.draggableModalReducer = void 0;
var _getWindowSize = require("./getWindowSize");
var _clamp = require("./clamp");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
var mapObject = function mapObject(o, f) {
  return Object.assign.apply(Object, [{}].concat(_toConsumableArray(Object.keys(o).map(function (k) {
    return _defineProperty({}, k, f(o[k]));
  }))));
};

// ID for a specific modal.

// State for a specific modal.

// State of all modals.

var initialModalsState = exports.initialModalsState = {
  maxZIndex: 0,
  windowSize: (0, _getWindowSize.getWindowSize)(),
  modals: {}
};
var initialModalState = exports.initialModalState = {
  x: 0,
  y: 0,
  width: 800,
  height: 800,
  zIndex: 0,
  visible: false
};
var getInitialModalState = function getInitialModalState(_ref2) {
  var _ref2$initialWidth = _ref2.initialWidth,
    initialWidth = _ref2$initialWidth === void 0 ? initialModalState.width : _ref2$initialWidth,
    _ref2$initialHeight = _ref2.initialHeight,
    initialHeight = _ref2$initialHeight === void 0 ? initialModalState.height : _ref2$initialHeight;
  return _objectSpread(_objectSpread({}, initialModalState), {}, {
    width: initialWidth,
    height: initialHeight
  });
};
var getModalState = exports.getModalState = function getModalState(_ref3) {
  var state = _ref3.state,
    id = _ref3.id,
    initialWidth = _ref3.initialWidth,
    initialHeight = _ref3.initialHeight;
  return state.modals[id] || getInitialModalState({
    initialWidth: initialWidth,
    initialHeight: initialHeight
  });
};
var getNextZIndex = function getNextZIndex(state, id) {
  return getModalState({
    state: state,
    id: id
  }).zIndex === state.maxZIndex ? state.maxZIndex : state.maxZIndex + 1;
};
var clampDrag = function clampDrag(windowWidth, windowHeight, x, y, width, height) {
  var maxX = windowWidth - width;
  var maxY = windowHeight - height;
  var clampedX = (0, _clamp.clamp)(0, maxX, x);
  var clampedY = (0, _clamp.clamp)(0, maxY, y);
  return {
    x: clampedX,
    y: clampedY
  };
};
var clampResize = function clampResize(windowWidth, windowHeight, x, y, width, height) {
  var maxWidth = windowWidth - x;
  var maxHeight = windowHeight - y;
  var clampedWidth = (0, _clamp.clamp)(200, maxWidth, width);
  var clampedHeight = (0, _clamp.clamp)(200, maxHeight, height);
  return {
    width: clampedWidth,
    height: clampedHeight
  };
};
var draggableModalReducer = exports.draggableModalReducer = function draggableModalReducer(state, action) {
  switch (action.type) {
    case 'resize':
      var size = clampResize(state.windowSize.width, state.windowSize.height, action.x, action.y, action.width, action.height);
      return _objectSpread(_objectSpread({}, state), {}, {
        maxZIndex: getNextZIndex(state, action.id),
        modals: _objectSpread(_objectSpread({}, state.modals), {}, _defineProperty({}, action.id, _objectSpread(_objectSpread(_objectSpread({}, state.modals[action.id]), size), {}, {
          zIndex: getNextZIndex(state, action.id)
        })))
      });
    case 'drag':
      return _objectSpread(_objectSpread({}, state), {}, {
        maxZIndex: getNextZIndex(state, action.id),
        modals: _objectSpread(_objectSpread({}, state.modals), {}, _defineProperty({}, action.id, _objectSpread(_objectSpread(_objectSpread({}, state.modals[action.id]), clampDrag(state.windowSize.width, state.windowSize.height, action.x, action.y, state.modals[action.id].width, state.modals[action.id].height)), {}, {
          zIndex: getNextZIndex(state, action.id)
        })))
      });
    case 'show':
      {
        var _modalState = state.modals[action.id];
        var centerX = state.windowSize.width / 2 - _modalState.width / 2;
        var centerY = state.windowSize.height / 2 - _modalState.height / 2;
        var position = clampDrag(state.windowSize.width, state.windowSize.height, centerX, centerY, _modalState.width, _modalState.height);
        var _size = clampResize(state.windowSize.width, state.windowSize.height, position.x, position.y, _modalState.width, _modalState.height);
        return _objectSpread(_objectSpread({}, state), {}, {
          maxZIndex: state.maxZIndex + 1,
          modals: _objectSpread(_objectSpread({}, state.modals), {}, _defineProperty({}, action.id, _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, _modalState), position), _size), {}, {
            zIndex: state.maxZIndex + 1,
            visible: true
          })))
        });
      }
    case 'focus':
      var modalState = state.modals[action.id];
      return _objectSpread(_objectSpread({}, state), {}, {
        maxZIndex: state.maxZIndex + 1,
        modals: _objectSpread(_objectSpread({}, state.modals), {}, _defineProperty({}, action.id, _objectSpread(_objectSpread({}, modalState), {}, {
          zIndex: state.maxZIndex + 1
        })))
      });
    case 'hide':
      {
        var _modalState2 = state.modals[action.id];
        return _objectSpread(_objectSpread({}, state), {}, {
          modals: _objectSpread(_objectSpread({}, state.modals), {}, _defineProperty({}, action.id, _objectSpread(_objectSpread({}, _modalState2), {}, {
            visible: false
          })))
        });
      }
    case 'mount':
      var initialState = getInitialModalState(action.intialState);
      return _objectSpread(_objectSpread({}, state), {}, {
        maxZIndex: state.maxZIndex + 1,
        modals: _objectSpread(_objectSpread({}, state.modals), {}, _defineProperty({}, action.id, _objectSpread(_objectSpread({}, initialState), {}, {
          x: state.windowSize.width / 2 - initialState.width / 2,
          y: state.windowSize.height / 2 - initialState.height / 2,
          zIndex: state.maxZIndex + 1
        })))
      });
    case 'unmount':
      var modalsClone = _objectSpread({}, state.modals);
      delete modalsClone[action.id];
      return _objectSpread(_objectSpread({}, state), {}, {
        modals: modalsClone
      });
    case 'windowResize':
      return _objectSpread(_objectSpread({}, state), {}, {
        windowSize: action.size,
        modals: mapObject(state.modals, function (modalState) {
          if (!modalState.visible) {
            return modalState;
          }
          var position = clampDrag(state.windowSize.width, state.windowSize.height, modalState.x, modalState.y, modalState.width, modalState.height);
          var size = clampResize(state.windowSize.width, state.windowSize.height, position.x, position.y, modalState.width, modalState.height);
          return _objectSpread(_objectSpread(_objectSpread({}, modalState), position), size);
        })
      });
    default:
      throw new Error();
  }
};