const CACHE='forge-academy-0003-20260801T072507Z';
const ASSETS=[
  './css/style.css?v=0003-20260801T072507Z',
  './js/app.js?v=0003-20260801T072507Z',
  './js/i18n.js?v=0003-20260801T072507Z',
  './js/storage.js?v=0003-20260801T072507Z',
  './js/audio.js?v=0003-20260801T072507Z',
  './js/speech.js?v=0003-20260801T072507Z',
  './assets/images/clean_screen.png?v=0003-20260801T072507Z',
  './assets/images/puffy.png?v=0003-20260801T072507Z',
  './assets/images/byte.png?v=0003-20260801T072507Z',
  './assets/icons/icon.png?v=0003-20260801T072507Z',
  './manifest.json?v=0003-20260801T072507Z'
];

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

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

self.addEventListener('fetch',event=>{
  if(event.request.method!=='GET')return;

  if(event.request.mode==='navigate'){
    event.respondWith(
      fetch(event.request,{cache:'reload'})
        .then(response=>response)
        .catch(()=>caches.match('./index.html'))
    );
    return;
  }

  event.respondWith(
    fetch(event.request,{cache:'no-store'})
      .then(response=>{
        if(response.ok){
          const copy=response.clone();
          caches.open(CACHE).then(cache=>cache.put(event.request,copy));
        }
        return response;
      })
      .catch(()=>caches.match(event.request))
  );
});
