import React from 'react';
import ReactDOM from 'react-dom/client';
import './styles/index.css';
import { App } from './App';
import { I18nProvider } from './lib/I18nContext';
import { ThemeProvider } from './lib/ThemeContext';
import { ThemeEffects } from './components/ThemeEffects';
import { setApiBase } from './lib/api';

// Detect Tauri desktop mode and configure API base URL
async function initApiBase() {
  if ('__TAURI__' in window) {
    try {
      // Dynamic import hidden from TypeScript to avoid build-time dependency
      const mod = await new Function("return import('@tauri-apps/api/core')")();
      const url: string = await mod.invoke('get_backend_url');
      setApiBase(url);
    } catch {
      // Fallback: relative URLs (web mode)
    }
  }
}

initApiBase().then(() => {
  const rootEl = document.getElementById('root');
  if (!rootEl) throw new Error('Missing #root element in index.html');
  ReactDOM.createRoot(rootEl).render(
    <React.StrictMode>
      <ThemeProvider>
        <I18nProvider>
          <ThemeEffects />
          <App />
        </I18nProvider>
      </ThemeProvider>
    </React.StrictMode>,
  );
});
