import { StrictMode, useEffect, useContext } from 'react';
import { createRoot } from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import './assets/fonts.css';
import './assets/style/styles.css';

// scroll bar
import 'simplebar/src/simplebar.css';

// third-party
import { Provider as ReduxProvider } from 'react-redux';

// apex-chart
import 'assets/third-party/apex-chart.css';

// project import
import App from './App';
import { store } from 'store';
import reportWebVitals from './reportWebVitals';
import { I18nProvider } from "components/I18nProvider";
import { AutoLogoutProvider } from "components/AutoLogoutProvider";
import { RefreshTokenProvider } from "components/RefreshTokenProvider";
import { SysSettingProvider, SysContext } from 'components/SysSettingProvider';

import { useLocation } from 'react-router-dom';

function GreyWrapper({ children }) {
  const location = useLocation();
  const { sysSetting } = useContext(SysContext);

  useEffect(() => {
    const isLoginPage = location.pathname === '/login';
    const enableGrey = sysSetting?.greyLogin === true;

    if (isLoginPage && enableGrey) {
      document.body.classList.add('page-grey');
    } else {
      document.body.classList.remove('page-grey');
    }

    return () => {
      document.body.classList.remove('page-grey');
    };
  }, [location.pathname, sysSetting?.greyLogin]);

  return children;
}

// ==============================|| MAIN - REACT DOM RENDER  ||============================== //

const container = document.getElementById('root');
const root = createRoot(container); // createRoot(container!) if you use TypeScript
//const NotAuthorized = lazy(() => import('../views/NotAuthorized'))
//const Error = lazy(() => import('../views/Error'))

root.render(
  <StrictMode>
    <ReduxProvider store={store}>
      <SysSettingProvider>
        <I18nProvider>
          <BrowserRouter basename="/">
            <RefreshTokenProvider>
              <AutoLogoutProvider>
                <GreyWrapper>
                  <App />
                </GreyWrapper>
              </AutoLogoutProvider>
            </RefreshTokenProvider>
          </BrowserRouter>
        </I18nProvider>
      </SysSettingProvider>
    </ReduxProvider>
  </StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
