const CACHE_NAME = "know-god-more-v134";
const ASSETS = [
  "./",
  "./index.html",
  "./styles.css?v=134",
  "./legacy-polyfills.js?v=134",
  "./app.js?v=134",
  "./renewal-config.js",
  "./data/stories.js",
  "./data/company-profile.js?v=134",
  "./data/lective-subject-manifest.js?v=134",
  "./manifest.webmanifest",
  "./assets/certificate.png",
  "./assets/icon.png",
  "./assets/login.jpg",
  "./assets/mic.jpg",
  "./assets/speaker.png",
  "./assets/questions.png",
  "./assets/reading.jpg",
  "./assets/audio/click.wav",
  "./assets/audio/correct-answer.wav",
  "./assets/audio/wrong-answer.mp3",
  "./assets/audio/certificate-100.mp3",
  "./assets/audio/certificate-less-than-100.mp3"
];

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

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

self.addEventListener("fetch", (event) => {
  if (event.request.method !== "GET") return;
  event.respondWith(caches.match(event.request).then((cached) => cached || fetch(event.request)));
});
