/**
 * POS service worker — caches pos.html for faster reload.
 * Full offline write queue / conflict handling is deferred (see pos.html).
 */
const CACHE_NAME = 'pos-shell-v1';
const POS_URLS = ['./pos.html', 'pos.html'];

self.addEventListener('install', (event) => {
    event.waitUntil(
        caches.open(CACHE_NAME).then(async (cache) => {
            for (const u of POS_URLS) {
                try {
                    await cache.add(u);
                    break;
                } catch (e) { /* try next */ }
            }
        })
    );
    self.skipWaiting();
});

self.addEventListener('activate', (event) => {
    event.waitUntil(self.clients.claim());
});

self.addEventListener('fetch', (event) => {
    if (event.request.mode !== 'navigate') return;
    event.respondWith(
        fetch(event.request).catch(() =>
            caches.match('./pos.html').then((r) => r || caches.match('pos.html'))
        )
    );
});
