import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { createRootRoute, Outlet } from '@tanstack/react-router';
import { ThemeProvider } from '@/components/theme-provider';
import { Toaster } from '@/components/ui/sonner';
import '@/index.css';

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      retry: (failureCount, error: any) => {
        // eslint-disable-next-line ts/no-unsafe-assignment
        const status
          // eslint-disable-next-line ts/no-unsafe-member-access
          = error?.response?.status ?? error?.status;

        if (status >= 400 && status < 500) {
          return false;
        }

        return failureCount < 3;
      },
    },
  },
});

function RootLayout() {
  return (
    <>
      <ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
        <QueryClientProvider client={queryClient}>
          <Outlet />
        </QueryClientProvider>
      </ThemeProvider>
      <Toaster position="top-right" />
    </>
  );
}

export const Route = createRootRoute({ component: RootLayout });
