import {StrictMode} from 'react';
import {createRoot} from 'react-dom/client';
import App from './App.tsx';
import ErrorBoundary from './components/ErrorBoundary.tsx';
import { installPasswordRecoveryGuard } from './lib/auth/password-recovery-guard';
import { initClientSentry } from './lib/sentry-client';
import { probeExpressApiAvailable } from './lib/api-route-fallback';
import './lib/three/suppress-clock-deprecation';
import './index.css';

initClientSentry();
void probeExpressApiAvailable();
installPasswordRecoveryGuard();

// Suppress ResizeObserver loop limit exceeded error
const suppressResizeObserverError = (e: ErrorEvent | PromiseRejectionEvent) => {
  const message = e instanceof ErrorEvent ? e.message : (e.reason?.message || '');
  if (
    message.includes('ResizeObserver') || 
    message.includes('loop limit exceeded') ||
    message.includes('loop completed with undelivered notifications')
  ) {
    const overlay = document.getElementById('webpack-dev-server-client-overlay') || 
                    document.getElementById('vite-error-overlay');
    if (overlay) overlay.style.display = 'none';
    
    if (e instanceof ErrorEvent) {
      e.stopImmediatePropagation();
      e.preventDefault();
    }
  }
};

window.addEventListener('error', suppressResizeObserverError);
window.addEventListener('unhandledrejection', suppressResizeObserverError);

// Service worker disabled
// if ('serviceWorker' in navigator) {
//   window.addEventListener('load', () => {
//     navigator.serviceWorker.register('/sw.js').then(registration => {
//       console.log('SW registered: ', registration);
//     }).catch(registrationError => {
//       console.log('SW registration failed: ', registrationError);
//     });
//   });
// }

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <ErrorBoundary>
      <App />
    </ErrorBoundary>
  </StrictMode>,
);
