"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const executeAction_1 = require("./executeAction");
const findRouteByKey_1 = require("./findRouteByKey");
exports.historyToActionObject = (router, BB) => {
    const configs = BB.Configs.getValue('plugin.react-router.navigationConfigs');
    const enableSource = BB.Configs.getValue('plugin.react-router.enableSourceInNavigationActions');
    if (!router.match) {
        throw Error('An error occurent in React Router Plugn. We did not find match object');
    }
    const params = Object.assign({}, router.location.state, router.match.params);
    const obj = findRouteByKey_1.findRouteByKey(router.match.path, 'path', configs);
    const actions = {
        navigate: (routeName, _params) => {
            return executeAction_1.executeAction(configs, router.history.push, routeName, _params);
        },
        push: (routeName, _params) => {
            return executeAction_1.executeAction(configs, router.history.push, routeName, _params);
        },
        replace: (routeName, _params) => {
            return executeAction_1.executeAction(configs, router.history.replace, routeName, _params);
        },
        pop: (steps = 0) => {
            router.history.go(steps === 0 ? 0 : steps * -1);
        },
        goBack: () => {
            router.history.goBack();
        },
        setParams: (_params) => {
            router.history.replace(router.match.path, Object.assign({}, params, _params));
        },
        getParam: (key, defaultValue) => {
            return params[key] || defaultValue;
        },
        source: enableSource ? router : undefined,
        state: {
            key: router.location.key,
            params,
            routeName: obj ? obj.name : '',
            search: router.location.search,
            url: router.match.url,
        },
    };
    return actions;
};
