import { Toaster, TooltipProvider } from "@repo/shadcn-ui/components";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { I18nextProvider } from "react-i18next";

import { createAxiosDataProvider } from "@/lib/axios/create-axios-data-provider";
import { UnsavedWarnProvider } from "@/lib/hooks";
import { i18n } from "@/locales";

import { DataSourceProvider } from "@erp/core/hooks";
import { AppRoutes } from "./app-routes";
import "./app.css";

export const App = () => {
  const queryClient = new QueryClient({
    defaultOptions: {
      queries: {
        retry: false,
        staleTime: 10000, // Specify a staleTime to only fetch when the data is older than a certain amount of time
      },
    },
  });

  return (
    <I18nextProvider i18n={i18n}>
      <QueryClientProvider client={queryClient}>
        <DataSourceProvider dataSource={createAxiosDataProvider(import.meta.env.VITE_API_URL)}>
          <TooltipProvider delayDuration={0}>
            <UnsavedWarnProvider>
              <AppRoutes />
            </UnsavedWarnProvider>
          </TooltipProvider>
          <Toaster />

          {import.meta.env.MODE === "development" && <ReactQueryDevtools initialIsOpen={false} />}
        </DataSourceProvider>
      </QueryClientProvider>
    </I18nextProvider>
  );
};
