import { createRoot } from "react-dom/client";
import "./i18n";
import App from "./App";
import "./index.css";

// #350 (browser print): eagerly trigger DIN Next LT Arabic downloads
// via the CSS Font Loading API. The <link rel="preload"> in
// index.html only warms the HTTP cache; it does not register the font
// with the browser's font system. Calling `document.fonts.load()`
// here resolves the @font-face declaration immediately so the font is
// fully ready before the user opens the print dialog (Ctrl+P) — even
// if no on-screen element happens to render in DIN Next yet. Without
// this, Chrome's print snapshot can fall back to a system font that
// lacks Arabic GSUB shaping, producing isolated/reversed letters.
if (typeof document !== "undefined" && document.fonts?.load) {
  void document.fonts.load('400 1em "DIN Next LT Arabic"');
  void document.fonts.load('700 1em "DIN Next LT Arabic"');
}

// Register the Web Push service worker scoped to the SPA's base path.
// The SW only handles `push` + `notificationclick` — no asset caching —
// so it's safe to register unconditionally as soon as the page boots.
// Without registration, iOS PWAs cannot receive lock-screen notifications.
if (
  typeof window !== "undefined" &&
  "serviceWorker" in navigator &&
  "PushManager" in window &&
  window.isSecureContext
) {
  const base = import.meta.env.BASE_URL.endsWith("/")
    ? import.meta.env.BASE_URL
    : `${import.meta.env.BASE_URL}/`;
  window.addEventListener("load", () => {
    navigator.serviceWorker
      .register(`${base}sw.js`, { scope: base })
      .catch(() => {
        /* SW registration is best-effort — push will just stay disabled */
      });
  });
}

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