const CACHE_NAME = "tasty-eats-v20260406i";

const PRECACHE_URLS = [
  "./",
  "index.html",
  "about.html",
  "menu.html",
  "catering.html",
  "contact.html",
  "styles.css?v=20260406i",
  "script.js?v=20260406h",
  "site.webmanifest",
  "robots.txt",
  "sitemap.xml",
  "Backgrond.webp",
  "logo.webp",
  "logo-icon-192.png",
  "logo-icon-512.png",
  "tasty-eats-hat.svg",
  "tasty-eats-menu.pdf",
  "juicy-chuck-wors-combo.webp",
  "quarter-chicken-wors-combo.webp",
  "juicy-chuck-chicken-quarter-leg-combo.webp",
  "full-chicken-meal.webp",
  "food-slideshow/image1.webp",
  "food-slideshow/image2.webp",
  "food-slideshow/image3.webp",
  "food-slideshow/image4.webp",
  "food-slideshow/image5.webp",
  "food-slideshow/image6.webp",
  "food-slideshow/image7.webp",
  "food-slideshow/image8.webp",
  "food-slideshow/image9.webp",
  "food-slideshow/image10.webp",
  "food-slideshow/image11.webp",
  "food-slideshow/image12.webp",
  "food-slideshow/image13.webp",
  "food-slideshow/image14.webp",
  "food-slideshow/image15.webp",
  "food-slideshow/image16.webp",
  "food-slideshow/image17.webp",
  "food-slideshow/image18.webp",
  "food-slideshow/image19.webp",
  "food-slideshow/image20.webp",
];

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

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

const isSameOrigin = (requestUrl) => requestUrl.origin === self.location.origin;

const isHtmlRequest = (request) =>
  request.mode === "navigate" ||
  request.destination === "document" ||
  request.headers.get("accept")?.includes("text/html");

const networkFirst = async (request) => {
  const cache = await caches.open(CACHE_NAME);

  try {
    const response = await fetch(request);
    if (response && response.ok) {
      cache.put(request, response.clone());
    }
    return response;
  } catch (error) {
    const cachedResponse = await cache.match(request);
    if (cachedResponse) {
      return cachedResponse;
    }

    if (request.mode === "navigate") {
      return cache.match("index.html");
    }

    throw error;
  }
};

const staleWhileRevalidate = async (request) => {
  const cache = await caches.open(CACHE_NAME);
  const cachedResponse = await cache.match(request);

  const fetchPromise = fetch(request)
    .then((response) => {
      if (response && response.ok) {
        cache.put(request, response.clone());
      }
      return response;
    })
    .catch(() => cachedResponse);

  return cachedResponse || fetchPromise;
};

self.addEventListener("fetch", (event) => {
  const { request } = event;
  const requestUrl = new URL(request.url);

  if (request.method !== "GET" || !isSameOrigin(requestUrl)) {
    return;
  }

  if (isHtmlRequest(request)) {
    event.respondWith(networkFirst(request));
    return;
  }

  event.respondWith(staleWhileRevalidate(request));
});
