"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.taskPaneReducer = exports.TASK_PANE_INITIAL_STATE = void 0;
var tslib_1 = require("tslib");
var actions_1 = require("../constants/actions");
exports.TASK_PANE_INITIAL_STATE = {
    navIndex: -1,
    navigation: [],
    initialUrl: undefined,
    //Having this state sounds extremely hacky, but we need a way to signal to the "dumb" task pane that
    //the url its about to receive was pushed and should not be reloaded into the internal iframe
    lastUrlPushed: false
};
function mergeNavigatedUrl(state, url) {
    var index = state.navIndex;
    var nav = state.navigation;
    index++;
    nav[index] = url;
    //If we slotted at a position that is not the end of the array
    //remove everything after it
    if (nav.length > index + 1) {
        nav.splice(index + 1);
    }
    var newState = {
        navIndex: index,
        navigation: nav,
        lastUrlPushed: false
    };
    return tslib_1.__assign(tslib_1.__assign({}, state), newState);
}
function taskPaneReducer(state, action) {
    if (state === void 0) { state = exports.TASK_PANE_INITIAL_STATE; }
    switch (action.type) {
        case actions_1.ActionType.INIT_APP:
            {
                var payload = action.payload;
                var newState = {
                    initialUrl: payload.initialUrl,
                    navIndex: 0,
                    navigation: [payload.initialUrl]
                };
                return tslib_1.__assign(tslib_1.__assign({}, state), newState);
            }
        case actions_1.ActionType.TASK_PANE_HOME:
            {
                if (state.initialUrl != null) {
                    return mergeNavigatedUrl(state, state.initialUrl);
                }
                return state;
            }
        case actions_1.ActionType.TASK_PANE_BACK:
            {
                var index = state.navIndex;
                var nav = state.navigation;
                index--;
                var newState = {
                    navIndex: index,
                    navigation: nav,
                    lastUrlPushed: false
                };
                return tslib_1.__assign(tslib_1.__assign({}, state), newState);
            }
        case actions_1.ActionType.TASK_PANE_FORWARD:
            {
                var index = state.navIndex;
                var nav = state.navigation;
                index++;
                var newState = {
                    navIndex: index,
                    navigation: nav,
                    lastUrlPushed: false
                };
                return tslib_1.__assign(tslib_1.__assign({}, state), newState);
            }
        case actions_1.ActionType.TASK_PANE_PUSH_URL:
            {
                var payload = action.payload;
                var index = state.navIndex;
                var nav = state.navigation;
                //if (areUrlsSame(nav[index], action.payload.url)) {
                //    return state;
                //}
                nav[index + 1] = payload.url;
                //If we slotted at a position that is not the end of the array
                //remove everything after it
                if (nav.length > index + 2) {
                    nav.splice(index + 2);
                }
                if (payload.silent === true) {
                    var newState = {
                        navigation: nav
                    };
                    return tslib_1.__assign(tslib_1.__assign({}, state), newState);
                }
                else {
                    var newState = {
                        navIndex: index + 1,
                        navigation: nav,
                        lastUrlPushed: true
                    };
                    return tslib_1.__assign(tslib_1.__assign({}, state), newState);
                }
            }
        case actions_1.ActionType.TASK_INVOKE_URL:
            {
                return mergeNavigatedUrl(state, action.payload.url);
            }
    }
    return state;
}
exports.taskPaneReducer = taskPaneReducer;
//# sourceMappingURL=taskpane.js.map