const CACHE_NAME = "suijun-voice-book-v198";
const APP_ASSETS = [
  "./",
  "./index.html",
  "./css/style.css?v=198",
  "./js/app.js?v=198",
  "./js/calculation.js?v=190",
  "./js/voice.js?v=190",
  "./js/storage.js?v=190",
  "./js/export.js?v=198",
  "./js/rules.js?v=190",
  "./js/point-names.js?v=190",
  "./js/analytics.js?v=190",
  "./manifest.json?v=197",
  "./assets/share-qr.png",
  "./icons/icon-192.png?v=197",
  "./icons/icon-512.png?v=197",
  "./icons/icon-master.png"
];

self.addEventListener("install", (event) => {
  event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_ASSETS)));
  self.skipWaiting();
});

self.addEventListener("activate", (event) => {
  event.waitUntil(
    caches.keys()
      .then((keys) => Promise.all(keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key))))
      .then(() => self.clients.claim())
  );
});

self.addEventListener("fetch", (event) => {
  if (event.request.method !== "GET") return;
  event.respondWith(
    fetch(event.request)
      .then((response) => {
        if (response.ok && new URL(event.request.url).origin === self.location.origin) {
          const copy = response.clone();
          caches.open(CACHE_NAME).then((cache) => cache.put(event.request, copy));
        }
        return response;
      })
      .catch(() => caches.match(event.request).then((cached) => cached || caches.match("./index.html")))
  );
});
