"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.templateReducer = exports.setCustomTemplateReducer = exports.isElementState = exports.TEMPLATE_INITIAL_STATE = void 0;
var tslib_1 = require("tslib");
var actions_1 = require("../constants/actions");
exports.TEMPLATE_INITIAL_STATE = {
    initialInfoPaneWidth: 250,
    initialTaskPaneWidth: 300,
    taskPaneVisible: true,
    selectionPanelVisible: true,
    legendVisible: true,
    autoDisplaySelectionPanelOnSelection: true,
    templateData: {}
};
/**
 * Checks if the given payload is an element state
 *
 * @export
 * @param {*} args
 * @returns {args is IElementState}
 */
function isElementState(args) {
    return args != null
        && typeof (args.legendVisible) != 'undefined'
        && typeof (args.selectionPanelVisible) != 'undefined'
        && typeof (args.taskPaneVisible) != 'undefined';
}
exports.isElementState = isElementState;
var _ovReducer;
/**
 * Installs a custom template reducer function. This is generally used by viewer templates
 * which need to respond to template-related actions in a way that is unique to the template
 *
 * @export
 * @param {ReducerFunction<ITemplateReducerState>} func
 */
function setCustomTemplateReducer(func) {
    _ovReducer = func;
}
exports.setCustomTemplateReducer = setCustomTemplateReducer;
function templateReducer(origState, action) {
    if (origState === void 0) { origState = exports.TEMPLATE_INITIAL_STATE; }
    var state = origState;
    //Whether this reducer has been overridden or not, service the MAP_SET_SELECTION action and
    //if we get modified state, pass that down to the rest of the reducer
    if (action.type == actions_1.ActionType.MAP_SET_SELECTION) {
        var selection = action.payload.selection;
        if (selection && selection.SelectedFeatures) {
            if (selection.SelectedFeatures.SelectedLayer.length && origState.autoDisplaySelectionPanelOnSelection) {
                state = tslib_1.__assign(tslib_1.__assign({}, origState), { selectionPanelVisible: true, legendVisible: false, taskPaneVisible: false });
            }
        }
    }
    if (action.type == actions_1.ActionType.MAP_ADD_CLIENT_SELECTED_FEATURE) {
        var feature = action.payload.feature;
        if (feature === null || feature === void 0 ? void 0 : feature.properties) {
            state = tslib_1.__assign(tslib_1.__assign({}, origState), { selectionPanelVisible: true, legendVisible: false, taskPaneVisible: false });
        }
    }
    if (action.type == actions_1.ActionType.FUSION_SET_TEMPLATE_CUSTOM_DATA) {
        var state1 = tslib_1.__assign({}, state);
        state1.templateData[action.payload.name] = action.payload.value;
        return state1;
    }
    if (typeof (_ovReducer) == 'function') {
        return _ovReducer(origState, state, action);
    }
    else {
        //If no template present (they would've overridden this reducer), at least service the actions we know affect this
        //particular state branch
        switch (action.type) {
            case actions_1.ActionType.FUSION_SET_ELEMENT_STATE:
                {
                    if (isElementState(action.payload)) {
                        return tslib_1.__assign(tslib_1.__assign({}, state), action.payload);
                    }
                    return state;
                }
            case actions_1.ActionType.INIT_APP:
                {
                    var state1 = {};
                    if (action.payload.initialTaskPaneWidth) {
                        state1.initialTaskPaneWidth = action.payload.initialTaskPaneWidth;
                    }
                    if (action.payload.initialInfoPaneWidth) {
                        state1.initialInfoPaneWidth = action.payload.initialInfoPaneWidth;
                    }
                    return tslib_1.__assign(tslib_1.__assign({}, state), state1);
                }
        }
    }
    return state;
}
exports.templateReducer = templateReducer;
//# sourceMappingURL=template.js.map