import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './styles/index.css'
import App from './App.tsx'
import { AuthProvider } from './auth/AuthProvider.tsx'

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <AuthProvider>
      <App />
    </AuthProvider>
  </StrictMode>,
)

// Register service worker for PWA support
if ('serviceWorker' in navigator) {
  window.addEventListener('load', async () => {
    try {
      const { registerSW } = await import('virtual:pwa-register')
      registerSW({
        onNeedRefresh() {
          if (confirm('New version available. Reload to update?')) {
            window.location.reload()
          }
        },
        onOfflineReady() {
          console.log('CalcPad is ready to work offline')
        },
      })
    } catch {
      // PWA registration not available in dev mode
    }
  })
}
