// project import
import Routes from 'routes';
import ThemeCustomization from 'themes';
import ScrollTop from 'components/ScrollTop';
import {ToastContainer} from "react-toastify";
import React from "react";
import 'react-toastify/dist/ReactToastify.css';
import {useEffect} from "react";
import {useDispatch} from "react-redux";
import {
    SetupAxiosInterceptors
} from "./auth";
import { useNavigate } from 'react-router-dom';
//import {isUserLoggedIn} from 'utils/Utils';
//import {DefaultRoute} from 'routes/index'
// ==============================|| APP - THEME, ROUTER, LOCAL  ||============================== //

const App = () => {
    const dispatch = useDispatch();
    const navigate = useNavigate();

    // useEffect(() => {
    //     const handleBeforeUnload = () => {
    //         let url = new URL(window.location.href);
    //         let pathname = url.pathname;
    //         const lastPath = localStorage.getItem('lastVisitedPath');
    //         if (
    //             pathname !== '/login' &&
    //             pathname !== '/lionerDashboard' &&
    //             pathname !== lastPath &&
    //             !isRefresh.current && isUserClick.current) {
    //             //for all page auto logout
    //             dispatch(handleLogoutFunction());
    //             window.location.reload();
    //         }
    //         else if (!isRefresh.current && !isUserClick.current) {
    //             //for dashboard and login page auto logout
    //             dispatch(handleLogoutFunction());
    //             window.location.reload();
    //         }
    //     };

    //     const handleRefresh = () => {
    //         let url = new URL(window.location.href);
    //         let pathname = url.pathname;
    //         if(pathname !== '/lionerDashboard'){
    //             //skip checking for unload dashboard
    //             isUserClick.current = true;
    //         }
    //         if (event.key === 'F5' ||
    //             (event.ctrlKey && event.key === 'r') ||
    //             (event.ctrlKey && event.shiftKey && event.key === 'R')
    //         ) {
    //             // User pressed F5 or Ctrl+R, do not trigger logout
    //             isRefresh.current = true;
    //         }
    //     };


    //     window.addEventListener('beforeunload', handleBeforeUnload);
    //     window.addEventListener('keydown', handleRefresh);

    //     return () => {
    //         window.removeEventListener('beforeunload', handleBeforeUnload);
    //         window.removeEventListener('keydown', handleRefresh);
    //     };
    // }, [dispatch]);

    useEffect(() => {
        let url = new URL(window.location.href);
        let pathname = url.pathname;
        if (pathname !== '/login'){
            SetupAxiosInterceptors(dispatch, navigate);
        }
    }, [])

    return (
        <ThemeCustomization>
            <ScrollTop>
                <Routes>
                </Routes>
            </ScrollTop>
            <ToastContainer/>
        </ThemeCustomization>
    );
};

export default App;
