import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { App } from '@/app/App';
import { getEnv } from '@/config/env';
import { ConfigErrorPage } from '@/pages/ConfigErrorPage';

const rootElement = document.getElementById('root');
if (!rootElement) throw new Error('No se encontró el elemento #root');

function configError(): string | null {
  try {
    getEnv();
    return null;
  } catch (error) {
    return error instanceof Error ? error.message : String(error);
  }
}

const error = configError();

createRoot(rootElement).render(
  <StrictMode>{error ? <ConfigErrorPage detail={error} /> : <App />}</StrictMode>,
);
