import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { QueryClientProvider } from '@tanstack/react-query'
import { RouterProvider } from '@tanstack/react-router'
import './index.css'
import './shared/lib/i18n'
import { ThemeProvider } from './theme/ThemeProvider'
import { ToastProvider } from './shared/ui/ToastProvider'
import { Toaster } from './shared/ui/toaster'
import { router } from './router'
import { queryClient } from './shared/api/query-client'
import { setUnauthorizedHandler } from './shared/api/client'
import { clearSession } from './features/auth/api'

// Если refresh-токен недействителен (истёк/отозван) — очищаем стор авторизации, чтобы
// useRequireAuth/useRequireAdmin увидели user === null и сами увели на /login.
setUnauthorizedHandler(clearSession)

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <QueryClientProvider client={queryClient}>
      <ThemeProvider>
        <ToastProvider>
          <RouterProvider router={router} />
          <Toaster />
        </ToastProvider>
      </ThemeProvider>
    </QueryClientProvider>
  </StrictMode>,
)
