import{__}from"@wordpress/i18n";import{h,Host}from"@stencil/core";import apiFetch from"../../../functions/fetch";import{loadRazorpay}from"../../../functions/razorpay";export class ScRazorpayAddMethod{constructor(){this.razorpayInstance=null,this.confirming=!1,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(){if(this.confirming)return;const{external_intent_id:e,processor_data:t}=this.paymentIntent||{},{public_key:r,customer_id:a}=(null==t?void 0:t.razorpay)||{};if(r&&e){this.confirming=!0;try{this.razorpayInstance||(this.razorpayInstance=await loadRazorpay());const t={key:r,order_id:e,customer_id:a,recurring:!0,handler:async e=>{(null==e?void 0:e.razorpay_payment_id)?window.location.assign(this.successUrl):(this.error=__("Payment verification failed. Please contact support.","surecart"),this.loading=!1)},modal:{ondismiss:()=>{this.loading=!1}}},i=new this.razorpayInstance(t);i.on("payment.failed",(e=>{var t;this.error=(null===(t=null==e?void 0:e.error)||void 0===t?void 0:t.description)||__("Payment failed. Please try again.","surecart"),this.loading=!1,console.error("payment.failed",e)})),i.open()}catch(e){this.error=(null==e?void 0:e.message)||__("Something went wrong","surecart"),this.loading=!1,console.error(e)}finally{this.confirming=!1}}}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:"razorpay",reusable:!0,live_mode:this.liveMode,customer_id:this.customerId,currency:this.currency,refresh_status:!0}})}catch(r){console.error(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"),this.loading=!1}}render(){return h(Host,{key:"c797b31b9a7625ab18a85a84043cd3d08d2502c5"},this.error&&h("sc-alert",{key:"188d3fc4f2d09898b51c09a1fe6937a375208c5a",open:!!this.error,type:"danger"},h("span",{key:"f5c32c2b5dd77b97a10a382be95dc2cfba6d6e7d",slot:"title"},__("Error","surecart")),this.error),h("div",{key:"d03cd5a52a87e3619356f720c671a32c90a4c177",class:"sc-razorpay-button-container"},h("sc-alert",{key:"7e381208deab7bb0fb2d53d77bd3ba66526095a8",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:"d5bdd1c1e62773c4111a0834a65b8f9988a96e62"},h("sc-button",{key:"d1fd503f1297bc40db29896c3051471a67a613f6",loading:this.loading,type:"primary",onClick:()=>this.createPaymentIntent(),style:{marginTop:"var(--sc-spacing-medium)"}},__("Add New Card","surecart"))))))}static get is(){return"sc-razorpay-add-method"}static get originalStyleUrls(){return{$:["sc-razorpay-add-method.scss"]}}static get styleUrls(){return{$:["sc-razorpay-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"}]}}