const CACHE='stepaway-v3-2-1-redteam-mobile-play-surface';
const CORE=['./','index.html','index.php','share.html','share.php','manifest.webmanifest','assets/css/app.css','assets/js/app.js','data/settings.json','data/cards.json','data/cases.json','data/campaigns.json','data/wrinkles.json','data/about.json','data/variables.json','content/packs/index.json'];
function isContentRequest(req){
  const url=new URL(req.url);
  return (url.pathname.endsWith('/api.php')&&url.searchParams.get('endpoint')==='content') || /\/(data|content)\/.+\.json$/.test(url.pathname);
}
self.addEventListener('install',e=>{e.waitUntil(caches.open(CACHE).then(c=>c.addAll(CORE)).then(()=>self.skipWaiting()))});
self.addEventListener('activate',e=>{e.waitUntil(caches.keys().then(keys=>Promise.all(keys.filter(k=>k!==CACHE).map(k=>caches.delete(k)))).then(()=>self.clients.claim()))});
self.addEventListener('fetch',e=>{
  if(e.request.method!=='GET')return;
  if(isContentRequest(e.request)){
    e.respondWith(fetch(e.request,{cache:'no-store'}).then(r=>{const cp=r.clone();caches.open(CACHE).then(c=>c.put(e.request,cp)).catch(()=>{});return r;}).catch(()=>caches.match(e.request)));
    return;
  }
  e.respondWith(caches.match(e.request).then(c=>c||fetch(e.request).then(r=>{const cp=r.clone();caches.open(CACHE).then(cache=>cache.put(e.request,cp)).catch(()=>{});return r}).catch(()=>caches.match('index.html'))));
});
