// @ts-nocheck


// sw.js
// self.addEventListener('install', event => {
//     event.waitUntil(
//         caches.open('static-cache').then(() => {
//             // cache files
//         })
//     );
// });

self.addEventListener('activate', event => {
    // alert("Service worker activated");
    console.log('Service worker activating...');
    event.waitUntil(self.clients.claim());
    console.log('Service worker activated');
    setTimeout(() => {
        notif();
    }, 5000);

});

// self.addEventListener('fetch', event => {
//     event.respondWith(
//         caches.match(event.request).then(response => {
//             return response || fetch(event.request);
//         })
//     );
// });

// Send notification after 5 seconds
function notif() {
    console.log('Checking notifications...');
    fetch("/notif?username=user1").then((res) => {
        res.json().then((data) => {
            if (data.length) {
                for (const d of data) {
                    console.log(d);
                    self.registration.showNotification('New Notification', {
                        body: d.text,
                    });
                    console.log('Sent notification');
                }
            } else {
                console.log('No new notifications');
            }
        });
    });
    // self.registration.showNotification('Test Notification', {
    //     body: 'This is a test notification sent by the service worker!',
    // });
    setTimeout(() => {
        notif();
    }, 5000);
}
setTimeout(() => {
    notif();
}, 5000);
