const publicVapidKey =
  "BK8cnyTGqgS7KbiqNoT3tYQgiDXplgnumSPKijvFeqnDKf9u1PJNy6NBeIZKy2FaU2V3MrgUG_ErjkuZWmRJqsQ";
async function addDevice() {
    var subscription;
  if ("serviceWorker" in navigator) {
    subscription = await registerServiceWorker().catch(console.log);
  }
  return subscription;
}
async function registerServiceWorker() {
  console.log('registre')
  const register = await navigator.serviceWorker.register("./worker.js", {
    scope: "/",
  });

  const subscription = await register.pushManager.subscribe({
    userVisibleOnly: true,
    applicationServerKey: publicVapidKey,
  });
  console.log("Pas 1 fet");
  await fetch("/subscribe", {
    method: "POST",
    body: JSON.stringify(subscription),
    headers: {
      "Content-Type": "application/json",
    },
  });

  return subscription;
}
