// App.js
import { BrowserRouter as Router, useRoutes } from 'react-router-dom';
import "./App.css"
import routes from "@/Routes";
import { useCallback, useEffect, useState } from "react";
import appApi from "@/api/app";
import store from "@/store";

function AppRoutes() {
    return useRoutes(routes);
}

function App() {
    const [init, setInit] = useState(false);

    const initState = useCallback((data: any) => {
        const { favicon, shortName } = data?.company?.config || {};
        if (favicon) {
            // 替换网页图标
            const favor: any = document.querySelector("link[rel*='icon']") || document.createElement('link');
            favor.type = 'image/x-icon';
            favor.rel = 'shortcut icon';
            favor.href = favicon;
        }
        if (shortName) {
            document.title = shortName
        }
    }, [])

    const getAppConfig = useCallback(async () => {
        try {
            let res = await appApi.getAppConfig();
            console.log('-------res', res)
            let data = res.data;
           
            store.setItem("appConfig", data);
            initState(data)
        } catch (error) {
            console.log(error)
        }
        setInit(true);
    }, [])

    useEffect(() => {
        if (init) {
            return;
        }
        getAppConfig();
    }, [])

    return (
        <>
            <div>
                <Router>
                    <AppRoutes />
                </Router>
            </div>
        </>
    );
}

export default App;