import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import './lib/i18n';
import { App } from './app/App.tsx';

// Initialize MSW if enabled
async function enableMocking() {
  if (import.meta.env.VITE_USE_MSW === '1' && import.meta.env.DEV) {
    const { worker } = await import('./mocks/browser');

    // Start the worker
    return worker.start({
      serviceWorker: { url: '/mockServiceWorker.js' }, // IMPORTANT pour Vite
      onUnhandledRequest: 'bypass',
    });
  }
}

// Enable mocking then render the app
enableMocking().then(() => {
  createRoot(document.getElementById('root')!).render(
    <StrictMode>
      <App />
    </StrictMode>
  );
});
