import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { App } from './app/App'
import { reloadOnStaleBuild } from './lib/reloadOnStaleBuild'
import './styles/index.css'

// A tab left open across a deploy reloads instead of failing to load the new build's files.
reloadOnStaleBuild()

async function enableMocks() {
  // Dev only; the dynamic import keeps MSW out of the production bundle.
  if (!import.meta.env.DEV || import.meta.env.VITE_USE_MOCKS !== 'true') return
  const { startMocks } = await import('./mocks/browser')
  await startMocks()
}

// If the mock worker cannot start, still render: sign-in then says the server is unreachable.
void enableMocks()
  .catch((error: unknown) => console.error('Mock API did not start', error))
  .then(() => {
    createRoot(document.getElementById('root')!).render(
      <StrictMode>
        <App />
      </StrictMode>,
    )
  })
