"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const components_1 = require("@bluebase/components");
const core_1 = require("@bluebase/core");
const react_native_1 = require("react-native");
const react_1 = tslib_1.__importDefault(require("react"));
const DrawerLayout = core_1.getComponent('DrawerLayout');
const DrawerNavigationView = (props) => () => {
    const { navigationState } = props;
    const { routes } = navigationState;
    return (react_1.default.createElement(react_native_1.ScrollView, null,
        react_1.default.createElement(react_native_1.SafeAreaView, null,
            react_1.default.createElement(components_1.NavigationActions, null, ({ navigate }) => {
                return routes.map((r) => (react_1.default.createElement(components_1.DrawerItem, { key: r.routeName, title: r.title, onPress: () => navigate(r.routeName) })));
            }))));
};
exports.DrawerView = (props) => {
    const { screen: component, navigationOptions, navigator, styles } = props, rest = tslib_1.__rest(props, ["screen", "navigationOptions", "navigator", "styles"]);
    const { contentOptions, routes, type } = navigator, other = tslib_1.__rest(navigator, ["contentOptions", "routes", "type"]);
    const stylesheet = styles;
    const Component = component; // || DrawerViewContent;
    // Resolve active tab index
    const currentRouteName = props.navigation.state.routeName;
    const currentIndex = navigator.routes.findIndex(route => route.name === currentRouteName);
    // Navigation State
    const navigationState = {
        index: currentIndex,
        routes: navigator.routes.map((route, index) => {
            // Resolve navigationOptions
            const options = core_1.resolveThunk(route.navigationOptions || {}, {
                navigation: props.navigation,
                screenProps: rest
            });
            return {
                index,
                path: route.path,
                routeName: route.name,
                title: options.title,
            };
        })
    };
    return (react_1.default.createElement(components_1.View, { style: stylesheet.root },
        react_1.default.createElement(DrawerLayout, Object.assign({}, other, { renderNavigationView: DrawerNavigationView(Object.assign({}, props, { navigationState })) }), Component ? react_1.default.createElement(Component, Object.assign({}, rest)) : null)));
};
exports.DrawerView.defaultStyles = (theme) => ({
    root: {
        backgroundColor: theme.palette.background.default,
        flex: 1,
    }
});
