import { HashRouter as Router, Route } from 'react-router-dom';
import { Routes } from 'react-router-dom';
import { ConfigProvider } from 'antd';
import zhCN from 'antd/locale/zh_CN';
import enUS from 'antd/locale/en_US';
import { useTranslation } from 'react-i18next';
import mainRoutes from './mainRoute';
import debugRoutes from './debugRoute';
import { useEffect, useState } from 'react';

//定义的路由函数直接挂载到主页面上去
const HRoutes = function () {
  let allRoutes;
  const { i18n } = useTranslation();

  if (import.meta.env.DEV) {
    allRoutes = mainRoutes.concat(debugRoutes);
  } else {
    allRoutes = mainRoutes;
  }
  const [currentLocale, setCurrentLocale] = useState(zhCN); //

  useEffect(() => {
    const localeMap = {
      zh: zhCN,
      en: enUS,
    };
    if (i18n.language) {
      setCurrentLocale(localeMap[i18n.language]);
    }
  }, [i18n.language]);

  return (
    <ConfigProvider locale={currentLocale}>
      {/* <Version /> */}
      <Router>
        <Routes>
          {allRoutes.map((item, index) => {
            return (
              <Route
                key={index}
                path={item.path}
                element={item.component}></Route>
            );
          })}
        </Routes>
      </Router>
    </ConfigProvider>
  );
};
export default HRoutes;
