import React from 'react';
import ReactDOM from 'react-dom/client';
import { MobileThemeProvider } from '@/mobile/theme';
import '@/mobile/animations.css';
import { AppRoot } from './AppRoot';
import { AppErrorBoundary } from './AppErrorBoundary';
import { OfflineIndicator } from './offline/OfflineIndicator';
import { DriverLoadsLauncher } from '@/portals/driver/loads/DriverLoadsLauncher';
import { DriverBuildTag } from './DriverBuildTag';
import { DriverPermissionsPrimer } from './DriverPermissionsPrimer';
import { registerPWA } from '@/mobile/pwa';
import { startOfflineSync } from '@/lib/offline';
import { applyDriverPwaMeta } from './driverPwaMeta';

// On the driver surface (/driver/app), swap the install manifest + apple-touch
// icon to the driver app's BEFORE the user can "Add to Home Screen", so the
// installed PWA opens at /driver/app with the steering-wheel icon (not the
// generic Vaha shell). iOS reads these from the live DOM at install time.
applyDriverPwaMeta();

// Phase 0: register the service worker for the unified app.getvaha.com entrance
// (precaches the app shell → installable + works offline). No-ops in dev and in
// the native build, where the PWA plugin is disabled. Phase 2: drain the offline
// write outbox on load and on every reconnect (works on web + native).
registerPWA();
startOfflineSync();

ReactDOM.createRoot(document.getElementById('root')!).render(
  <MobileThemeProvider>
    <AppErrorBoundary>
      <AppRoot />
    </AppErrorBoundary>
    <OfflineIndicator />
    <DriverLoadsLauncher />
    <DriverPermissionsPrimer />
    <DriverBuildTag />
  </MobileThemeProvider>,
);
