import{r as h,$ as t,f as B,i as s,M as d,ab as M,ac as D,P as A}from"./app-JmgyQoR0.js";import{b as K,a as p,h as f,i as c,j as y,K as w,E as g,z as o,H as T}from"./vue-y3KLWBaS.js";const V=K({__name:"MkPushNotificationAllowButton",props:{primary:{type:Boolean},gradate:{type:Boolean},rounded:{type:Boolean},inline:{type:Boolean},link:{type:Boolean},to:{},autofocus:{type:Boolean},wait:{type:Boolean},danger:{type:Boolean},full:{type:Boolean},showOnlyToRegister:{type:Boolean}},setup(U,{expose:N}){let l=p(),u=p(!1),a=p(null),n=p();function C(){if(!(!l.value||!u.value||!h.swPublickey))return M(l.value.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:P(h.swPublickey)}).then(async e=>{a.value=e,n.value=await B("sw/register",{endpoint:e.endpoint,auth:k(e.getKey("auth")),publickey:k(e.getKey("p256dh"))})},async e=>{if((e==null?void 0:e.name)==="NotAllowedError"){console.info("User denied the notification permission request.");return}await b()}),null,null)}async function b(){if(!a.value)return;const e=a.value.endpoint,i=await D();n.value=void 0,t&&i.length>=2?A("sw/unregister",{i:t.token,endpoint:e}):(a.value.unsubscribe(),A("sw/unregister",{endpoint:e}),a.value=null)}function k(e){return btoa(String.fromCharCode.apply(null,new Uint8Array(e)))}function P(e){const i="=".repeat((4-e.length%4)%4),S=(e+i).replace(/-/g,"+").replace(/_/g,"/"),v=window.atob(S),m=new Uint8Array(v.length);for(let r=0;r<v.length;++r)m[r]=v.charCodeAt(r);return m}return navigator.serviceWorker==null||navigator.serviceWorker.ready.then(async e=>{if(l.value=e,a.value=await l.value.pushManager.getSubscription(),h.swPublickey&&"PushManager"in window&&t&&t.token&&(u.value=!0,a.value)){const i=await B("sw/show-registration",{endpoint:a.value.endpoint});i&&(n.value=i)}}),N({pushRegistrationInServer:n}),(e,i)=>u.value&&!n.value?(f(),c(d,{key:0,type:"button",primary:"",gradate:e.gradate,rounded:e.rounded,inline:e.inline,autofocus:e.autofocus,wait:e.wait,full:e.full,onClick:C},{default:y(()=>[w(g(o(s).ts.subscribePushNotification),1)]),_:1},8,["gradate","rounded","inline","autofocus","wait","full"])):!e.showOnlyToRegister&&(o(t)?n.value:a.value)?(f(),c(d,{key:1,type:"button",primary:!1,gradate:e.gradate,rounded:e.rounded,inline:e.inline,autofocus:e.autofocus,wait:e.wait,full:e.full,onClick:b},{default:y(()=>[w(g(o(s).ts.unsubscribePushNotification),1)]),_:1},8,["gradate","rounded","inline","autofocus","wait","full"])):o(t)&&n.value?(f(),c(d,{key:2,disabled:"",rounded:e.rounded,inline:e.inline,wait:e.wait,full:e.full},{default:y(()=>[w(g(o(s).ts.pushNotificationAlreadySubscribed),1)]),_:1},8,["rounded","inline","wait","full"])):u.value?T("",!0):(f(),c(d,{key:3,disabled:"",rounded:e.rounded,inline:e.inline,wait:e.wait,full:e.full},{default:y(()=>[w(g(o(s).ts.pushNotificationNotSupported),1)]),_:1},8,["rounded","inline","wait","full"]))}});export{V as _};
