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

// Global handler to safely catch and suppress non-fatal browser/IndexedDB tab lifecycle errors
if (typeof window !== 'undefined') {
  const isClosingOrDbError = (str: string) => {
    const s = (str || '').toLowerCase();
    return (
      s.includes('database is closing') ||
      s.includes('database connection is closing') ||
      s.includes('closing/hidden') ||
      s.includes('indexeddb') ||
      s.includes('idbdatabase') ||
      s.includes('client is offline') ||
      s.includes('the operation failed because the database is closing')
    );
  };

  window.addEventListener('unhandledrejection', (event) => {
    const reasonStr = event.reason?.message || event.reason?.name || String(event.reason || '');
    if (isClosingOrDbError(reasonStr)) {
      event.preventDefault();
      event.stopImmediatePropagation?.();
      console.warn('[Handled Lifecycle Rejection]', reasonStr);
    }
  });

  window.addEventListener('error', (event) => {
    const msg = event.message || event.error?.message || String(event.error || '');
    if (isClosingOrDbError(msg)) {
      event.preventDefault();
      event.stopImmediatePropagation?.();
      console.warn('[Handled Lifecycle Error]', msg);
    }
  });
}

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

