import { createRoot } from "react-dom/client";
import { Provider } from "react-redux";
import { PersistGate } from "redux-persist/integration/react";
import App from "./app/App.tsx";
import { AuthBootstrap } from "./app/components/auth/AuthBootstrap";
import { HeadOfficeProvider } from "./app/context/HeadOfficeContext";
import { PlatformProvider } from "./app/context/PlatformContext";
import { ThemeProvider } from "./app/context/ThemeContext";
import { store, persistor } from "./redux/store";
import { registerSessionExpiredHandler } from "./api/authSession";
import { setUser } from "./redux/auth/authSlice";
import "./styles/index.css";

registerSessionExpiredHandler(() => {
  store.dispatch(setUser(null));
});

createRoot(document.getElementById("root")!).render(
  <Provider store={store}>
    <PersistGate loading={null} persistor={persistor}>
      <ThemeProvider>
        <AuthBootstrap>
          <HeadOfficeProvider>
            <PlatformProvider>
              <App />
            </PlatformProvider>
          </HeadOfficeProvider>
        </AuthBootstrap>
      </ThemeProvider>
    </PersistGate>
  </Provider>,
);
