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

// PWA manifest cache busting: ensures returning visitors fetch the latest manifest
const MANIFEST_VERSION = "20260605";
console.log("[PWA] Manifest version:", MANIFEST_VERSION);
try {
  const storedVersion = localStorage.getItem("pwa_manifest_version");
  console.log("[PWA] Stored version:", storedVersion);
  if (storedVersion !== MANIFEST_VERSION) {
    console.log("[PWA] Version changed, applying cache busting");
    const link = document.querySelector<HTMLLinkElement>('link[rel="manifest"]');
    if (link) {
      const fresh = `/manifest.webmanifest?v=${MANIFEST_VERSION}&t=${Date.now()}`;
      console.log("[PWA] New manifest href:", fresh);
      link.href = fresh;
    }
    localStorage.setItem("pwa_manifest_version", MANIFEST_VERSION);
    console.log("[PWA] Updated stored version to:", MANIFEST_VERSION);
  } else {
    console.log("[PWA] No version change, using cached manifest");
  }
} catch (e) {
  console.warn("[PWA] Storage error (private mode, etc.):", e);
}

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

