let deferredInstallPrompt = null; function isStandalone() { return window.matchMedia("(display-mode: standalone)").matches || window.navigator.standalone === true; } export function wirePwaInstall({ sectionId = "pwaInstallSection", buttonId = "installPwa" } = {}) { const section = document.getElementById(sectionId); const button = document.getElementById(buttonId); if (!section || !button) return; const hide = () => { section.hidden = true; }; if (isStandalone()) { hide(); return; } hide(); window.addEventListener("beforeinstallprompt", (e) => { e.preventDefault(); deferredInstallPrompt = e; section.hidden = false; }); window.addEventListener("appinstalled", () => { deferredInstallPrompt = null; hide(); }); button.addEventListener("click", async () => { if (!deferredInstallPrompt) return; deferredInstallPrompt.prompt(); await deferredInstallPrompt.userChoice; deferredInstallPrompt = null; hide(); }); } export async function registerPwa() { if (!("serviceWorker" in navigator)) return; try { await navigator.serviceWorker.register("/sw.js"); if ("sync" in ServiceWorkerRegistration.prototype) { navigator.serviceWorker.ready.then((reg) => { reg.sync?.register("sync-logs").catch(() => {}); }); } } catch (e) { console.error("SW registration failed:", e); } }