import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import { App } from "./App";
import reportWebVitals from "./reportWebVitals";
import { ServerApi } from "./api/ServerApi";

// Roboto font
import "@fontsource/roboto/300.css";
import "@fontsource/roboto/400.css";
import "@fontsource/roboto/500.css";
import "@fontsource/roboto/700.css";
import { BrowserRouter } from "react-router-dom";
import { ConfirmDialogProvider } from "./context_providers/ConfirmDialogProvider";
import { AlertDialogProvider } from "./context_providers/AlertDialogProvider";
import { AsyncWidget } from "./widgets/AsyncWidget";
import { SnackbarProvider } from "./context_providers/SnackbarProvider";
import { DarkThemeProvider } from "./context_providers/DarkThemeProvider";

async function init() {
  try {
    const root = ReactDOM.createRoot(
      document.getElementById("root") as HTMLElement
    );

    root.render(
      <React.StrictMode>
        <BrowserRouter>
          <DarkThemeProvider>
            <AlertDialogProvider>
              <ConfirmDialogProvider>
                <SnackbarProvider>
                  <div style={{ height: "100vh" }}>
                    <AsyncWidget
                      loadKey={1}
                      load={async () => await ServerApi.LoadConfig()}
                      errMsg="Echec de la connexion au serveur pour la récupération de la configuration statique !"
                      build={() => <App />}
                    />
                  </div>
                </SnackbarProvider>
              </ConfirmDialogProvider>
            </AlertDialogProvider>
          </DarkThemeProvider>
        </BrowserRouter>
      </React.StrictMode>
    );
  } catch (e) {
    console.error(e);
    alert("Echec de l'initialisation de l'application !");
  }
}

init();

// 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();
