import { precacheAndRoute } from 'workbox-precaching';

// Workbox precache manifest — injected at build time by vite-plugin-pwa
precacheAndRoute(self.__WB_MANIFEST);

// ── Push notification handler ────────────────────────────────────────────────

self.addEventListener('push', (event) => {
  const data = event.data?.json() ?? {};
  event.waitUntil(
    self.registration.showNotification(data.title || 'Осколкам.Нет', {
      body: data.body || '',
      icon: '/pwa-192x192.png',
      badge: '/pwa-64x64.png',
      data: { url: data.url || '/admin/kanban' },
    }),
  );
});

self.addEventListener('notificationclick', (event) => {
  event.notification.close();
  event.waitUntil(
    clients.openWindow(event.notification.data?.url || '/admin/kanban'),
  );
});
