import{q as m,m as d,s as c,c as f}from"./index-DLUb5LSe.js";const S=[{key:"vaccinations",label:"Vaccine reminders",description:"When your pet has a vaccine coming due."},{key:"appointments",label:"Appointment alerts",description:"Confirmations, reminders, status changes."},{key:"lab",label:"Lab results",description:"When new test results are ready to view."},{key:"marketing",label:"Clinic news",description:"Promotions and announcements (occasional)."}],k="serviceWorker"in navigator&&"PushManager"in window&&"Notification"in window;function P(u){const s=u+"=".repeat((4-u.length%4)%4),i=atob(s.replace(/-/g,"+").replace(/_/g,"/")),r=new ArrayBuffer(i.length),l=new Uint8Array(r);for(let t=0;t<i.length;t++)l[t]=i.charCodeAt(t);return r}function A(){const u=c(k),s=c("default"),i=c(!1),r=c(!1),l=c(""),t=c(["vaccinations","appointments","lab"]),o=c(""),g=f(()=>u.value&&s.value==="default"),b=f(()=>s.value==="denied");m(async()=>{if(u.value&&(s.value=Notification.permission,s.value==="granted"))try{const e=await navigator.serviceWorker.ready,a=await e.pushManager.getSubscription();if(a){i.value=!0,l.value=a.endpoint;return}await v(e)}catch(e){o.value=(e==null?void 0:e.message)??"Couldn't read current subscription."}});async function v(e){const{data:a}=await d.get("/push/key");if(!a.public_key)throw new Error("Server VAPID key not configured.");const p=(await e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:P(a.public_key)})).toJSON();await d.post("/push/subscribe",{endpoint:p.endpoint,keys:p.keys,categories:t.value}),l.value=p.endpoint,i.value=!0}async function y(){var e,a;if(!u.value){o.value="Your browser doesn't support push notifications.";return}r.value=!0,o.value="";try{if(s.value!=="granted"){const p=await Notification.requestPermission();if(s.value=p,p!=="granted")throw new Error("Permission denied.")}const n=await navigator.serviceWorker.ready;await v(n)}catch(n){o.value=((a=(e=n==null?void 0:n.response)==null?void 0:e.data)==null?void 0:a.message)??(n==null?void 0:n.message)??"Couldn't enable notifications."}finally{r.value=!1}}async function w(){r.value=!0,o.value="";try{const a=await(await navigator.serviceWorker.ready).pushManager.getSubscription();a&&(await d.delete("/push/unsubscribe",{data:{endpoint:a.endpoint}}),await a.unsubscribe()),i.value=!1,l.value=""}catch(e){o.value=(e==null?void 0:e.message)??"Couldn't disable notifications."}finally{r.value=!1}}async function h(e){const a=t.value.indexOf(e);if(a>=0?t.value.splice(a,1):t.value.push(e),i.value)try{await d.patch("/push/preferences",{endpoint:l.value,categories:t.value})}catch(n){o.value=(n==null?void 0:n.message)??"Couldn't save preferences."}}return{supported:u,permission:s,subscribed:i,busy:r,canPrompt:g,blocked:b,error:o,categories:t,enable:y,disable:w,toggleCategory:h}}export{S as P,A as u};
//# sourceMappingURL=usePush-Dh4PUTt1.js.map
