"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const components_1 = require("@bluebase/components");
const react_1 = tslib_1.__importStar(require("react"));
const helpers_1 = require("./helpers");
const helpers_2 = require("../../helpers");
const core_1 = require("@bluebase/core");
const components_2 = require("../../components");
const react_router_dom_1 = require("react-router-dom");
exports.DrawerNavigator = (props) => {
    const { children, routes, contentComponent: ContentComponent } = props;
    // Extract Contexts
    const { theme } = core_1.useTheme();
    const location = react_router_dom_1.useLocation();
    const { navigator: mainNavigator } = react_1.useContext(components_2.MainNavigatorContext);
    const navigation = core_1.useNavigation();
    const screenProps = helpers_2.useScreenProps();
    // Resolve active tab index
    const currentIndex = routes.findIndex((route) => route.path === location.pathname);
    // Navigation State
    const navigationState = {
        index: currentIndex,
        routes: routes.map((route, index) => {
            const options = helpers_2.resolveRouteOptions(route, props, mainNavigator, {
                navigation,
                screenProps,
                route: Object.assign(Object.assign({}, route), { params: navigation.state.params }),
            });
            return {
                index,
                options,
                path: route.path,
                routeName: route.name,
            };
        }),
    };
    const renderNavigationView = () => {
        const { routes: navRoutes } = navigationState;
        if (ContentComponent) {
            return react_1.default.createElement(ContentComponent, { navigation: navigation });
        }
        return (react_1.default.createElement(components_1.ScrollView, { style: { flex: 1 }, contentContainerStyle: { flex: 1 } },
            react_1.default.createElement(components_1.SafeAreaView, null,
                react_1.default.createElement(components_1.DrawerSection, null, navRoutes.map((r) => {
                    const title = helpers_1.getTitle(r.options);
                    const icon = helpers_1.getIcon(r.options);
                    const onPress = () => navigation.navigate(r.routeName, navigation.state.params);
                    return react_1.default.createElement(components_1.DrawerItem, { key: r.routeName, title: title, icon: icon, onPress: onPress });
                })))));
    };
    return (react_1.default.createElement(components_1.View, { style: {
            backgroundColor: theme.palette.background.default,
            flex: 1,
        } },
        react_1.default.createElement(components_1.DrawerLayout, Object.assign({}, props, { renderNavigationView: renderNavigationView }), children)));
};
exports.DrawerNavigator.displayName = 'DrawerNavigator';
