"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const lodash_get_1 = tslib_1.__importDefault(require("lodash.get"));
const core_1 = require("@bluebase/core");
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
function insertChildNavigator(parent, child, locationRouteName) {
    // If routes prop is a thunk, resolve it.
    // Then map it to have new paths
    const routes = core_1.resolveThunk(lodash_get_1.default(parent, 'routes', [])).map((r) => {
        // Do we have a navigator here, if yes then recurcively prepare its paths as well
        const resolvedNavigator = r.navigator
            ? insertChildNavigator(r.navigator, child, locationRouteName)
            : undefined;
        // Return the final object
        return Object.assign(Object.assign({}, r), { navigator: resolvedNavigator });
    });
    const foundIndex = routes.findIndex((route) => route.name === locationRouteName);
    if (foundIndex >= 0) {
        routes[foundIndex] = Object.assign(Object.assign({}, routes[foundIndex]), { navigator: child });
    }
    // Merge and return incoming navigator with newer routes
    return Object.assign(Object.assign({}, parent), { routes });
}
exports.insertChildNavigator = insertChildNavigator;
