/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/push-js@1.1.1/dist/Push.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
/* @preserve version 1.1.1 */
const INIT="INIT",UNSUPPORTED="UNSUPPORTED",PERMISSION_DENIED="PERMISSION_DENIED",PERMISSION_GRANTED="PERMISSION_GRANTED",PERMISSION_PROMPT="PERMISSION_PROMPT",ERROR="ERROR",STARTING_SUBSCRIBE="STARTING_SUBSCRIBE",SUBSCRIBED="SUBSCRIBED",STARTING_UNSUBSCRIBE="STARTING_UNSUBSCRIBE",UNSUBSCRIBED="UNSUBSCRIBED",states={INIT:{id:INIT,interactive:!1,subscribed:!1},UNSUPPORTED:{id:UNSUPPORTED,interactive:!1,subscribed:!1},PERMISSION_DENIED:{id:PERMISSION_DENIED,interactive:!1,subscribed:!1},PERMISSION_GRANTED:{id:PERMISSION_GRANTED,interactive:!0,subscribed:!1},PERMISSION_PROMPT:{id:PERMISSION_PROMPT,interactive:!0,subscribed:!1},ERROR:{id:ERROR,interactive:!1,subscribed:!1},STARTING_SUBSCRIBE:{id:STARTING_SUBSCRIBE,interactive:!1,subscribed:!1},SUBSCRIBED:{id:SUBSCRIBED,interactive:!0,subscribed:!0},STARTING_UNSUBSCRIBE:{id:STARTING_UNSUBSCRIBE,interactive:!1,subscribed:!1},UNSUBSCRIBED:{id:UNSUBSCRIBED,interactive:!0,subscribed:!1}},PUSH_JS_WINDOW="PUSH_JS_WINDOW",PUSH_JS_SW="PUSH_JS_SW";var noop=()=>{};class PermissionError extends Error{constructor(t){super(t),this.name="PermissionError",this.message=t||this.name,this.stack=(new Error).stack}}const MIN_LENGTH=10,randId=function t(e=10,s=""){const i=Math.random().toString(36).slice(2);return i.length>=10?s+i:t(e,s)},rejectedStates={UNSUBSCRIBED:!0,PERMISSION_DENIED:!0,ERROR:!0},notifications=Object.create(null);class Push{constructor({stateChangeCb:t=noop,subscriptionUpdateCb:e=noop,logCb:s=noop,serviceWorker:i}={}){this._state=Object.assign({},states.INIT),this._stateChange=t,this._subscriptionUpdateCb=e,this._log=s,this._serviceWorker=i,Push.checkSupport()?(this._setState(Push.getPermissionState()),rejectedStates[this._state.id]||(this._addListener(),this._installServiceWorker().then((()=>Push.getSubscription())).then((t=>{t?(this._setState(states.SUBSCRIBED),this._updateSubscription(t)):this._setState(states.UNSUBSCRIBED)})).catch((t=>{this._setState(states.ERROR,t)})))):this._setState(states.UNSUPPORTED)}_setState(t,e){this._log("State update: ",t,e),this._state=Object.assign({},this._state,t),this._stateChange(this._state,e)}_updateSubscription(t){this._log("Subscription update: ",t),this._subscription=t,this._subscriptionUpdateCb(this._subscription)}_installServiceWorker(){return this._serviceWorker?navigator.serviceWorker.register(this._serviceWorker).catch((t=>(this._setState(states.ERROR,t),Promise.reject(t)))):Promise.resolve()}_addListener(){navigator.serviceWorker.addEventListener("message",(t=>{let e={};try{e=JSON.parse(t.data)}catch(t){}if(e.source===PUSH_JS_SW&&e.data&&e.data.eventName&&notifications[e.data.tag]&&t.ports){const{tag:s,eventName:i}=e.data;this._log("Message from SW: ",e.data),t.ports[0].postMessage("OK"),notifications[s].dispatchEvent(new Event(i))}}))}_handleUnsubscribe(){return this._updateSubscription(null),this._setState(states.UNSUBSCRIBED),Promise.resolve()}_handleSubscribe(t){return this._updateSubscription(t),this._setState(states.SUBSCRIBED),Promise.resolve(t)}static getPermissionState(){return{denied:states.PERMISSION_DENIED,granted:states.PERMISSION_GRANTED,default:states.PERMISSION_PROMPT}[Notification.permission]||states.ERROR}static checkSupport(){return["serviceWorker","PushManager","permissions"].forEach((t=>{if(!(t in navigator))return!1})),!!Notification}static getSubscription(){return navigator.serviceWorker.ready.then((t=>t.pushManager.getSubscription()))}static requestPermission(){return new Promise(((t,e)=>{switch(Notification.permission){case"denied":return e(new PermissionError("Push messages are blocked."));case"granted":return t();default:return Notification.requestPermission((s=>{"granted"!==s?e(new PermissionError("Couldn't obtain permission.")):t()}))}}))}static showNotification(t,e={}){const s=e.tag||randId(),i=e.data||{};let r;return i._source=PUSH_JS_WINDOW,Push.requestPermission().then((()=>navigator.serviceWorker.ready)).then((r=>r.showNotification(t,Object.assign({},e,{data:i,tag:s})))).then((()=>navigator.serviceWorker.ready)).then((t=>t.getNotifications({tag:s}))).then((t=>(r=t[0],notifications[s]=r,r)))}subscribe(){return Push.getSubscription().then((t=>t?this._handleSubscribe(t):(this._setState(states.STARTING_SUBSCRIBE),Push.requestPermission().then((()=>navigator.serviceWorker.ready)).then((t=>t.pushManager.subscribe({userVisibleOnly:!0}))).then((t=>this._handleSubscribe(t))).catch((t=>(this._setState(t instanceof PermissionError?Push.getPermissionState():states.ERROR,t),Promise.reject(t)))))))}unsubscribe(){return this._setState(states.STARTING_UNSUBSCRIBE),Push.getSubscription().then((t=>t?t.unsubscribe():Promise.resolve(!0))).then((()=>{this._log("Unsubscribed successfully.")})).catch((()=>{this._log("Something went wrong, but whatever.")})).then((()=>this._handleUnsubscribe()))}}export{states};export default Push;
//# sourceMappingURL=/sm/76d53c8721c419f285e43ecdf28059f98af20ce9f0a905079718eb2ba82f4ace.map