{"version":3,"file":"pwa.mjs","names":["ROOT","document","documentElement","GLOBE","getElementById","kept","hasAttribute","offline","navigator","onLine","standalone","window","matchMedia","matches","idle","fn","go","requestIdleCallback","timeout","setTimeout","readyState","addEventListener","once","storeP","store","then","m","openStore","ui","fcstFor","Map","keep","b","f","get","icao","s","save","fcst","catch","e","detail","briefing","d","set","saveFcst","__azWxLast","registered","register","serviceWorker","scope","updateViaCache","console","warn","message","connection","saveData","preventDefault","storage","persist","sw","PAGE_BUILD","querySelector","content","RELOADED","checkedAt","Date","now","touched","later","workerBuild","Promise","done","c","controller","MessageChannel","ch","t","port1","onmessage","clearTimeout","data","build","postMessage","type","port2","decide","async","updateAction","all","lastReloadAt","Number","sessionStorage","getItem","act","pageBuild","hidden","setItem","String","location","reload","capture","passive","getRegistration","r","update","reachable","ctl","AbortController","timer","abort","fetch","cache","signal","headers","accept","shown","hub","probing","wentOffline","setMeta","clearInterval","setInterval","cameBack","AflyZonGlobe","mountBar","nav","mountGlobe","removeAttribute","online","unmountBar","dispatchEvent","CustomEvent","refresh","ev","defaultPrevented","button","metaKey","ctrlKey","shiftKey","altKey","a","target","closest","u","URL","href","signIn","test","pathname","lang","searchParams","has","outside","origin","stopImmediatePropagation","explain","running","sync","lastAuto","offlineAt","meta","autoDue","Math","random","refreshSaved","fetchBriefing","briefingFetcher","fetchFcst","fcstFetcher","auto","saved","length","finally","startFlightMode"],"sources":["pwa.mjs"],"sourcesContent":["/* ============================================================\n   AflyZon Weather — offline support on Weather's pages (PWA)\n\n   Added by worker.js to Weather's pages of the public site only (never while PRIVATE_MODE is on). For a visitor\n   online it does three quiet things and changes nothing on the page:\n   - keeps each airport briefing the page receives from the network (app.mjs announces it: az:wx-briefing) on this\n     device, for offline use (lib/offline-store.mjs: IndexedDB, with when it arrived; never the NOTAMs);\n   - registers the service worker (/sw.js, scope /weather) once the page has loaded and the browser is idle — not on a\n     data-saving connection, unless Weather runs as an installed app or the visitor asks for it (\"Save for flight\");\n     if that fails, nothing else does: the pages work as they always did;\n   - keeps the browser's own install prompt from appearing by itself over Weather (Home offers the install).\n   Offline — the browser says so, or the page is the copy the service worker kept (<html data-az-offline>) — it shows\n   the offline bar (ui/offline.mjs), or on the globe's page, when the globe could not load, what is saved instead; and\n   a link that cannot work offline (the rest of the site, signing in, another language) says so instead of opening the\n   browser's error page, which an installed app on a phone cannot leave. When the connection comes back (\"online\",\n   confirmed by GET /api/health; for a kept copy, the same check every 10 s while the page is visible) it says so,\n   tells the page (az:wx-online) and refreshes the saved airports in one small batch (lib/offline-sync.mjs: only after\n   the connection was lost, at most once every 5 minutes, stopping at the first sign of trouble). An installed app also\n   asks the browser to keep its data (navigator.storage.persist), and follows each deploy: brought back to the screen it\n   asks for a new worker, and a page older than its worker is loaded again when nobody is using it (below).\n   ============================================================ */\nconst ROOT = document.documentElement;\nconst GLOBE = document.getElementById(\"wxh\");\nconst PROBE_MS = 10e3;\nconst kept = () => ROOT.hasAttribute(\"data-az-offline\");\nconst offline = () => navigator.onLine === false || kept();\nconst standalone = () => (window.matchMedia && window.matchMedia(\"(display-mode: standalone)\").matches) || navigator.standalone === true;\nconst idle = (fn) => {\n  const go = () => (window.requestIdleCallback ? window.requestIdleCallback(fn, { timeout: 10e3 }) : setTimeout(fn, 3e3));\n  if (document.readyState === \"complete\") go(); else window.addEventListener(\"load\", go, { once: true });\n};\n\nlet storeP = null;\nconst store = () => storeP || (storeP = import(\"./lib/offline-store.mjs\").then((m) => m.openStore()));\nconst ui = () => import(\"./ui/offline.mjs\");\n\n/* the airports the page receives: kept for offline use (one that arrived before this script ran: app.mjs's __azWxLast) */\nconst fcstFor = new Map(); // a field without a station: its FCST reports for the next day (app.mjs az:wx-fcst), kept with it\nconst keep = (b) => {\n  if (!b) return;\n  const f = fcstFor.get(b.icao);\n  store().then((s) => s.save(f && !b.fcst ? { ...b, fcst: f } : b)).catch(() => {});\n};\nwindow.addEventListener(\"az:wx-briefing\", (e) => keep(e.detail && e.detail.briefing));\nwindow.addEventListener(\"az:wx-fcst\", (e) => {\n  const d = e.detail || {};\n  if (!d.icao || !d.fcst) return;\n  fcstFor.set(d.icao, d.fcst);\n  store().then((s) => s.saveFcst(d.icao, d.fcst)).catch(() => {});\n});\nkeep(window.__azWxLast);\n\n/* the service worker */\nlet registered = false;\nfunction register() {\n  if (registered || !(\"serviceWorker\" in navigator)) return;\n  registered = true;\n  navigator.serviceWorker.register(\"/sw.js\", { scope: \"/weather\", updateViaCache: \"none\" }).catch((e) => { registered = false; console.warn(\"[AflyZon Weather] offline support unavailable:\", e && e.message); });\n}\nwindow.addEventListener(\"az:wx-offline-register\", register);\nif (!(navigator.connection && navigator.connection.saveData) || standalone()) idle(register);\nwindow.addEventListener(\"beforeinstallprompt\", (e) => e.preventDefault());\nif (standalone() && navigator.storage && navigator.storage.persist) navigator.storage.persist().catch(() => {});\n\n/* an installed app follows each deploy (installed-app report, 2026-11; lib/app-update.mjs decides): brought back to the\n   screen, at most every 5 minutes, it asks for a new worker (a browser looks for one only on a navigation, and an app\n   brought back makes none); when a newer worker takes over a page served from an older deploy (<meta name=\"az-build\">\n   against the worker's BUILD), the page is loaded again — at once if it is hidden or nobody has touched it since it came\n   back, else the next time it is left. A browser tab: nothing of this, as before. */\nif (standalone() && \"serviceWorker\" in navigator) {\n  const sw = navigator.serviceWorker;\n  const PAGE_BUILD = (document.querySelector('meta[name=\"az-build\"]') || {}).content || \"\";\n  const RELOADED = \"az-wx-reloaded\";\n  let checkedAt = Date.now(), touched = false, later = false;\n  const workerBuild = () => new Promise((done) => {\n    const c = sw.controller;\n    if (!c || typeof MessageChannel !== \"function\") { done(\"\"); return; }\n    const ch = new MessageChannel(), t = setTimeout(() => done(\"\"), 3e3);\n    ch.port1.onmessage = (e) => { clearTimeout(t); done((e.data && e.data.build) || \"\"); };\n    try { c.postMessage({ type: \"az:build\" }, [ch.port2]); } catch (e) { clearTimeout(t); done(\"\"); }\n  });\n  const decide = async () => {\n    try {\n      const [{ updateAction }, build] = await Promise.all([import(\"./lib/app-update.mjs\"), workerBuild()]);\n      let lastReloadAt;\n      try { lastReloadAt = Number(sessionStorage.getItem(RELOADED)) || 0; } catch (e) { return; } // no storage: never risk a loop\n      const act = updateAction({ pageBuild: PAGE_BUILD, workerBuild: build, hidden: document.hidden, touched, offline: offline(), lastReloadAt, now: Date.now() });\n      if (act === \"later\") later = true;\n      if (act !== \"now\") return;\n      sessionStorage.setItem(RELOADED, String(Date.now()));\n      location.reload();\n    } catch (e) { /* the page as it is */ }\n  };\n  sw.addEventListener(\"controllerchange\", decide);\n  for (const t of [\"pointerdown\", \"keydown\", \"wheel\"]) window.addEventListener(t, () => { touched = true; }, { capture: true, passive: true });\n  document.addEventListener(\"visibilitychange\", () => {\n    if (document.hidden) { if (later) { later = false; decide(); } return; }\n    touched = false;\n    if (Date.now() - checkedAt < 5 * 60e3) return;\n    checkedAt = Date.now();\n    sw.getRegistration(\"/weather\").then((r) => r && r.update()).catch(() => {});\n  });\n}\n\n/* connectivity */\nasync function reachable() {\n  const ctl = new AbortController();\n  const timer = setTimeout(() => ctl.abort(), 5e3);\n  try { await fetch(\"/api/health\", { cache: \"no-store\", signal: ctl.signal, headers: { accept: \"application/json\" } }); return true; } catch (e) { return false; } finally { clearTimeout(timer); }\n}\nlet shown = false, hub = null, probing = 0;\nasync function wentOffline() {\n  store().then((s) => s.setMeta(\"offlineAt\", Date.now())).catch(() => {});\n  watch();\n  if (shown) return;\n  shown = true;\n  try {\n    const m = await ui();\n    // the globe's page answered from the kept copy has no globe (its scripts and pictures are online only)\n    if (GLOBE && (kept() || !window.AflyZonGlobe)) hub = await m.mountGlobe(GLOBE);\n    else await m.mountBar({ nav: !!document.getElementById(\"wx\") });\n  } catch (e) { /* the page as it is */ }\n}\nasync function cameBack() {\n  clearInterval(probing);\n  if (kept()) ROOT.removeAttribute(\"data-az-offline\");\n  if (shown) {\n    shown = false;\n    try { const m = await ui(); if (hub) hub.online(); else m.unmountBar(); } catch (e) { /* gone */ }\n  }\n  window.dispatchEvent(new CustomEvent(\"az:wx-online\"));\n  refresh();\n}\n/** A kept copy whose network comes back without an \"online\" event (the browser never said it was gone). */\nfunction watch() {\n  clearInterval(probing);\n  if (!kept()) return;\n  probing = setInterval(async () => {\n    if (document.hidden || navigator.onLine === false) return;\n    if (await reachable()) cameBack();\n  }, PROBE_MS);\n}\nwindow.addEventListener(\"offline\", () => { wentOffline(); });\nwindow.addEventListener(\"online\", async () => { if (await reachable()) cameBack(); else wentOffline(); });\n\n/* links that cannot work offline: a short note instead of the browser's error page */\ndocument.addEventListener(\"click\", (ev) => {\n  if (!offline() || ev.defaultPrevented || ev.button !== 0 || ev.metaKey || ev.ctrlKey || ev.shiftKey || ev.altKey) return;\n  const a = ev.target.closest && ev.target.closest(\"a[href]\");\n  if (!a || (a.target && a.target !== \"_self\")) return;\n  let u;\n  try { u = new URL(a.href, location.href); } catch (e) { return; }\n  const signIn = a.hasAttribute(\"data-az-auth-open\") || /^\\/(account|auth)(\\/|$)/.test(u.pathname);\n  const lang = u.searchParams.has(\"lang\");\n  const outside = u.origin !== location.origin || !/^\\/weather(\\/|$)/.test(u.pathname);\n  if (!signIn && !lang && !outside) return;\n  ev.preventDefault();\n  ev.stopImmediatePropagation();\n  ui().then((m) => m.explain(signIn ? \"signin\" : lang ? \"lang\" : \"link\")).catch(() => {});\n}, true);\n\n/* the saved airports, refreshed when the connection comes back (one batch at a time) */\nlet running = null;\nfunction refresh() {\n  if (running) return running;\n  running = (async () => {\n    const [s, sync] = await Promise.all([store(), import(\"./lib/offline-sync.mjs\")]);\n    const [lastAuto, offlineAt] = await Promise.all([s.meta(\"lastAuto\"), s.meta(\"offlineAt\")]);\n    if (!sync.autoDue({ lastAuto, offlineAt, now: Date.now() })) return null;\n    await new Promise((r) => setTimeout(r, Math.random() * 1500)); // a server that comes back is not asked by every device at once\n    if (offline()) return null;\n    const r = await sync.refreshSaved({ store: s, fetchBriefing: sync.briefingFetcher(), fetchFcst: sync.fcstFetcher(), auto: true });\n    if (r.saved.length) window.dispatchEvent(new CustomEvent(\"az:wx-saved\", { detail: r }));\n    return r;\n  })().catch(() => null).finally(() => { running = null; });\n  return running;\n}\n\nif (offline()) wentOffline();\nelse idle(refresh); // the app opened again after being offline: the saved airports, refreshed once the page has loaded\n\n/* Flight Mode on every Weather page, the globe's too (P1 offline hardening): the saved flights refreshed the moment the\n   app is opened or brought back and the server answers (lib/flight-store.mjs createFlightSync: it asks /api/health, so\n   a network without internet is not taken for a connection). Nothing without a saved flight. */\nidle(() => { import(\"./ui/flight-mode.mjs\").then((m) => m.startFlightMode()).catch(() => {}); });\n"],"mappings":"AAqBA,MAAMA,EAAOC,SAASC,gBAChBC,EAAQF,SAASG,eAAe,OAEhCC,EAAO,IAAML,EAAKM,aAAa,mBAC/BC,EAAU,KAA2B,IAArBC,UAAUC,QAAoBJ,IAC9CK,EAAa,IAAOC,OAAOC,YAAcD,OAAOC,WAAW,8BAA8BC,UAAqC,IAAzBL,UAAUE,WAC/GI,EAAQC,IACZ,MAAMC,EAAK,IAAOL,OAAOM,oBAAsBN,OAAOM,oBAAoBF,EAAI,CAAEG,QAAS,MAAUC,WAAWJ,EAAI,KACtF,aAAxBd,SAASmB,WAA2BJ,IAAWL,OAAOU,iBAAiB,OAAQL,EAAI,CAAEM,MAAM,KAGjG,IAAIC,EAAS,KACb,MAAMC,EAAQ,IAAMD,IAAWA,SAAgB,2BAA2BE,KAAMC,GAAMA,EAAEC,cAClFC,EAAK,WAAa,oBAGlBC,EAAU,IAAIC,IACdC,EAAQC,IACZ,IAAKA,EAAG,OACR,MAAMC,EAAIJ,EAAQK,IAAIF,EAAEG,MACxBX,IAAQC,KAAMW,GAAMA,EAAEC,KAAKJ,IAAMD,EAAEM,KAAO,IAAKN,EAAGM,KAAML,GAAMD,IAAIO,MAAM,SAE1E5B,OAAOU,iBAAiB,iBAAmBmB,GAAMT,EAAKS,EAAEC,QAAUD,EAAEC,OAAOC,WAC3E/B,OAAOU,iBAAiB,aAAemB,IACrC,MAAMG,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