self.addEventListener("install", (event) => {
  console.log("✅ Service Worker Installed");
  self.skipWaiting();
});

self.addEventListener("activate", (event) => {
  console.log("✅ Service Worker Activated");
  self.clients.claim();
});

// ✅ Enable Background Fetch
self.addEventListener("fetch", (event) => {
  if (event.request.url.includes("/background-fetch")) {
      event.respondWith(updateLocation());
  }
});

// ✅ Function to trigger a background sync
async function updateLocation() {
  const clients = await self.clients.matchAll();
  if (clients.length === 0) return;

  clients.forEach(client => {
      client.postMessage({ action: "getLocation" });
  });
}
