import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { lazy, useState } from 'react';
import { Route, Routes, useLocation, useNavigate } from 'react-router-dom';
import zhCN from 'antd/es/locale/zh_CN';
import { ConfigProvider, Result } from 'antd';
import { useRequest } from 'ahooks';
import { getPath, UserMenuContext, analysisTree } from './utils';
import { setStorage, logoutOut } from '@component/utils';
import Layout from './layout';
import { isEmpty, last, includes, map } from 'lodash';
import { getUserInfo } from 'userContext';
import routes from 'routes';
export default () => {
    const location = useLocation();
    const navigate = useNavigate();
    const { loading, data = {} } = useRequest(getUserInfo, {
        onSuccess({ userInfo, menu }) {
            if (userInfo) {
                setStorage(userInfo);
            }
            if (isEmpty(userInfo)) {
                navigate('/404');
                return null;
            }
            if (isEmpty(menu)) {
                navigate('/404');
                return null;
            }
            else {
                const paths = location.pathname.split('/');
                const item = last(paths);
                if (item === 'app') {
                    const path = getPath(menu);
                    navigate(path, {
                        replace: true,
                    });
                }
            }
        },
        onError(ERROR) {
            console.log(ERROR, 'ERROR');
            logoutOut(navigate, 'logoutOut');
        },
    });
    if (loading) {
        return null;
    }
    const menuPath = analysisTree(data.menu, 'path');
    const authRoutes = routes.filter((item) => includes(menuPath, '/app/' + item.auth));
    return (_jsx(UserMenuContext.Provider, Object.assign({ value: data }, { children: _jsx(ConfigProvider, Object.assign({ locale: zhCN }, { children: _jsxs(Routes, { children: [_jsx(Route, Object.assign({ path: "/", element: _jsx(Layout, {}) }, { children: authRoutes.map((item) => (_jsx(Route, { path: item.path, element: item.element }, item.path))) })), _jsx(Route, { path: "*", element: _jsx(Result, { status: "404", title: "404" }) })] }) })) })));
};