"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path_to_regexp_1 = require("path-to-regexp");
const executeAction_1 = require("./executeAction");
const findRouteByKey_1 = require("./findRouteByKey");
const query_string_1 = tslib_1.__importDefault(require("query-string"));
exports.historyToActionObject = (router, mainNavigator
// BB: BlueBase
) => {
    // const enableSource: boolean = 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', mainNavigator);
    const actions = {
        navigate: (routeName, _params) => {
            return executeAction_1.executeAction(mainNavigator, router.history.push, routeName, _params);
        },
        push: (routeName, _params) => {
            return executeAction_1.executeAction(mainNavigator, router.history.push, routeName, _params);
        },
        replace: (routeName, _params) => {
            return executeAction_1.executeAction(mainNavigator, router.history.replace, routeName, _params);
        },
        pop: (steps = 0) => {
            router.history.go(steps === 0 ? 0 : steps * -1);
        },
        goBack: () => {
            router.history.goBack();
        },
        setParams: (_params, search = false) => {
            const currentState = router.location.state;
            let searchStr = router.history.location.search;
            if (search) {
                const parsed = query_string_1.default.parse(router.history.location.search, { arrayFormat: 'index' });
                const searchItems = Object.assign({}, parsed);
                // If items are objects, JSON.stringify them, otherwise use as is
                Object.keys(_params).forEach(key => (searchItems[key] =
                    typeof _params[key] === 'object' ? JSON.stringify(_params[key]) : _params[key]));
                searchStr = query_string_1.default.stringify(searchItems, { arrayFormat: 'index' });
            }
            const fn = search ? router.history.push : router.history.replace;
            const toPath = path_to_regexp_1.compile(router.match.path);
            const state = Object.assign({}, currentState, params, _params);
            fn({
                pathname: toPath(state),
                search: searchStr,
                state,
            });
        },
        getParam: (key, defaultValue) => {
            return params[key] || defaultValue;
        },
        // source: enableSource ? router : undefined,
        source: router,
        state: {
            key: router.location.key,
            params,
            routeName: obj ? obj.name : '',
            search: router.location.search,
            url: router.match.url,
        },
    };
    return actions;
};
