import{__,sprintf}from"@wordpress/i18n";import PaystackPop from"@paystack/inline-js";import{h,Host}from"@stencil/core";import apiFetch from"../../../functions/fetch";export class ScPaystackAddMethod{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}async handlePaymentIntentCreate(){var e,t;const{public_key:r,access_code:s}=(null===(t=null===(e=this.paymentIntent)||void 0===e?void 0:e.processor_data)||void 0===t?void 0:t.paystack)||{};if(!r||!s)return;const a=new PaystackPop;await a.newTransaction({key:r,accessCode:s,onSuccess:async e=>{if("success"!==(null==e?void 0:e.status))throw{message:sprintf(__("Paystack transaction could not be finished. Status: %s","surecart"),null==e?void 0:e.status)};window.location.assign(this.successUrl)},onClose:e=>{console.error(e),alert((null==e?void 0:e.message)||__("The payment did not process. Please try again.","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:"paystack",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:"c7834e27c130521d7944a6df4adc15bccdfdf67e"},this.error&&h("sc-alert",{key:"c0f0ff2bcf73e6e18b513275cecf6092d0b6d84d",open:!!this.error,type:"danger"},h("span",{key:"050c49e02d40ae4c6d62da7c4a67219723b6ac31",slot:"title"},__("Error","surecart")),this.error),h("div",{key:"131c553e7867a53bbbf586609df2e97066f4c7a4",class:"sc-paystack-button-container"},h("sc-alert",{key:"95f980fbf168fdc9fd528846ed0ae983eeb6c161",open:!0,type:"warning"},__("In order to add a new card, we will need to make a small transaction to authenticate it. This is for authentication purposes and will be immediately refunded.","surecart"),h("div",{key:"d8b8bdce1b6d20c48442fe755c92e07ded997893"},h("sc-button",{key:"e579bcf3a4d801c1981a8ee97450043c544b989f",loading:this.loading,type:"primary",onClick:()=>this.createPaymentIntent(),style:{marginTop:"var(--sc-spacing-medium)"}},__("Add New Card","surecart"))))))}static get is(){return"sc-paystack-add-method"}static get originalStyleUrls(){return{$:["sc-paystack-add-method.scss"]}}static get styleUrls(){return{$:["sc-paystack-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"}]}}