import{r as t,h as s,F as i}from"./p-b891c2f1.js";import{_ as e}from"./p-684c85b4.js";import"./p-aafd8ea4.js";import{a as o}from"./p-6fd3642a.js";import{a as n}from"./p-1c2e2695.js";import"./p-112455b1.js";const l=class{constructor(s){t(this,s),this.paymentMethods=[],this.customerIds=[]}componentWillLoad(){this.fetchItems()}async fetchItems(){try{this.loading=!0,await Promise.all([this.fetchSubscription(),this.fetchPaymentMethods()])}catch(t){console.error(t),this.error=(null==t?void 0:t.message)||e("Something went wrong","surecart")}finally{this.loading=!1}}async fetchSubscription(){this.subscriptionId&&(this.subscription=await o({path:n(`/surecart/v1/subscriptions/${this.subscriptionId}`,{expand:["price","price.product","latest_invoice","product"]})}))}async fetchPaymentMethods(){var t;this.paymentMethods=await o({path:n("/surecart/v1/payment_methods",{expand:["card"],customer_ids:this.customerIds,...null!==(null===(t=this.subscription)||void 0===t?void 0:t.live_mode)?{live_mode:this.subscription.live_mode}:{}})})}async handleSubmit(t){var s;const{method:i}=await t.target.getFormJson();try{this.error="",this.busy=!0,await o({path:`/surecart/v1/subscriptions/${null===(s=this.subscription)||void 0===s?void 0:s.id}`,method:"PATCH",data:{payment_method:i}}),this.successUrl?window.location.assign(this.successUrl):this.busy=!1}catch(t){this.error=(null==t?void 0:t.message)||e("Something went wrong","surecart"),this.busy=!1}}renderLoading(){return s(i,null,s("sc-choice",{name:"loading",disabled:!0},s("sc-skeleton",{style:{width:"60px",display:"inline-block"}}),s("sc-skeleton",{style:{width:"80px",display:"inline-block"},slot:"price"}),s("sc-skeleton",{style:{width:"120px",display:"inline-block"},slot:"description"})),s("sc-button",{type:"primary",full:!0,submit:!0,loading:!0,busy:!0}),!!this.backUrl&&s("sc-button",{href:this.backUrl,full:!0,loading:!0,busy:!0}))}renderContent(){if(this.loading)return this.renderLoading();const t=this.paymentMethods.filter((t=>{var s;return(null==t?void 0:t.live_mode)===(null===(s=this.subscription)||void 0===s?void 0:s.live_mode)}));return(null==t?void 0:t.length)?s(i,null,s("sc-choices",null,s("div",null,(this.paymentMethods||[]).map((({id:t,card:i,live_mode:o})=>{var n,l;return o!==(null===(n=null==this?void 0:this.subscription)||void 0===n?void 0:n.live_mode)?null:s("sc-choice",{checked:(null===(l=this.subscription)||void 0===l?void 0:l.payment_method)===t,name:"method",value:t},s("sc-flex",{"justify-content":"flex-start","align-items":"center",style:{"--spacing":"0.5em"}},s("sc-cc-logo",{style:{fontSize:"36px"},brand:null==i?void 0:i.brand}),s("span",{style:{fontSize:"7px",whiteSpace:"nowrap"}},"⬤"," ","⬤"," ","⬤"," ","⬤"),s("span",null,null==i?void 0:i.last4)),s("span",{slot:"per"},e("Expires","surecart")," ",null==i?void 0:i.exp_month,"/",null==i?void 0:i.exp_year))})))),s("sc-button",{type:"primary",full:!0,submit:!0,loading:this.loading||this.busy,disabled:this.loading||this.busy},e("Update","surecart")),!!this.backUrl&&s("sc-button",{href:this.backUrl,full:!0,loading:this.loading||this.busy,disabled:this.loading||this.busy},e("Go Back","surecart"))):s(i,null,s("sc-empty",{icon:"credit-card"},e("You have no saved payment methods.","surecart")),!!this.backUrl&&s("sc-button",{href:this.backUrl,full:!0},e("Go Back","surecart")))}render(){return s("sc-dashboard-module",{heading:e("Select a payment method","surecart"),class:"subscription-payment",error:this.error},s("sc-form",{onScFormSubmit:t=>this.handleSubmit(t)},s("sc-card",null,this.renderContent())),this.busy&&s("sc-block-ui",null))}};l.style=":host{display:block;position:relative}.subscription-payment{display:grid;gap:0.5em}";export{l as sc_subscription_payment}