/// <reference types="@sveltejs/kit" />
/// <reference lib="webworker" />

// Native SvelteKit service worker (no Workbox). It precaches the app
// shell, the build artefacts (JS/CSS + core.wasm), and the static files
// under a version-keyed cache, so the installed PWA loads offline and a
// new deploy (new `version`) drops the stale cache instead of serving
// old code. The gateway is never intercepted — it is always live network.
//
// SvelteKit registers this worker automatically in the production build.

import { base, build, files, version } from "$service-worker";

const sw = self as unknown as ServiceWorkerGlobalScope;

const CACHE = `galaxy-cache-${version}`;
// `${base}/` is the SPA shell (adapter-static fallback); precaching it
// makes the start_url load offline. `base` is empty at the root and
// `/game` under the single-origin deployment, and `$service-worker`
// derives it from `location.pathname` so it stays correct in a subdir.
const PRECACHE = [`${base}/`, ...build, ...files];

sw.addEventListener("install", (event) => {
	event.waitUntil(
		caches
			.open(CACHE)
			.then((cache) => cache.addAll(PRECACHE))
			.then(() => sw.skipWaiting()),
	);
});

sw.addEventListener("activate", (event) => {
	event.waitUntil(
		(async () => {
			for (const key of await caches.keys()) {
				if (key !== CACHE) await caches.delete(key);
			}
			await sw.clients.claim();
		})(),
	);
});

sw.addEventListener("fetch", (event) => {
	const { request } = event;
	if (request.method !== "GET") return;
	const url = new URL(request.url);
	// Cross-origin (the gateway) is always live network — never cached.
	if (url.origin !== sw.location.origin) return;

	event.respondWith(
		(async () => {
			const cache = await caches.open(CACHE);
			// Version-keyed build/files: cache-first (content-hashed/immutable).
			if (PRECACHE.includes(url.pathname)) {
				const hit = await cache.match(url.pathname);
				if (hit) return hit;
			}
			// Everything else: network-first, falling back to the cache, and
			// for a navigation to the cached app shell when fully offline.
			try {
				const response = await fetch(request);
				if (response.ok && response.type === "basic") {
					cache.put(request, response.clone());
				}
				return response;
			} catch (err) {
				const cached = await cache.match(request);
				if (cached) return cached;
				if (request.mode === "navigate") {
					const shell = await cache.match(`${base}/`);
					if (shell) return shell;
				}
				throw err;
			}
		})(),
	);
});
