!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).GooglePayButton=e()}(this,(function(){"use strict";
function t(t,e,n,o){var i,a=arguments.length,r=a<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,n):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(t,e,n,o);else for(var s=t.length-1;s>=0;s--)(i=t[s])&&(r=(a<3?i(r):a>3?i(e,n,r):i(e,n))||r);return a>3&&r&&Object.defineProperty(e,n,r),r}function e(t,e,n,o){return new(n||(n=Promise))((function(i,a){function r(t){try{l(o.next(t))}catch(t){a(t)}}function s(t){try{l(o.throw(t))}catch(t){a(t)}}function l(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(r,s)}l((o=o.apply(t,e||[])).next())}))}function n(t){return t.replace(/[A-Z]+/g,t=>"-"+t).replace(/^-/,"").toLowerCase()}function o(t){return function(e,o){const i=t||n(o);e.addObservedAttribute&&e.addObservedAttribute(i),Object.defineProperty(e,o,{get(){return this.getAttribute(i)},set(t){null==t?this.removeAttribute(i):this.setAttribute(i,t),this.notifyPropertyChanged(o)},enumerable:!0,configurable:!0})}}function i(t){return t?o(t):function(t,e){Object.defineProperty(t,e,{get(){return this["$__"+e]},set(t){this["$__"+e]=t,this.notifyPropertyChanged(e)},enumerable:!0,configurable:!0})}}function a(t){return function(e,n){Object.defineProperty(e,t,{get(){return this[n]},set(t){this[n]=t},enumerable:!0,configurable:!1})}}let r={};function s(t){const e=r[t];if(e)return e;const n=new Promise((e,n)=>{const o=document.createElement("script");o.src=t,o.async=!0;const i=()=>{e()},a=()=>{o.removeEventListener("load",i),o.removeEventListener("error",a),delete r[t],o.remove(),n(new Error("Unable to load script "+t))};o.addEventListener("load",i),o.addEventListener("error",a),document.body.appendChild(o)});return r[t]=n,n}class l{constructor(t){this.handleClick=t=>e(this,void 0,void 0,(function*(){const e=this.config;if(!e)throw new Error("google-pay-button: Missing configuration");const n=this.createLoadPaymentDataRequest(e);try{if(e.onClick&&e.onClick(t),t.defaultPrevented)return;const o=yield this.client.loadPaymentData(n);e.onLoadPaymentData&&e.onLoadPaymentData(o)}catch(t){"CANCELED"===t.statusCode?e.onCancel&&e.onCancel(t):e.onError?e.onError(t):console.error(t)}})),this.options=t}getElement(){return this.element}isGooglePayLoaded(){var t,e;return"google"in(window||global)&&!!(null===(e=null===(t=null===google||void 0===google?void 0:google.payments)||void 0===t?void 0:t.api)||void 0===e?void 0:e.PaymentsClient)}mount(t){return e(this,void 0,void 0,(function*(){this.isGooglePayLoaded()||(yield s("https://pay.google.com/gp/p/js/pay.js")),this.element=t,t&&(this.appendStyles(),this.config&&this.updateElement())}))}unmount(){this.element=void 0}configure(t){let e=void 0;return this.config=t,this.oldInvalidationValues&&!this.isClientInvalidated(t)||(e=this.updateElement()),this.oldInvalidationValues=this.getInvalidationValues(t),null!=e?e:Promise.resolve()}createClientOptions(t){const e={environment:t.environment,merchantInfo:this.createMerchantInfo(t)};return(t.onPaymentDataChanged||t.onPaymentAuthorized)&&(e.paymentDataCallbacks={},t.onPaymentDataChanged&&(e.paymentDataCallbacks.onPaymentDataChanged=e=>t.onPaymentDataChanged(e)||{}),t.onPaymentAuthorized&&(e.paymentDataCallbacks.onPaymentAuthorized=e=>t.onPaymentAuthorized(e)||{})),e}createIsReadyToPayRequest(t){const e=t.paymentRequest;return{apiVersion:e.apiVersion,apiVersionMinor:e.apiVersionMinor,allowedPaymentMethods:e.allowedPaymentMethods,existingPaymentMethodRequired:t.existingPaymentMethodRequired}}createLoadPaymentDataRequest(t){return Object.assign(Object.assign({},t.paymentRequest),{merchantInfo:this.createMerchantInfo(t)})}createMerchantInfo(t){const e=Object.assign({},t.paymentRequest.merchantInfo);return e.softwareInfo||(e.softwareInfo={id:this.options.softwareInfoId,version:this.options.softwareInfoVersion}),e}isMounted(){return null!=this.element&&!1!==this.element.isConnected}removeButton(){if(this.element instanceof ShadowRoot||this.element instanceof Element)for(const t of Array.from(this.element.children))"STYLE"!==t.tagName&&t.remove()}updateElement(){return e(this,void 0,void 0,(function*(){if(!this.isMounted())return;const t=this.getElement();if(!this.config)throw new Error("google-pay-button: Missing configuration");this.removeButton(),this.client=new google.payments.api.PaymentsClient(this.createClientOptions(this.config));const e={buttonType:this.config.buttonType,buttonColor:this.config.buttonColor,buttonSizeMode:this.config.buttonSizeMode,buttonLocale:this.config.buttonLocale,onClick:this.handleClick,allowedPaymentMethods:this.config.paymentRequest.allowedPaymentMethods},n=t.getRootNode();n instanceof ShadowRoot&&(e.buttonRootNode=n);const o=this.client.createButton(e);this.setClassName(t,[t.className,"not-ready"]),t.appendChild(o);let i,a=!1;try{i=yield this.client.isReadyToPay(this.createIsReadyToPayRequest(this.config)),a=i.result&&!this.config.existingPaymentMethodRequired||i.result&&i.paymentMethodPresent&&this.config.existingPaymentMethodRequired||!1}catch(t){this.config.onError?this.config.onError(t):console.error(t)}if(this.isMounted()){if(a){try{this.client.prefetchPaymentData(this.createLoadPaymentDataRequest(this.config))}catch(t){console.log("Error with prefetch",t)}this.setClassName(t,(t.className||"").split(" ").filter(t=>t&&"not-ready"!==t))}if((this.isReadyToPay!==(null==i?void 0:i.result)||this.paymentMethodPresent!==(null==i?void 0:i.paymentMethodPresent))&&(this.isReadyToPay=!!(null==i?void 0:i.result),this.paymentMethodPresent=null==i?void 0:i.paymentMethodPresent,this.config.onReadyToPayChange)){const t={isButtonVisible:a,isReadyToPay:this.isReadyToPay};this.paymentMethodPresent&&(t.paymentMethodPresent=this.paymentMethodPresent),this.config.onReadyToPayChange(t)}}}))}setClassName(t,e){const n=e.filter(t=>t).join(" ");n?t.className=n:t.removeAttribute("class")}appendStyles(){var t,e,n;if("undefined"==typeof document)return;const o=null===(t=this.element)||void 0===t?void 0:t.getRootNode(),i=`default-google-style-${this.options.cssSelector.replace(/[^\w-]+/g,"")}-${null===(e=this.config)||void 0===e?void 0:e.buttonLocale}`;if(o&&!(null===(n=o.getElementById)||void 0===n?void 0:n.call(o,i))){const t=document.createElement("style");t.id=i,t.type="text/css",t.innerHTML=`\n          ${this.options.cssSelector} {\n            display: inline-block;\n          }\n          ${this.options.cssSelector}.not-ready {\n            width: 0;\n            height: 0;\n            overflow: hidden;\n          }\n        `,o instanceof Document&&o.head?o.head.appendChild(t):o.appendChild(t)}}isClientInvalidated(t){if(!this.oldInvalidationValues)return!0;return this.getInvalidationValues(t).some((t,e)=>t!==this.oldInvalidationValues[e])}getInvalidationValues(t){var e,n;return[t.environment,t.existingPaymentMethodRequired,!!t.onPaymentDataChanged,!!t.onPaymentAuthorized,t.buttonColor,t.buttonType,t.buttonLocale,t.buttonSizeMode,t.paymentRequest.merchantInfo.merchantId,t.paymentRequest.merchantInfo.merchantName,null===(e=t.paymentRequest.merchantInfo.softwareInfo)||void 0===e?void 0:e.id,null===(n=t.paymentRequest.merchantInfo.softwareInfo)||void 0===n?void 0:n.version,t.paymentRequest.allowedPaymentMethods]}}var d;function c(t,e=0){let n;return function(...o){window.clearTimeout(n);return new Promise(i=>{n=window.setTimeout(()=>{const e=(n=void 0,t(...o));i(e)},e)})}}class u extends HTMLElement{constructor(){super(...arguments),this.manager=new l({cssSelector:"google-pay-button",softwareInfoId:"@google-pay/button-element",softwareInfoVersion:"3.0.0"}),this.initializeButton=c(()=>{if(!this.assertRequiredProperty("paymentRequest"))return;if(!this.assertRequiredProperty("environment"))return;const t={paymentRequest:this.paymentRequest,environment:this.environment,existingPaymentMethodRequired:this.existingPaymentMethodRequired,onPaymentDataChanged:this.onPaymentDataChanged,onPaymentAuthorized:this.onPaymentAuthorized,buttonColor:this.buttonColor,buttonType:this.buttonType,buttonSizeMode:this.buttonSizeMode,buttonLocale:this.buttonLocale,onReadyToPayChange:t=>{this.onReadyToPayChange&&this.onReadyToPayChange(t),this.dispatch("readytopaychange",t)},onCancel:t=>{this.onCancel&&this.onCancel(t),this.dispatch("cancel",t)},onError:t=>{var e;this.onError&&(null===(e=this.onError)||void 0===e||e.call(this,t)),this.dispatchEvent(new ErrorEvent("error",{error:t}))},onLoadPaymentData:t=>{this.onLoadPaymentData&&this.onLoadPaymentData(t),this.dispatch("loadpaymentdata",t)},onClick:t=>{var e;this.onClick&&(null===(e=this.onClick)||void 0===e||e.call(this,t))}};this.manager.configure(t)})}get isReadyToPay(){return this.manager.isReadyToPay}assertRequiredProperty(t){const e=this[t];return null!=e||(this.throwError(Error("Required property not set: "+t)),!1)}throwError(t){throw t}static get observedAttributes(){return u._observedAttributes}addObservedAttribute(t){u._observedAttributes.push(t)}dispatch(t,e){this.dispatchEvent(new CustomEvent(t,{bubbles:!0,cancelable:!1,detail:e}))}connectedCallback(){return e(this,void 0,void 0,(function*(){return yield this.manager.mount(this),this.initializeButton()}))}disconnectedCallback(){this.manager.unmount()}attributeChangedCallback(t){return this.initializeButton()}notifyPropertyChanged(t){return this.initializeButton()}}return u._observedAttributes=[],t([i(),a("paymentrequest")],u.prototype,"paymentRequest",void 0),t([o()],u.prototype,"environment",void 0),t([function(t,e){const o=d||n(e);t.addObservedAttribute&&t.addObservedAttribute(o),Object.defineProperty(t,e,{get(){return this.hasAttribute(o)},set(t){t?this.setAttribute(o,""):this.removeAttribute(o),this.notifyPropertyChanged(e)},enumerable:!0,configurable:!0})},a("existingpaymentmethodrequired")],u.prototype,"existingPaymentMethodRequired",void 0),t([o(),a("buttoncolor")],u.prototype,"buttonColor",void 0),t([o(),a("buttontype")],u.prototype,"buttonType",void 0),t([o(),a("buttonsizemode")],u.prototype,"buttonSizeMode",void 0),t([o(),a("buttonlocale")],u.prototype,"buttonLocale",void 0),t([i(),a("paymentDataChangedCallback"),a("paymentdatachangedcallback"),a("onpaymentdatachanged")],u.prototype,"onPaymentDataChanged",void 0),t([i(),a("paymentAuthorizedCallback"),a("paymentauthorizedcallback"),a("onpaymentauthorized")],u.prototype,"onPaymentAuthorized",void 0),t([a("readyToPayChangeCallback"),a("readytopaychangecallback"),a("onreadytopaychange")],u.prototype,"onReadyToPayChange",void 0),t([a("loadPaymentDataCallback"),a("loadpaymentdatacallback"),a("onloadpaymentdata")],u.prototype,"onLoadPaymentData",void 0),t([a("cancelCallback"),a("cancelcallback"),a("oncancel")],u.prototype,"onCancel",void 0),t([a("errorCallback"),a("errorcallback"),a("onerror")],u.prototype,"onError",void 0),t([a("clickCallback"),a("clickcallback"),a("onclick")],u.prototype,"onClick",void 0),customElements.define("google-pay-button",u),u}));