/** @format */

import * as React from 'react';
import {BrowserRouter as Router, Route, Redirect, Switch} from 'react-router-dom';
import KeepAlive from 'react-activation';
import {decodeBase64} from '../utils/base64';
import globalStore from '../service/globalStore';
import Home from '../layout';
import Index from '../pages/login/index';
import NotFound from '../pages/404';
const createRoutes = (data: any[], prefixPath: string) => {
    const routes: React.ReactNode[] = [];
    data.map(element => {
        const {component: Component, ...rest} = element;
        rest.path = `${prefixPath + rest.path}`;
        if (!!element.render) {
            routes.push(<Route {...rest} key={`${rest.path}`} />);
        } else {
            routes.push(
                <Route
                    {...rest}
                    key={`${rest.path}`}
                    component={(props: any) => {
                        const {
                            location: {search, pathname},
                        } = props;
                        return (
                            <KeepAlive name={pathname + search}>
                                <Component {...props} />
                            </KeepAlive>
                        );
                    }}
                />,
            );
        }
        if (element.extraRouter) {
            element.extraRouter.map((element: any) => {
                const {component: Component, ...rest} = element;
                rest.path = `${prefixPath + rest.path}`;
                if (!!rest.render) {
                    routes.push(<Route {...rest} key={`${rest.path}`} />);
                } else {
                    routes.push(
                        <Route
                            {...rest}
                            key={`${rest.path}`}
                            component={(props: any) => {
                                const {
                                    location: {search, pathname},
                                } = props;
                                const id = new URLSearchParams(decodeBase64(search.slice(1))).get('id') || '';
                                return (
                                    <KeepAlive id={id} name={pathname + search}>
                                        <Component {...props} />
                                    </KeepAlive>
                                );
                            }}
                        />,
                    );
                }
            });
        }
        if (element.child.length !== 0) {
            routes.push(...createRoutes(rest.child, rest.path));
        }
    });
    return routes;
};

export default () => {
    const {access} = React.useContext(globalStore);
    return (
        <Router>
            <Switch>
                <Route exact path='/' component={Index} />
                <Route exact path='/home' render={() => <Redirect to='/home/dashBoard' />} />
                <Route path='/home'>
                    <Home>{createRoutes(access.data, '/home')}</Home>
                </Route>
                <Route component={NotFound} />
            </Switch>
        </Router>
    );
};
