"use client";

import { useEffect } from "react";

export default function PWARegister() {
  useEffect(() => {
    if ("serviceWorker" in navigator) {
      navigator.serviceWorker
        .register("/mobile/sw.js", { scope: "/mobile" })
        .then((reg) => {
          console.log("[PWA] SW registered:", reg.scope);
        })
        .catch((err) => console.warn("[PWA] SW registration failed:", err));

      // Listen for beforeinstallprompt (Android Chrome)
      window.addEventListener("beforeinstallprompt", (e) => {
        e.preventDefault();
        (window as Record<string, unknown>).__deferredPrompt = e;
      });

      window.addEventListener("appinstalled", () => {
        console.log("[PWA] App installed");
        (window as Record<string, unknown>).__deferredPrompt = null;
      });
    }
  }, []);

  return null;
}
