/**
 * ACC Service Widget PWA — cache ویجت چت برای دسترسی آفلاین محدود
 */
const CACHE = 'acc-svc-widget-v1';
const ASSETS = [
    './icon.svg',
    '../css/base/chat-widget.css',
    '../js/chat-widget/widget.js',
];

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((k) => k !== CACHE).map((k) => caches.delete(k)))
        ).then(() => self.clients.claim())
    );
});

self.addEventListener('fetch', (event) => {
    if (event.request.method !== 'GET') return;
    event.respondWith(
        caches.match(event.request).then((cached) => cached || fetch(event.request))
    );
});
