// project import
import LoginRoutes from './LoginRoutes'
import MainRoutes from './MainRoutes'
import {useLocation, useRoutes} from 'react-router-dom'
import {isUserLoggedIn} from "utils/Utils";
import {Navigate} from "react-router";
import SettingRoutes from "./SettingRoutes";
import AwardRoutes from "./AwardRoutes";
import ClientRoutes from "./ClientRoutes";
import {useContext, useEffect} from "react";
import LocaleContext from "../components/I18nProvider";
import AppreciationRoutes from "./AppreciationRoute";
// ==============================|| ROUTING RENDER ||============================== //

export default function ThemeRoutes() {
    const location = useLocation();
    const { setIsAdvanceDisplay } = useContext(LocaleContext);

    useEffect(() => {
        if(location && !isUserLoggedIn()){
            if(location.pathname !== "/" && location.pathname !== "/login"){
                localStorage.setItem('lastVisitedPath', location.pathname);
            }
        }

        if(
            !location.pathname.includes("/client")
        ){
            setIsAdvanceDisplay(true);
        }
        else{
            setIsAdvanceDisplay(false);
        }
    }, [location]);

    return useRoutes([
        {
            path: '',
            element: isUserLoggedIn() ? <Navigate to="/client" />
                : <Navigate to="/login"/>
        },
        LoginRoutes,
        MainRoutes(),
        AwardRoutes(),
        ClientRoutes(),
        AppreciationRoutes(),
        SettingRoutes()
    ]);
}
