var e=1e4;function t(){return Promise.race([navigator.serviceWorker.ready,new Promise((t,n)=>setTimeout(()=>n(Error(`Service worker not ready (timed out)`)),e))])}function n(e){let t=(e+`=`.repeat((4-e.length%4)%4)).replace(/-/g,`+`).replace(/_/g,`/`),n=atob(t),r=new Uint8Array(n.length);for(let e=0;e<n.length;e++)r[e]=n.charCodeAt(e);return r}async function r(){return`serviceWorker`in navigator?await(await t()).pushManager.getSubscription():null}async function i(e){var r,i;let a=await Notification.requestPermission();if(a!==`granted`)throw Error(`Notification permission ${a}`);let o=(await e.request(`push.web.vapidPublicKey`,{})).vapidPublicKey;if(!o)throw Error(`Failed to retrieve VAPID public key`);let s=await(await t()).pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:n(o).buffer}),c=s.toJSON();if(!c.endpoint||!((r=c.keys)!=null&&r.p256dh)||!((i=c.keys)!=null&&i.auth))throw Error(`Invalid push subscription from browser`);try{return await e.request(`push.web.subscribe`,{endpoint:c.endpoint,keys:{p256dh:c.keys.p256dh,auth:c.keys.auth}})}catch(e){try{await s.unsubscribe()}catch{}throw e}}async function a(e){let n=await(await t()).pushManager.getSubscription();if(n){try{await e.request(`push.web.unsubscribe`,{endpoint:n.endpoint})}catch{}await n.unsubscribe()}}async function o(e,t){await e.request(`push.web.test`,{title:t==null?void 0:t.title,body:t==null?void 0:t.body})}export{r as getExistingSubscription,o as sendTestWebPush,i as subscribeToWebPush,a as unsubscribeFromWebPush};
//# sourceMappingURL=push-subscription-GEdjvonj.js.map