import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { App } from './app'
import { MOCK_PROPOSED, MOCKS_ENABLED } from '@/lib/env'
import './index.css'

/**
 * Mock Service Worker modes. Sign-in (/identity) always goes to the real backend.
 *  - VITE_ENABLE_MOCKS unset/true: /membership, /profile and /pm come from seeded demo data,
 *    and "Switch role" grants the signed-in user a persona's role in it.
 *  - VITE_ENABLE_MOCKS=false, VITE_MOCK_PROPOSED unset/true: /membership and /profile go to
 *    the real backend; the proposed /pm routes stay mocked.
 *  - both false: everything goes to the backend.
 */
async function enableMocks() {
  if (!MOCKS_ENABLED && !MOCK_PROPOSED) return
  const { startWorker } = await import('@/mocks/browser')
  await startWorker(MOCKS_ENABLED ? 'all' : 'proposed')
}

void enableMocks().then(() => {
  createRoot(document.getElementById('root')!).render(
    <StrictMode>
      <App />
    </StrictMode>,
  )
})
