import React from "react";
import ReactDOM from "react-dom/client";
import { ChakraProvider, extendTheme } from "@chakra-ui/react";
import { Global } from "@emotion/react";
import App from "./App";
import "./global.css";
import { AuthContextProvider } from "./context/AuthContext";
import { auth } from "../firebase";
import { client } from "./api/client/client.gen";

declare global {
  interface Navigator {
    msSaveBlob?: (blob: Blob, defaultName?: string) => boolean;
  }
}

client.setConfig({
  baseURL: import.meta.env.VITE_BACKEND_URL,
});

client.instance.interceptors.request.use(async (config) => {
  const currentUser = auth.currentUser;
  const token = currentUser ? await currentUser.getIdToken(false) : null;
  // if (!token && !allowedOperations.includes(config.)) {
  // return;
  // }
  config.headers.set("Authorization", `Bearer ${token}`);
  return config;
});

const theme = extendTheme({
  fonts: {
    heading: `"Open Sans", "Arial", sans-serif`,
    body: `"Open Sans", "Arial", sans-serif`,
  },
  colors: {
    primary: {
      "100": "#e6ebff",
      "200": "#b3c0ff",
      "300": "#8096ff",
      "400": "#4d6cff",
      "500": "#5d78ff",
      "600": "#4058cc",
      "700": "#3043a3",
      "800": "#20307a",
      "900": "#10184d",
    },
  },
});

window.addEventListener("vite:preloadError", (_event) => {
  window.location.reload();
});

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
ReactDOM.createRoot(document.getElementById("root")!).render(
  <React.StrictMode>
    <AuthContextProvider>
      <ChakraProvider theme={theme}>
        <Global styles={`@font-face {font-family: "Open Sans", "Arial", sans-serif;}`} />
        <App />
      </ChakraProvider>
    </AuthContextProvider>
  </React.StrictMode>,
);
