import {createRoot} from 'react-dom/client';
import App from './App';
import ErrorBoundary from './components/ErrorBoundary';
import './index.css';

// Global error handler for debugging blank screen
window.onerror = (message, source, lineno, colno, error) => {
  console.error('Global Error:', { message, source, lineno, colno, error });
};

window.onunhandledrejection = (event) => {
  if (event.reason instanceof Error && event.reason.message.includes('WebSocket')) {
    console.warn('Ignored WebSocket error:', event.reason);
    return;
  }
  // Ignore Service Worker registration errors (often happens in dev/iframe/incognito)
  if (event.reason && event.reason.message && event.reason.message.includes('ServiceWorker')) {
    console.warn('Ignored ServiceWorker error:', event.reason);
    return;
  }
  if (event.reason && String(event.reason).includes('registerSW.js')) {
    console.warn('Ignored registerSW error:', event.reason);
    return;
  }
  console.error('Unhandled Rejection:', event.reason);
};

console.log('Main.tsx is running...');
const container = document.getElementById('root');
if (container) {
  console.log('Root container found, rendering...');
  const root = createRoot(container);
  root.render(
    <ErrorBoundary>
      <App />
    </ErrorBoundary>
  );
} else {
  console.error('Root container not found');
}
