export default defineNuxtPlugin(async () => {
  const { setInstallPromptEvent, isNativeApp } = usePush();

  if (isNativeApp()) {
    return;
  }

  if ("serviceWorker" in navigator) {
    await navigator.serviceWorker.register("/sw.js");
  }

  window.addEventListener("beforeinstallprompt", (event) => {
    event.preventDefault();
    setInstallPromptEvent(event as Event & {
      prompt: () => Promise<void>;
      userChoice: Promise<{ outcome: "accepted" | "dismissed"; platform: string }>;
    });
  });

  window.addEventListener("appinstalled", () => {
    setInstallPromptEvent(null);
  });
});
