"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.modalReducer = exports.MODAL_INITIAL_STATE = void 0;
var tslib_1 = require("tslib");
var actions_1 = require("../constants/actions");
var common_1 = require("../api/common");
var immutability_helper_1 = tslib_1.__importDefault(require("immutability-helper"));
function tryRestoreModalSizeAndPosition(modal, prevModal) {
    if (prevModal === null || prevModal === void 0 ? void 0 : prevModal.position) {
        modal.position = prevModal.position;
    }
    else if (!modal.position) {
        modal.position = common_1.DEFAULT_MODAL_POSITION;
    }
    if (prevModal === null || prevModal === void 0 ? void 0 : prevModal.size) {
        modal.size = prevModal.size;
    }
    else if (!modal.size) {
        modal.size = common_1.DEFAULT_MODAL_SIZE;
    }
}
exports.MODAL_INITIAL_STATE = {};
function modalReducer(state, action) {
    var _a, _b;
    if (state === void 0) { state = exports.MODAL_INITIAL_STATE; }
    switch (action.type) {
        case actions_1.ActionType.MODAL_SHOW_COMPONENT:
            {
                var newModal = action.payload.modal;
                tryRestoreModalSizeAndPosition(newModal, (_a = state[action.payload.name]) === null || _a === void 0 ? void 0 : _a.modal);
                var newData = {};
                newData[action.payload.name] = {
                    modal: newModal,
                    component: action.payload.component,
                    componentProps: action.payload.componentProps
                };
                return tslib_1.__assign(tslib_1.__assign({}, state), newData);
            }
        case actions_1.ActionType.MODAL_UPDATE:
            {
                var newData = {};
                newData[action.payload.name] = {
                    modal: {
                        "$merge": {
                            size: [action.payload.args.width, action.payload.args.height],
                            position: [action.payload.args.x, action.payload.args.y]
                        }
                    }
                };
                var newState = (0, immutability_helper_1.default)(state, newData);
                return newState;
            }
        case actions_1.ActionType.MODAL_SHOW_URL:
            {
                var _c = action.payload, newModal = _c.modal, rest = tslib_1.__rest(_c, ["modal"]);
                tryRestoreModalSizeAndPosition(newModal, (_b = state[action.payload.name]) === null || _b === void 0 ? void 0 : _b.modal);
                var newData = {};
                newData[action.payload.name] = tslib_1.__assign({ modal: newModal }, rest);
                return tslib_1.__assign(tslib_1.__assign({}, state), newData);
            }
        case actions_1.ActionType.MODAL_CLOSE:
            {
                var newState = tslib_1.__assign({}, state);
                delete newState[action.payload].modal.backdrop;
                delete newState[action.payload].modal.overflowYScroll;
                delete newState[action.payload].modal.title;
                // Delete all non-modal properties
                for (var k in newState[action.payload]) {
                    if (k != "modal") {
                        delete newState[action.payload][k];
                    }
                }
                return newState;
            }
    }
    return state;
}
exports.modalReducer = modalReducer;
//# sourceMappingURL=modal.js.map