import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'
import ZeichnungsebenenApp from './ZeichnungsebenenApp.jsx'
import GeschossSettingsApp from './GeschossSettingsApp.jsx'
import ProjectSettingsApp from './ProjectSettingsApp.jsx'
import LibraryApp from './LibraryApp.jsx'
import EbenenSettingsApp from './EbenenSettingsApp.jsx'
import GeschossDialogApp from './GeschossDialogApp.jsx'
import LayerCombinationsApp from './LayerCombinationsApp.jsx'
import AusschnittSettingsApp from './AusschnittSettingsApp.jsx'
import LayoutDialogApp from './LayoutDialogApp.jsx'
import SwisstopoApp from './SwisstopoApp.jsx'
import OsmApp from './OsmApp.jsx'
import KameraApp from './KameraApp.jsx'
import MasseSettingsApp from './MasseSettingsApp.jsx'
import AboutApp from './AboutApp.jsx'
import TextEditorApp from './TextEditorApp.jsx'
import GestaltungApp from './GestaltungApp.jsx'
import AusschnitteApp from './AusschnitteApp.jsx'
import MassstabApp from './MassstabApp.jsx'
import WerkzeugeApp from './WerkzeugeApp.jsx'
import OberleisteApp from './OberleisteApp.jsx'
import OverridesApp from './OverridesApp.jsx'
import DimensionenApp from './DimensionenApp.jsx'
import LayoutsApp from './LayoutsApp.jsx'
import ElementeApp from './ElementeApp.jsx'
import ElementeUebersichtApp from './ElementeUebersichtApp.jsx'
import ElementePropertiesApp from './ElementePropertiesApp.jsx'

const mode = (typeof window !== 'undefined' && window.PANEL_MODE) || 'ebenen'
const RootApp = mode === 'gestaltung'           ? GestaltungApp
              : mode === 'ausschnitte'          ? AusschnitteApp
              : mode === 'massstab'             ? MassstabApp
              : mode === 'werkzeuge'            ? WerkzeugeApp
              : mode === 'oberleiste'           ? OberleisteApp
              : mode === 'overrides'            ? OverridesApp
              : mode === 'dimensionen'          ? DimensionenApp
              : mode === 'layouts'              ? LayoutsApp
              : mode === 'elemente'             ? ElementeApp
              : mode === 'zeichnungsebenen'     ? ZeichnungsebenenApp
              : mode === 'geschoss_settings'    ? GeschossSettingsApp
              : mode === 'project_settings'     ? ProjectSettingsApp
              : mode === 'library'              ? LibraryApp
              : mode === 'ebenen_settings'      ? EbenenSettingsApp
              : mode === 'geschoss_dialog'      ? GeschossDialogApp
              : mode === 'layer_combinations'   ? LayerCombinationsApp
              : mode === 'ausschnitt_settings'  ? AusschnittSettingsApp
              : mode === 'layout_dialog'        ? LayoutDialogApp
              : mode === 'swisstopo'            ? SwisstopoApp
              : mode === 'osm'                  ? OsmApp
              : mode === 'kamera'               ? KameraApp
              : mode === 'masse_settings'       ? MasseSettingsApp
              : mode === 'about'                ? AboutApp
              : mode === 'text_editor'          ? TextEditorApp
              : mode === 'elemente_uebersicht'  ? ElementeUebersichtApp
              : mode === 'elemente_properties'  ? ElementePropertiesApp
              : App

window.onerror = function (msg, src, line, col, err) {
  document.body.style.cssText = 'background:#1c1c1e;margin:0;padding:12px'
  document.body.innerHTML =
    '<pre style="color:#ff6b6b;font-size:10px;font-family:monospace;white-space:pre-wrap">' +
    'JS ERROR:\n' + msg + '\n' + src + ':' + line + '\n' +
    (err ? err.stack : '') + '</pre>'
  return false
}

window.onunhandledrejection = function (e) {
  document.body.style.cssText = 'background:#1c1c1e;margin:0;padding:12px'
  document.body.innerHTML =
    '<pre style="color:#ff6b6b;font-size:10px;font-family:monospace;white-space:pre-wrap">' +
    'PROMISE ERROR:\n' + (e.reason ? e.reason.stack || e.reason : e) + '</pre>'
}

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <RootApp />
  </StrictMode>,
)
