import{h,Host}from"@stencil/core";import{loadScript}from"@paypal/paypal-js";import{__}from"@wordpress/i18n";import apiFetch from"../../../functions/fetch";import{getScriptLoadParams}from"../paypal-buttons/functions";export class ScPaypalAddMethod{constructor(){this.liveMode=!0,this.customerId=void 0,this.successUrl=void 0,this.currency=void 0,this.loading=void 0,this.loaded=void 0,this.error=void 0,this.paymentIntent=void 0}componentWillLoad(){this.createPaymentIntent()}async handlePaymentIntentCreate(){var e,t,r;const{external_intent_id:a}=this.paymentIntent||{},{client_id:i,account_id:s,merchant_initiated:o}=(null===(t=null===(e=this.paymentIntent)||void 0===e?void 0:e.processor_data)||void 0===t?void 0:t.paypal)||{};if(i&&s&&a&&!this.paypal)try{this.paypal=await loadScript(getScriptLoadParams({clientId:i,merchantId:s,merchantInitiated:o,reusable:!0,locale:null===(r=window.scData)||void 0===r?void 0:r.locale})),this.paypal.Buttons({onInit:()=>{this.loaded=!0},createBillingAgreement:()=>new Promise((e=>e(a))),onApprove:async()=>{var e;try{this.loading=!0;const t=await apiFetch({method:"PATCH",path:`surecart/v1/payment_intents/${null===(e=this.paymentIntent)||void 0===e?void 0:e.id}/capture`});if(!["succeeded","pending","requires_approval"].includes(null==t?void 0:t.status))throw{message:__("The payment did not process. Please try again.","surecart")};window.location.assign(this.successUrl)}catch(e){console.error(e),this.error=(null==e?void 0:e.message)||__("The payment did not process. Please try again.","surecart"),this.loading=!1}},onError:e=>{console.error(e),alert((null==e?void 0:e.message)||__("The payment did not process. Please try again.","surecart"))}}).render(this.container)}catch(e){console.error("Failed to load the PayPal JS SDK script",e),this.error=__("Failed to load the PayPal JS SDK script","surecart")}}async createPaymentIntent(){var e,t;try{this.loading=!0,this.error="",this.paymentIntent=await apiFetch({method:"POST",path:"surecart/v1/payment_intents",data:{processor_type:"paypal",reusable:!0,live_mode:this.liveMode,customer_id:this.customerId,currency:this.currency,refresh_status:!0}})}catch(r){this.error=(null===(t=null===(e=null==r?void 0:r.additional_errors)||void 0===e?void 0:e[0])||void 0===t?void 0:t.message)||(null==r?void 0:r.message)||__("Something went wrong","surecart")}finally{this.loading=!1}}render(){return h(Host,{key:"f1ab046ac184127426e53c7dff336a2c2fc05775"},this.error&&h("sc-alert",{key:"14f25f7ca714fa2ace2a5838ab8a8934be3e2dc6",open:!!this.error,type:"danger"},h("span",{key:"87015bb886c5571795e64af5a14f61e5d244071b",slot:"title"},__("Error","surecart")),this.error),h("div",{key:"940a2ebac3d5b6deaba844494a77f6ec30b418ce",class:"sc-paypal-button-container",hidden:!this.loaded,ref:e=>this.container=e}))}static get is(){return"sc-paypal-add-method"}static get originalStyleUrls(){return{$:["sc-paypal-add-method.scss"]}}static get styleUrls(){return{$:["sc-paypal-add-method.css"]}}static get properties(){return{liveMode:{type:"boolean",mutable:!1,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"live-mode",reflect:!1,defaultValue:"true"},customerId:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"customer-id",reflect:!1},successUrl:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"success-url",reflect:!1},currency:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"currency",reflect:!1}}}static get states(){return{loading:{},loaded:{},error:{},paymentIntent:{}}}static get watchers(){return[{propName:"paymentIntent",methodName:"handlePaymentIntentCreate"}]}}