var __FSCheckoutAdapterInternal__=function(h){"use strict";var tt=Object.defineProperty,et=Object.defineProperties;var st=Object.getOwnPropertyDescriptors;var O=Object.getOwnPropertySymbols;var W=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable;var U=(h,l,u)=>l in h?tt(h,l,{enumerable:!0,configurable:!0,writable:!0,value:u}):h[l]=u,y=(h,l)=>{for(var u in l||(l={}))W.call(l,u)&&U(h,u,l[u]);if(O)for(var u of O(l))Q.call(l,u)&&U(h,u,l[u]);return h},j=(h,l)=>et(h,st(l));var V=(h,l)=>{var u={};for(var d in h)W.call(h,d)&&l.indexOf(d)<0&&(u[d]=h[d]);if(h!=null&&O)for(var d of O(h))l.indexOf(d)<0&&Q.call(h,d)&&(u[d]=h[d]);return u};var o=(h,l,u)=>U(h,typeof l!="symbol"?l+"":l,u);var M=(h,l,u)=>new Promise((d,L)=>{var P=f=>{try{I(u.next(f))}catch(w){L(w)}},v=f=>{try{I(u.throw(f))}catch(w){L(w)}},I=f=>f.done?d(f.value):Promise.resolve(f.value).then(P,v);I((u=u.apply(h,l)).next())});const l={Log(...i){console&&console.log&&console.log(...i)},Error(...i){console&&console.error&&console.error(...i)},Warn(...i){console&&console.warn&&console.warn(...i)},Debug(...i){console&&console.debug&&console.debug(...i)}};function u(i,t){const e=i.contentWindow,s=i.src,n={};if(!e||!s)return null;const r=function(a){if(!(a&&a.origin&&a.origin.indexOf("js.stripe.com")>0||a.origin.indexOf("www.paypal.com")>0)&&a.origin===t&&a&&a.data&&typeof a.data=="string"&&a.data.charAt(0)==="{")try{const c=JSON.parse(a.data);typeof c=="object"&&c.type&&n[c.type]&&n[c.type].forEach(m=>{m(c.data)})}catch(c){l.Error(c)}};return window.addEventListener("message",r),{on(a,c){n[a]||(n[a]=[]),n[a].push(c)},one(a,c,m=!1){m&&delete n[a],!n[a]&&this.on(a,c)},destroy(){window.removeEventListener("message",r)},post(a,c){e.postMessage(JSON.stringify({type:a,data:c}),s.replace(/([^:]+:\/\/[^/]+).*/,"$1"))}}}const d=2147483647;function L(){const i=function(){return Math.floor((1+Math.random())*65536).toString(16).substring(1)};return i()+i()+"-"+i()+"-"+i()+"-"+i()+"-"+i()+i()+i()}function P(){let i=!1;try{const t=navigator.userAgent.toLowerCase();/edge\/|trident\/|msie /.test(t)?i=!0:t.indexOf("safari")!==-1&&(t.indexOf("chrome")>-1||(i=!0))}catch(t){}return i}function v(i){return typeof i=="undefined"||typeof i=="function"||typeof i=="object"&&i!==null}function I(i){return v(i)?null:i===null?"null":i===!0?"1":i===!1?"0":encodeURIComponent(i).replace(/!/g,"%21").replace(/'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29").replace(/\*/g,"%2A").replace(/%20/g,"+")}function f(i){const t=[];return Object.keys(i).forEach(e=>{const s=i[e],n=I(s);n!==null&&t.push(`${e}=${n}`)}),t.join("&")}function w(i){const t={};Object.entries(i).forEach(([s,n])=>{v(n)||(t[s]=n)});const{sandbox:e}=i;return e&&e.ctx&&e.token&&(t.s_ctx_ts=e.ctx,t.sandbox=e.token),t}function G(i){return i.pageY<=20}function E(){return typeof window=="undefined"}function A(i,t){if(i==null)throw new Error(t)}function z(i){if(i===null)return!1;const t=i.toLowerCase();return t==="1"||t==="true"}class H{constructor(t){o(this,"styleElement");o(this,"bodyScrollDisableClassName");o(this,"isFlashingBrowser");o(this,"overflow",{x:0,y:0});o(this,"metaColorSchemeValue",null);o(this,"metaColorSchemeElement",null);this.guid=t,this.bodyScrollDisableClassName=`is-fs-checkout-open-${this.guid}`,this.styleElement=document.createElement("style"),this.styleElement.textContent+=this.getBasicStyle(),this.isFlashingBrowser=P()||!E()&&!!document.querySelector("#___gatsby"),this.metaColorSchemeElement=document.head.querySelector('meta[name="color-scheme"]')}addStyle(t){this.styleElement.textContent+=t}attach(){return document.head.appendChild(this.styleElement),this}remove(){return document.head.contains(this.styleElement)&&document.head.removeChild(this.styleElement),this}disableBodyScroll(){this.backupScrollPosition(),document.body.classList.add(this.bodyScrollDisableClassName)}enableBodyScroll(){document.body.classList.remove(this.bodyScrollDisableClassName),this.restoreScrollPosition()}disableMetaColorScheme(){this.metaColorSchemeElement&&(this.metaColorSchemeValue=this.metaColorSchemeElement.getAttribute("content"),this.metaColorSchemeElement.setAttribute("content","light"))}enableMetaColorScheme(){this.metaColorSchemeElement&&this.metaColorSchemeValue&&(this.metaColorSchemeElement.setAttribute("content",this.metaColorSchemeValue),this.metaColorSchemeValue=null)}getBasicStyle(){return`body.${this.bodyScrollDisableClassName} { overflow: hidden !important; }`}backupScrollPosition(){this.overflow={x:document.documentElement.scrollTop,y:document.documentElement.scrollLeft}}restoreScrollPosition(){document.documentElement.scrollTop=this.overflow.x,document.documentElement.scrollLeft=this.overflow.y}}const S=class S{constructor(t,e,s="Loading Freemius Checkout"){o(this,"loaderElement");o(this,"isOpen",!1);o(this,"loaderElementId");this.style=t,this.loaderElementId=`fs-loader-${this.style.guid}`,this.loaderElement=document.createElement("div"),this.loaderElement.id=this.loaderElementId,this.loaderElement.innerHTML=`<img src="${e}" alt="${s}" />`,this.style.addStyle(this.getStyle())}show(){return this.isOpen?this:(this.loaderElement.classList.add("show"),document.body.appendChild(this.loaderElement),this.isOpen=!0,this)}hide(){return this.isOpen?(this.loaderElement.classList.remove("show"),this.isOpen=!1,setTimeout(()=>{this.hideImmediate()},S.TRANSITION_TIME),this):this}hideImmediate(){return this.loaderElement.parentNode&&(this.loaderElement.classList.remove("show"),this.loaderElement.parentNode.removeChild(this.loaderElement),this.isOpen=!1),this}getStyle(){return`#${this.loaderElementId} { display: none; position: fixed; z-index: ${d-1}; width: 100%; height: 100%; top: 0; left: 0; right: 0; bottom: 0; text-align: left; background: rgba(0, 0, 0, 0.6); transition: opacity ${S.TRANSITION_TIME}ms ease-out; will-change: opacity; opacity: 0; } #${this.loaderElementId}.show { opacity: 1; display: block; } #${this.loaderElementId} img { position: absolute; top: 40%; left: 50%; width: auto; height: auto; margin-left: -25px; background: #fff; padding: 10px; border-radius: 50%; box-shadow: 2px 2px 2px rgba(0,0,0,0.1); }`}};o(S,"TRANSITION_TIME",200);let D=S;const _=class _{constructor(t,e,s,n,r){o(this,"postman",null);o(this,"iFrame");o(this,"iFrameWrapper");o(this,"loadedEventListeners",new Set);o(this,"closedEventListeners",new Set);this.baseUrl=t,this.visibleClass=n,this.checkoutOptions=r;const{iFrame:a,iFrameWrapper:c}=this.attachIFrame(t,e,s);this.iFrame=a,this.iFrameWrapper=c,this.addEventListeners()}static getWrapperID(t){return`${t}-wrapper`}close(){var t;(t=this.postman)==null||t.post("close",null)}onClosed(t){this.closedEventListeners.add(t)}onLoaded(t){this.loadedEventListeners.add(t)}addToExitIntent(t){t.addListener(()=>{var e,s,n;(e=this.postman)==null||e.post("exit_intent",null),(n=(s=this.checkoutOptions).onExitIntent)==null||n.call(s)})}attachIFrame(t,e,s){var m;const n=`${t}/?${f(e)}#${encodeURIComponent(document.location.href)}`,r=document.createElement("iframe");r.id=s,r.src=n,r.setAttribute("allowTransparency","true"),r.setAttribute("width","100%"),r.setAttribute("height","100%"),r.setAttribute("style","background: rgba(0,0,0,0.003); border: 0 none transparent;"),r.setAttribute("frameborder","0"),r.setAttribute("data-testid",r.id);const a=(m=this.checkoutOptions.modal_title)!=null?m:_.DEFAULT_TITLE;r.setAttribute("title",a);const c=document.createElement("div");return c.setAttribute("role","dialog"),c.setAttribute("aria-label",a),c.setAttribute("aria-modal","true"),c.setAttribute("data-testid",`${r.id}-wrapper`),c.id=_.getWrapperID(s),c.appendChild(r),document.body.appendChild(c),{iFrame:r,iFrameWrapper:c}}addEventListeners(){var c,m,C,k,q;const{success:t,purchaseCompleted:e,cancel:s,track:n,afterOpen:r,afterClose:a}=this.checkoutOptions;this.postman=u(this.iFrame,this.baseUrl),(c=this.postman)==null||c.one("upgraded",p=>{try{t==null||t(p)}catch(g){l.Error(g)}this.dispatchOnClosed(),this.removeIFrameAndPostman(),a==null||a()},!0),(m=this.postman)==null||m.one("purchaseCompleted",p=>{try{e==null||e(p)}catch(g){l.Error(g)}},!0),(C=this.postman)==null||C.one("canceled",()=>{try{s==null||s()}catch(p){l.Error(p)}this.dispatchOnClosed(),this.removeIFrameAndPostman(),a==null||a()},!0),(k=this.postman)==null||k.on("track",p=>{try{n==null||n(p.event,p)}catch(g){l.Error(g)}}),(q=this.postman)==null||q.one("loaded",()=>{var p;this.dispatchOnLoaded(),(p=this.iFrame)==null||p.classList.add(this.visibleClass);try{r==null||r()}catch(g){l.Error(g)}},!0)}removeIFrameAndPostman(){var t;(t=this.postman)==null||t.destroy(),this.postman=null,this.iFrame.remove(),this.iFrameWrapper.remove(),this.closedEventListeners.clear(),this.loadedEventListeners.clear()}dispatchOnLoaded(){this.loadedEventListeners.forEach(t=>t())}dispatchOnClosed(){this.closedEventListeners.forEach(t=>t())}};o(_,"DEFAULT_TITLE","Checkout");let x=_;const F=class F{constructor(t,e,s){o(this,"iFrameID");this.style=t,this.options=e,this.baseUrl=s,this.iFrameID=`fs-checkout-page-${this.style.guid}`}create(t){const e=y(y({},this.options),t);return new x(this.baseUrl,this.getQueryParams(e),this.iFrameID,F.VISIBLE_CLASS,e)}addStyle(){this.style.addStyle(this.getStyle())}getQueryParams(t){return y({mode:"dialog",guid:this.style.guid,_fs_checkout:!0},w(t))}getStyle(){return`#${this.iFrameID} { z-index: ${d-1}; background: rgba(0,0,0,0.003); border: 0 none transparent; visibility: ${this.style.isFlashingBrowser?"hidden":"visible"}; margin: 0; padding: 0; position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; -webkit-tap-highlight-color: transparent; overflow: hidden; } #${this.iFrameID}.${F.VISIBLE_CLASS} { visibility: visible; } #${x.getWrapperID(this.iFrameID)} { z-index: ${d-1}; position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; overflow: hidden; }`}};o(F,"VISIBLE_CLASS","show");let T=F;class K{constructor(t,e,s,n,r){o(this,"checkoutIFrameBuilder");o(this,"checkoutIFrame",null);o(this,"lastFocusedElement",null);this.style=t,this.exitIntent=e,this.loader=s,this.checkoutIFrameBuilder=new T(this.style,r,n),this.checkoutIFrameBuilder.addStyle()}isOpen(){return this.checkoutIFrame!==null}open(t){return this.isOpen()?this:(this.backupFocusedElement(),this.style.attach(),this.style.disableBodyScroll(),this.style.disableMetaColorScheme(),this.loader.show(),this.exitIntent.attach(),this.checkoutIFrame=this.checkoutIFrameBuilder.create(t),this.checkoutIFrame.onLoaded(this.onLoaded.bind(this)),this.checkoutIFrame.onClosed(this.onClosed.bind(this)),this.checkoutIFrame.addToExitIntent(this.exitIntent),this)}close(){var t;return(t=this.checkoutIFrame)==null||t.close(),this.loader.hideImmediate(),this}onLoaded(){this.loader.hide()}onClosed(){this.checkoutIFrame=null,this.loader.hide(),this.style.enableBodyScroll(),this.style.enableMetaColorScheme(),this.exitIntent.detach(),this.restoreFocusedElement(),this.style.remove()}backupFocusedElement(){var t;this.lastFocusedElement=(t=document.activeElement)!=null?t:null}restoreFocusedElement(){var t,e;this.lastFocusedElement&&((e=(t=this.lastFocusedElement).focus)==null||e.call(t),this.lastFocusedElement=null)}}class J{constructor(t){o(this,"exitIntentId");o(this,"exitIntentDiv",null);o(this,"clearExitIntentListener",null);o(this,"listeners",[]);this.style=t,this.exitIntentId=`fs-exit-intent-${this.style.guid}`,this.style.addStyle(this.getStyle())}isAttached(){return this.exitIntentDiv!==null}addListener(...t){this.listeners.push(...t)}attach(...t){if(t&&this.addListener(...t),this.isAttached())return this;this.exitIntentDiv=document.createElement("div"),this.exitIntentDiv.id=this.exitIntentId,document.body.appendChild(this.exitIntentDiv);const e=document.documentElement;let s=null;const n=300,r=c=>{G(c)&&(s=window.setTimeout(()=>{try{this.fireListeners()}catch(m){l.Error(m)}},n))},a=()=>{s&&(clearTimeout(s),s=null)};return e.addEventListener("mouseleave",r),e.addEventListener("mouseenter",a),this.clearExitIntentListener=()=>{s&&(clearTimeout(s),s=null),e.removeEventListener("mouseleave",r),e.removeEventListener("mouseenter",a)},this}detach(){var t,e;return this.isAttached()?((t=this.exitIntentDiv)==null||t.remove(),this.exitIntentDiv=null,(e=this.clearExitIntentListener)==null||e.call(this),this):this}fireListeners(){this.listeners.forEach(t=>{t()})}getStyle(){return`#${this.exitIntentId} { z-index: ${d}; border: 0; background: transparent; position: fixed; padding: 0; margin: 0; height: 10px; left: 0; right: 0; width: 100%; top: 0; }`}}const b=class b{constructor(t){o(this,"queryParams",null);this.url=t,this.queryParams=this.parseQueryStringForCart()}hasCart(){return this.queryParams!==null}getPluginID(){return A(this.queryParams,b.NO_CART_FOUND_MESSAGE),this.queryParams.__fs_plugin_id}getPluginPublicKey(){return A(this.queryParams,b.NO_CART_FOUND_MESSAGE),this.queryParams.__fs_plugin_public_key}matchesPluginID(t){const e=Number.parseInt(this.getPluginID(),10),s=Number.parseInt(t.toString(),10);return Number.isFinite(e)&&Number.isFinite(s)&&e===s}getCheckoutOptions(){A(this.queryParams,b.NO_CART_FOUND_MESSAGE);const t={plugin_id:"",public_key:""};return Object.entries(this.queryParams).forEach(([e,s])=>{e==="__fs_plugin_id"?t.plugin_id=s:e==="__fs_plugin_public_key"?t.public_key=s:t[e]=s}),t}parseQueryStringForCart(){const t="__fs_authorization",e=new URLSearchParams(this.url.search);if(!e.has(t))return null;const s={};return e.forEach((n,r)=>{r.startsWith("__fs_")&&(s[r]=n)}),s}};o(b,"NO_CART_FOUND_MESSAGE","No cart was found");let N=b;const B="https://checkout.freemius.com",X="http://checkout.freemius-local.com:8080";class ${constructor(t,e=!0,s=null){o(this,"options",{plugin_id:0,public_key:""});o(this,"guid");o(this,"baseUrl");o(this,"style");o(this,"loader");o(this,"checkoutPopup");o(this,"exitIntent");o(this,"cart");var C,k;this.baseUrl=s!=null?s:B;const m=t,{plugin_id:n,product_id:r,public_key:a}=m,c=V(m,["plugin_id","product_id","public_key"]);if(!n&&!r)throw new Error("Must provide a product_id to options.");this.options=j(y({},c),{public_key:a,plugin_id:r!=null?r:n}),this.guid=L(),!E()&&(this.baseUrl=s!=null?s:(C=window.FS)!=null&&C.__FS__IS_TEST__?X:B,this.style=new H(this.guid),this.loader=new D(this.style,(k=t.loadingImageUrl)!=null?k:`${this.baseUrl}/assets/img/spinner.svg`,t.loadingImageAlt),this.exitIntent=new J(this.style),this.checkoutPopup=new K(this.style,this.exitIntent,this.loader,this.baseUrl,this.options),this.cart=new N(new URL(window.location.href)),e&&this.recoverCart())}open(t){return M(this,null,function*(){var e,s;if(!E()){if((e=this.checkoutPopup)!=null&&e.isOpen()){l.Warn("Checkout popup already open. Please close it first before opening it again.");return}if(document.body)(s=this.checkoutPopup)==null||s.open(t);else return new Promise(n=>{document.addEventListener("DOMContentLoaded",()=>{var r;(r=this.checkoutPopup)==null||r.open(t),n()},{once:!0})})}})}close(){var t;E()||(t=this.checkoutPopup)==null||t.close()}destroy(){E()||this.close()}getGuid(){return this.guid}recoverCart(){var t,e;if((t=this.cart)!=null&&t.hasCart()&&((e=this.cart)!=null&&e.matchesPluginID(this.options.plugin_id))){const s=this.cart.getCheckoutOptions();this.open(s)}}}class Y{constructor(t){o(this,"parsedUrl",null);try{this.parsedUrl=new URL(t)}catch(e){this.parsedUrl=null}}hasDunning(){var s;if(!this.parsedUrl)return!1;const t=this.parsedUrl.searchParams.get("_fs_dunning"),e=(s=this.parsedUrl.searchParams.get("_fs_plugin_id"))!=null?s:this.parsedUrl.searchParams.get("_fs_product_id");return z(t)&&e!==null}getCheckoutOptions(){if(!this.parsedUrl)throw new Error("No dunning information found in the URL.");const t={product_id:""};return this.parsedUrl.searchParams.forEach((s,n)=>{if(n.startsWith("_fs_")){const r=n.replace("_fs_","");t[r]=s}}),Object.hasOwn(t,"plugin_id")&&(t.product_id=t.plugin_id,delete t.plugin_id),t}}function Z(i){return M(this,null,function*(){if(E())return null;const t=new Y(window.location.href);if(t.hasDunning()){const e=new $(t.getCheckoutOptions());return yield e.open(i),e}return null})}window.FS=window.FS||{},window.FS.Checkout=$,window.FS.postman=u,window.FS.Logger=l,Z(window.FS.paymentMethodUpdateEvents).catch(i=>{l.Error("Error restoring dunning checkout:",i)});class R{constructor(){o(this,"checkout",null);o(this,"options",null)}configure(t,e){var s,n;if(!this.checkout&&(this.checkout=new $(t,!1,e),(s=this.checkout.cart)!=null&&s.hasCart())){const r=(n=this.checkout.cart)==null?void 0:n.getCheckoutOptions();this.checkout.open(r)}return this.options=t,this}open(t={}){var s,n;((s=this.checkout)!=null?s:this.configure(t)).open(y(y({},(n=this.options)!=null?n:{}),t))}close(){var t;(t=this.checkout)==null||t.close()}clearOptions(){this.options=null}getGuid(){var t,e;return(e=(t=this.checkout)==null?void 0:t.getGuid())!=null?e:""}}return window.FS.Checkout=new R,h.FSOldCheckout=R,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"}),h}({});
//# sourceMappingURL=checkout.adapter.js.map
