const CACHE = 'onlycoin-v1';
const STATIC = [
  '/game/', '/game/index.html', '/game/styles.css', '/game/app.js',
  '/game/assets/onlycoin-logo.png', '/game/assets/onlycoin-token.png',
  '/game/assets/icon-192.png', '/game/assets/icon-512.png'
];
self.addEventListener('install', (event) => event.waitUntil(caches.open(CACHE).then((cache) => cache.addAll(STATIC))));
self.addEventListener('activate', (event) => event.waitUntil(caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))));
self.addEventListener('fetch', (event) => {
  if (event.request.method !== 'GET' || new URL(event.request.url).pathname.startsWith('/api/')) return;
  event.respondWith(fetch(event.request).then((response) => {
    const copy = response.clone();
    caches.open(CACHE).then((cache) => cache.put(event.request, copy));
    return response;
  }).catch(() => caches.match(event.request).then((cached) => cached || caches.match('/game/'))));
});
