import { createRoot } from "react-dom/client";
import App from "./App.tsx";
import "./index.css";
import "./i18n";
import { registerServiceWorker, isPushSupported } from "./lib/pushNotifications";
import { installAnalyticsAdapter } from "./lib/analyticsAdapter";
import { AppErrorBoundary } from "./components/AppErrorBoundary";

// Wire the analytics adapter (subscribes to app:analytics CustomEvents and
// installs window.onerror + unhandledrejection listeners) before render so
// we capture any errors that occur during the very first render pass.
installAnalyticsAdapter();

createRoot(document.getElementById("root")!).render(
  <AppErrorBoundary>
    <App />
  </AppErrorBoundary>,
);

// Register the push service worker once the page is idle so it's ready
// when the user opts in. Failures are non-fatal.
if (isPushSupported()) {
  if (document.readyState === "complete") {
    void registerServiceWorker();
  } else {
    window.addEventListener("load", () => { void registerServiceWorker(); }, { once: true });
  }
}
