import{r as t,h as e}from"./p-dfbe004d.js";import{p as i}from"./p-abd9489c.js";import{_ as o}from"./p-8415f806.js";import"./p-fd56940f.js";import{a as s}from"./p-7393a06c.js";import"./p-9a7a47e8.js";import"./p-1c2e2695.js";const l=class{constructor(e){t(this,e),this.liveMode=!0}componentWillLoad(){this.createPaymentIntent()}async handlePaymentIntentCreate(){var t,e,s,l,n,r,d,a,c,h,u,m,p,v,y,f,g,w;if(!(null===(s=null===(e=null===(t=this.paymentIntent)||void 0===t?void 0:t.processor_data)||void 0===e?void 0:e.stripe)||void 0===s?void 0:s.publishable_key)||!(null===(r=null===(n=null===(l=this.paymentIntent)||void 0===l?void 0:l.processor_data)||void 0===n?void 0:n.stripe)||void 0===r?void 0:r.account_id))return;if(!this.stripe)try{this.stripe=await i.loadStripe(null===(c=null===(a=null===(d=this.paymentIntent)||void 0===d?void 0:d.processor_data)||void 0===a?void 0:a.stripe)||void 0===c?void 0:c.publishable_key,{stripeAccount:null===(m=null===(u=null===(h=this.paymentIntent)||void 0===h?void 0:h.processor_data)||void 0===u?void 0:u.stripe)||void 0===m?void 0:m.account_id})}catch(t){return void(this.error=(null==t?void 0:t.message)||o("Stripe could not be loaded","surecart"))}if(!(null===(y=null===(v=null===(p=this.paymentIntent)||void 0===p?void 0:p.processor_data)||void 0===v?void 0:v.stripe)||void 0===y?void 0:y.client_secret)||!this.container)return void console.warn("do not have client secret or container");const _=getComputedStyle(document.body);this.elements=this.stripe.elements({clientSecret:null===(w=null===(g=null===(f=this.paymentIntent)||void 0===f?void 0:f.processor_data)||void 0===g?void 0:g.stripe)||void 0===w?void 0:w.client_secret,appearance:{variables:{colorPrimary:_.getPropertyValue("--sc-color-primary-500"),colorText:_.getPropertyValue("--sc-input-label-color"),borderRadius:_.getPropertyValue("--sc-input-border-radius-medium"),colorBackground:_.getPropertyValue("--sc-input-background-color"),fontSizeBase:_.getPropertyValue("--sc-input-font-size-medium")},rules:{".Input":{border:_.getPropertyValue("--sc-input-border")},".Input::placeholder":{color:_.getPropertyValue("--sc-input-placeholder-color")}}}}),this.elements.create("payment",{wallets:{applePay:"never",googlePay:"never"}}).mount(".sc-payment-element-container"),this.element=this.elements.getElement("payment"),this.element.on("ready",(()=>this.loaded=!0))}async createPaymentIntent(){try{this.loading=!0,this.error="",this.paymentIntent=await s({method:"POST",path:"surecart/v1/payment_intents",data:{processor_type:"stripe",live_mode:this.liveMode,customer_id:this.customerId}})}catch(t){this.error=(null==t?void 0:t.message)||o("Something went wrong","surecart")}finally{this.loading=!1}}async handleSubmit(t){t.preventDefault(),this.loading=!0;try{const t=await this.stripe.confirmSetup({elements:this.elements,confirmParams:{return_url:this.successUrl},redirect:"always"});if(null==t?void 0:t.error)throw this.error=t.error.message,t.error}catch(t){console.error(t),this.error=(null==t?void 0:t.message)||o("Something went wrong","surecart"),this.loading=!1}}render(){return e("sc-form",{onScFormSubmit:t=>this.handleSubmit(t)},this.error&&e("sc-alert",{open:!!this.error,type:"danger"},e("span",{slot:"title"},o("Error","surecart")),this.error),e("div",{class:"loader",hidden:this.loaded},e("div",{class:"loader__row"},e("div",{style:{width:"50%"}},e("sc-skeleton",{style:{width:"50%",marginBottom:"0.5em"}}),e("sc-skeleton",null)),e("div",{style:{flex:"1"}},e("sc-skeleton",{style:{width:"50%",marginBottom:"0.5em"}}),e("sc-skeleton",null)),e("div",{style:{flex:"1"}},e("sc-skeleton",{style:{width:"50%",marginBottom:"0.5em"}}),e("sc-skeleton",null))),e("div",{class:"loader__details"},e("sc-skeleton",{style:{height:"1rem"}}),e("sc-skeleton",{style:{height:"1rem",width:"30%"}}))),e("div",{hidden:!this.loaded,class:"sc-payment-element-container",ref:t=>this.container=t}),e("sc-button",{type:"primary",submit:!0,full:!0,loading:this.loading},o("Save Payment Method","surecart")))}static get watchers(){return{paymentIntent:["handlePaymentIntentCreate"]}}};l.style="sc-stripe-add-method{display:block}sc-stripe-add-method [hidden]{display:none}.loader{display:grid;height:128px;gap:2em}.loader__row{display:flex;align-items:flex-start;justify-content:space-between;gap:1em}.loader__details{display:grid;gap:0.5em}";export{l as sc_stripe_add_method}