"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 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({}, router.match.params, params));
        },
        getParam: (key, defaultValue) => {
            return router.match.params[key] || defaultValue;
        },
        source: enableSource ? router : undefined,
        state: {
            key: router.location.key,
            params: Object.assign({}, router.location.state, router.match.params),
            routeName: obj ? obj.name : '',
            search: router.location.search,
            url: router.match.url,
        },
    };
    return actions;
};
