"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toolbarReducer = exports.TOOLBAR_INITIAL_STATE = void 0;
var tslib_1 = require("tslib");
var immutability_helper_1 = tslib_1.__importDefault(require("immutability-helper"));
var actions_1 = require("../constants/actions");
var template_1 = require("./template");
var constants_1 = require("../constants");
function mergeFlyoutState(flyoutId, state, flyoutPayload, flyoutUpdateAction, closeOtherFlyouts, bSettingStates) {
    if (closeOtherFlyouts === void 0) { closeOtherFlyouts = false; }
    if (bSettingStates === void 0) { bSettingStates = false; }
    var updateSpec = {
        flyouts: {}
    };
    if (!state.flyouts[flyoutId] && bSettingStates) {
        return state;
    }
    var flyoutUpdateSpec = {};
    flyoutUpdateSpec[flyoutUpdateAction] = flyoutPayload;
    updateSpec.flyouts[flyoutId] = flyoutUpdateSpec;
    if (closeOtherFlyouts) {
        //Close others
        for (var key in state.flyouts) {
            if (key != flyoutId) {
                updateSpec.flyouts[key] = {
                    "$merge": {
                        open: false
                    }
                };
            }
        }
    }
    var newState = (0, immutability_helper_1.default)(state, updateSpec);
    return newState;
}
function mergeFlyoutCloseState(flyoutId, state, bSettingStates) {
    if (bSettingStates === void 0) { bSettingStates = false; }
    return mergeFlyoutState(flyoutId, state, {
        open: false,
        metrics: null
    }, "$merge", false, bSettingStates);
}
exports.TOOLBAR_INITIAL_STATE = {
    toolbars: {},
    flyouts: {}
};
function toolbarReducer(state, action) {
    if (state === void 0) { state = exports.TOOLBAR_INITIAL_STATE; }
    switch (action.type) {
        case actions_1.ActionType.INIT_APP:
            {
                return tslib_1.__assign(tslib_1.__assign({}, state), action.payload.toolbars);
            }
        case actions_1.ActionType.COMPONENT_OPEN:
            {
                var payload = action.payload;
                var flyoutId = payload.flyoutId;
                if (flyoutId) {
                    return mergeFlyoutState(flyoutId, state, {
                        open: true,
                        metrics: payload.metrics,
                        componentName: payload.name,
                        componentProps: payload.props
                    }, "$set", //Need to use $set instead of $merge as the tree won't have component flyout info initially
                    true);
                }
                return state;
            }
        case actions_1.ActionType.COMPONENT_CLOSE:
            {
                if (!state.flyouts[constants_1.WEBLAYOUT_CONTEXTMENU]) {
                    return state;
                }
                var flyoutId = action.payload.flyoutId;
                if (flyoutId) {
                    return mergeFlyoutState(flyoutId, state, {
                        open: false,
                        metrics: null,
                        componentName: null,
                        componentProps: null
                    }, "$set"); //Need to use $set instead of $merge as the tree won't have component flyout info initially
                }
                return state;
            }
        case actions_1.ActionType.CONTEXT_MENU_OPEN:
            {
                if (!state.flyouts[constants_1.WEBLAYOUT_CONTEXTMENU]) {
                    return state;
                }
                return mergeFlyoutState(constants_1.WEBLAYOUT_CONTEXTMENU, state, {
                    open: true,
                    metrics: {
                        posX: action.payload.x,
                        posY: action.payload.y
                    }
                }, "$merge", true);
            }
        case actions_1.ActionType.FLYOUT_OPEN:
            {
                var flyoutId = action.payload.flyoutId;
                if (flyoutId) {
                    return mergeFlyoutState(flyoutId, state, {
                        open: true,
                        metrics: action.payload.metrics
                    }, "$merge", true);
                }
                return state;
            }
        case actions_1.ActionType.FUSION_SET_ELEMENT_STATE:
            {
                if ((0, template_1.isElementState)(action.payload) && !action.payload.taskPaneVisible) {
                    return mergeFlyoutCloseState(constants_1.WEBLAYOUT_TASKMENU, state, true);
                }
                return state;
            }
        case actions_1.ActionType.FUSION_SET_TASK_PANE_VISIBILITY:
            {
                if (!action.payload) {
                    return mergeFlyoutCloseState(constants_1.WEBLAYOUT_TASKMENU, state);
                }
                return state;
            }
        case actions_1.ActionType.FLYOUT_CLOSE:
            {
                var flyoutId = action.payload.flyoutId;
                if (flyoutId) {
                    return mergeFlyoutCloseState(flyoutId, state);
                }
            }
        case actions_1.ActionType.CONTEXT_MENU_CLOSE:
            {
                return mergeFlyoutCloseState(constants_1.WEBLAYOUT_CONTEXTMENU, state);
            }
    }
    return state;
}
exports.toolbarReducer = toolbarReducer;
//# sourceMappingURL=toolbar.js.map