import React from 'react';
import ReactDOM from 'react-dom/client';
// Forward console.error/warn/log to backend in dev mode so we can tail webview.log
import './lib/dev-logger';
// Tauri bridge polyfill: must be imported before App so window.electronAPI is available to all components
import './lib/tauri-bridge';
import App from './App';
import ErrorBoundary from './components/ErrorBoundary';
import './index.css';

window.addEventListener('error', (e) => {
  if (e.error) {
    try {
      console.error('[GlobalError]', e.error.message, e.error.stack);
      window.electronAPI?.logError?.(e.error.message, e.error.stack || '', '');
    } catch {}
  }
});

window.addEventListener('unhandledrejection', (e) => {
  const reason = e.reason instanceof Error ? e.reason : new Error(String(e.reason));
  try {
    console.error('[UnhandledRejection]', reason.message, reason.stack);
    window.electronAPI?.logError?.(reason.message, reason.stack || '', '');
  } catch {}
});

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