import { BrowserRouter, Route, Routes } from "react-router-dom";
import { useEffect } from "react";
import { useDispatch } from "react-redux";
import Layout from "./components/Layout";
import routes from "./routes";
import { mockData } from "./data/mockData";
import { setStudentInfo } from "./store/slices/studentSlice";

// 样式文件导入
import "./global.css";
import "@arco-design/web-react/dist/css/arco.css";

const getAllRoutes = (routes) => {
  const result = [];

  const traverse = (routeItems) => {
    routeItems.forEach((item) => {
      if (item.routes) {
        // 如果有子路由，递归处理
        traverse(item.routes);
      } else if (item.path && item.element) {
        // 如果是单个路由项，添加到结果数组
        result.push(item);
      }
    });
  };

  traverse(routes);
  return result;
};

function App() {
  const dispatch = useDispatch();
  const allRoutes = getAllRoutes(routes);
  
  useEffect(() => {
    // 初始化学生信息
    if (mockData.profileOverview?.studentInfo) {
      dispatch(setStudentInfo(mockData.profileOverview.studentInfo));
    }
  }, [dispatch]);
  
  return (
    <BrowserRouter>
      <Layout>
        <Routes>
          {allRoutes?.map((item) => (
            <Route {...item} key={item.path} />
          ))}
        </Routes>
      </Layout>
    </BrowserRouter>
  );
}

export default App;
