import{h}from"@stencil/core";import{sprintf,__}from"@wordpress/i18n";import{addQueryArgs}from"@wordpress/url";import apiFetch from"../../../functions/fetch";export class ScMollieAddMethod{constructor(){this.country=void 0,this.successUrl=void 0,this.processorId=void 0,this.currency=void 0,this.liveMode=void 0,this.customerId=void 0,this.methods=[],this.loading=void 0,this.error=void 0,this.selectedMethodId=void 0,this.paymentIntent=void 0}componentWillLoad(){this.fetchMethods()}async createPaymentIntent(){var e,t,s,o;try{this.loading=!0,this.error="",this.paymentIntent=await apiFetch({method:"POST",path:"surecart/v1/payment_intents",data:{processor_type:"mollie",live_mode:this.liveMode,customer_id:this.customerId,return_url:this.successUrl,payment_method_type:this.selectedMethodId,currency:this.currency,refresh_status:!0}}),(null===(t=null===(e=this.paymentIntent.processor_data)||void 0===e?void 0:e.mollie)||void 0===t?void 0:t.checkout_url)&&window.location.assign(null===(o=null===(s=this.paymentIntent.processor_data)||void 0===s?void 0:s.mollie)||void 0===o?void 0:o.checkout_url)}catch(e){console.error(e),this.error=(null==e?void 0:e.message)||__("Something went wrong","surecart"),this.loading=!1}}async fetchMethods(){var e,t,s;try{this.loading=!0;const o=await apiFetch({path:addQueryArgs(`surecart/v1/processors/${this.processorId}/payment_method_types`,{amount:2500,country:this.country,currency:this.currency,reusable:!0})});this.methods=(null==o?void 0:o.data)||[],(null===(e=this.methods)||void 0===e?void 0:e.length)&&(this.selectedMethodId=null===(s=null===(t=this.methods)||void 0===t?void 0:t[0])||void 0===s?void 0:s.id)}catch(e){this.error=(null==e?void 0:e.message)||__("Something went wrong","surecart"),console.error(e)}finally{this.loading=!1}}handleSubmit(){this.createPaymentIntent()}renderLoading(){return h("sc-card",null,h("sc-skeleton",{style:{width:"50%",marginBottom:"0.5em"}}),h("sc-skeleton",{style:{width:"30%",marginBottom:"0.5em"}}),h("sc-skeleton",{style:{width:"60%",marginBottom:"0.5em"}}))}render(){var e;return this.loading&&!(null===(e=this.methods)||void 0===e?void 0:e.length)?this.renderLoading():h("sc-form",{onScFormSubmit:()=>this.handleSubmit(),style:{position:"relative"}},h("sc-toggles",{collapsible:!1,theme:"container"},(this.methods||[]).map((e=>h("sc-toggle",{"show-control":!0,shady:!0,borderless:!0,open:this.selectedMethodId===(null==e?void 0:e.id),onScShow:()=>this.selectedMethodId=null==e?void 0:e.id},h("span",{slot:"summary",class:"sc-payment-toggle-summary"},!!(null==e?void 0:e.image)&&h("img",{src:null==e?void 0:e.image}),h("span",null,null==e?void 0:e.description)),h("sc-card",null,h("sc-payment-selected",{label:sprintf(__("%s selected.","surecart"),null==e?void 0:e.description)},!!(null==e?void 0:e.image)&&h("img",{slot:"icon",src:null==e?void 0:e.image,style:{width:"32px"}}),__("Another step will appear after submitting your order to add this payment method.","surecart"))))))),h("sc-button",{type:"primary",submit:!0,full:!0,loading:this.loading},__("Add Payment Method","surecart")),this.loading&&h("sc-block-ui",{"z-index":9,style:{"--sc-block-ui-opacity":"0.75"}}))}static get is(){return"sc-mollie-add-method"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-mollie-add-method.css"]}}static get styleUrls(){return{$:["sc-mollie-add-method.css"]}}static get properties(){return{country:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"country",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},processorId:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"processor-id",reflect:!1},currency:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"currency",reflect:!1},liveMode:{type:"boolean",mutable:!1,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"live-mode",reflect:!1},customerId:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"customer-id",reflect:!1}}}static get states(){return{methods:{},loading:{},error:{},selectedMethodId:{},paymentIntent:{}}}}