const CACHE='sitesight-v1998-normal-package-shell-1';
const SHELL=['/','/index.html','/styles.css','/styles-v1992.css','/styles-v1993.css','/styles-v1994.css','/styles-v1995.css','/styles-v1996.css','/app.js','/mobile-v1995.js','/mobile-v1996.js','/commercial-v1996.js','/field-v1996.js','/integration-v1996.js','/data/sitesight.json','/assets/vendor/pdfjs/pdf.min.mjs','/assets/vendor/pdfjs/pdf.worker.min.mjs','/manifest.webmanifest',
'/assets/sitesight-logo.png','/assets/sitesight-login-logo.png','/assets/sitesight-app-icon-192.png','/assets/sitesight-app-icon-512.png'];
self.addEventListener('install',event=>event.waitUntil(caches.open(CACHE).then(c=>c.addAll(SHELL)).then(()=>self.skipWaiting())));
self.addEventListener('activate',event=>event.waitUntil(caches.keys().then(keys=>Promise.all(keys.filter(k=>k!==CACHE).map(k=>caches.delete(k)))).then(()=>self.clients.claim())));
self.addEventListener('fetch',event=>{
 const u=new URL(event.request.url);
 if(event.request.method!=='GET'||u.origin!==location.origin)return;
 if(u.pathname.startsWith('/api/')||u.pathname.startsWith('/uploads/'))return;
 if(event.request.mode==='navigate'){
   event.respondWith(fetch(event.request).then(r=>{
     let html=String(r.headers.get('content-type')||'').includes('text/html');
     if(r.ok&&!r.redirected&&html)caches.open(CACHE).then(c=>c.put(event.request,r.clone()));
     return r
   }).catch(()=>caches.match('/index.html')));
   return;
 }
 event.respondWith(caches.match(event.request).then(hit=>hit||fetch(event.request).then(r=>{let copy=r.clone();caches.open(CACHE).then(c=>c.put(event.request,copy));return r})));
});
self.addEventListener('push',event=>{
 let payload={title:'SiteSight Clock Out reminder',body:'Your scheduled finish time has arrived. Clock Out when you have finished.',url:'/?view=attendance'};
 try{payload={...payload,...event.data.json()}}catch(e){}
 event.waitUntil(self.registration.showNotification(payload.title,{body:payload.body,icon:'/assets/sitesight-app-icon-192.png',badge:'/assets/sitesight-app-icon-192.png',tag:payload.tag||'sitesight-clock-reminder',renotify:true,data:{url:payload.url||'/?view=attendance'}}))
});
self.addEventListener('notificationclick',event=>{
 event.notification.close();let target=event.notification.data?.url||'/?view=attendance';
 event.waitUntil(clients.matchAll({type:'window',includeUncontrolled:true}).then(rows=>{let open=rows[0];if(open){open.postMessage({type:'SITESIGHT_OPEN_TARGET',url:target});return open.focus()}return clients.openWindow(target)}))
});
