import{r as t,h as e,g as i}from"./p-dfbe004d.js";import{_ as s}from"./p-8415f806.js";import"./p-fd56940f.js";import{a as o}from"./p-7393a06c.js";import{o as n}from"./p-c2833cb1.js";import{a as r}from"./p-1c2e2695.js";import"./p-9a7a47e8.js";const a=class{constructor(e){t(this,e)}renderLoading(){return e("sc-card",{noPadding:!0},e("sc-stacked-list",null,e("sc-stacked-list-row",{style:{"--columns":"4"},"mobile-size":500},[...Array(4)].map((()=>e("sc-skeleton",{style:{width:"100px",display:"inline-block"}}))))))}renderEmpty(){return e("slot",{name:"empty"},e("sc-card",null,e("sc-empty",{icon:"credit-card"},s("You do not have any payment methods.","surecart"))))}componentWillLoad(){n(this.el,(()=>{this.getPaymentMethods()}))}async getPaymentMethods(){var t,e,i,o;if(null===(t=this.paymentMethods)||void 0===t?void 0:t.length)return;const n=(null===(i=null===(e=this.subscription)||void 0===e?void 0:e.customer)||void 0===i?void 0:i.id)||(null===(o=this.subscription)||void 0===o?void 0:o.customer);if(n)try{this.loading=!0,this.paymentMethods=await this.fetchMethods(n)}catch(t){this.error=(null==t?void 0:t.messsage)||s("Something went wrong","surecart"),console.error(this.error)}finally{this.loading=!1}}async fetchMethods(t){var e;return await o({path:r("surecart/v1/payment_methods",{expand:["card","customer","billing_agreement","paypal_account","payment_instrument","bank_account"],customer_ids:[t],reusable:!0,live_mode:null===(e=this.subscription)||void 0===e?void 0:e.live_mode})})}async deleteMethod(t){if(confirm(s("Are you sure you want to remove this payment method?","surecart")))try{this.busy=!0,await o({path:`surecart/v1/payment_methods/${null==t?void 0:t.id}/detach`,method:"PATCH"}),this.paymentMethods=this.paymentMethods.filter((e=>e.id!==t.id))}catch(t){this.error=(null==t?void 0:t.messsage)||s("Something went wrong","surecart"),console.error(this.error)}finally{this.busy=!1}}async updateMethod(t){var e,i,n,r;const{payment_method:a}=await t.target.getFormJson();if(a!==((null===(i=null===(e=this.subscription)||void 0===e?void 0:e.payment_method)||void 0===i?void 0:i.id)||(null===(n=this.subscription)||void 0===n?void 0:n.payment_method)))try{this.busy=!0,this.subscription=await o({path:`surecart/v1/subscriptions/${null===(r=this.subscription)||void 0===r?void 0:r.id}`,method:"PATCH",data:{payment_method:a}})}catch(t){this.error=(null==t?void 0:t.messsage)||s("Something went wrong","surecart"),console.error(this.error)}finally{this.busy=!1}}renderContent(){var t,i,o;return this.loading?this.renderLoading():(null===(t=this.paymentMethods)||void 0===t?void 0:t.length)?e("sc-form",{onScSubmit:t=>this.updateMethod(t)},e("sc-choices",null,this.renderList()),(null===(i=this.paymentMethods)||void 0===i?void 0:i.length)>1?e("sc-button",{type:"primary",submit:!0,full:!0,size:"large",busy:this.busy,disabled:this.busy},s("Update Payment Method","surecart")):e("sc-button",{type:"link",full:!0,href:r(window.location.href,{action:"create",model:"payment_method",...!1===(null===(o=this.subscription)||void 0===o?void 0:o.live_mode)?{live_mode:!1}:{},success_url:window.location.href})},e("sc-icon",{name:"plus",slot:"prefix"}),s("Add New","surecart"))):this.renderEmpty()}renderList(){return this.paymentMethods.map((t=>{var i,o,n;const r=(null===(o=null===(i=this.subscription)||void 0===i?void 0:i.payment_method)||void 0===o?void 0:o.id)||(null===(n=this.subscription)||void 0===n?void 0:n.payment_method),{id:a,card:l,live_mode:d,paypal_account:c}=t;return e("sc-choice",{checked:r===a,name:"payment_method",value:a,required:!0},e("sc-flex",{justifyContent:"flex-start","align-items":"center"},e("sc-payment-method",{paymentMethod:t})," ",!d&&e("sc-tag",{type:"warning",size:"small"},s("Test","surecart"))),e("div",{slot:"description"},!!(null==l?void 0:l.exp_month)&&e("span",null,s("Exp.","surecart"),null==l?void 0:l.exp_month,"/",null==l?void 0:l.exp_year),!!c&&(null==c?void 0:c.email)),r===a&&e("sc-tag",{type:"info",slot:"price"},s("Current Payment Method","surecart")))}))}render(){var t,i;return e("sc-dashboard-module",{heading:this.heading||s("Update Payment Method","surecart"),class:"subscription",error:this.error},(null===(t=this.paymentMethods)||void 0===t?void 0:t.length)>1&&e("sc-button",{slot:"end",type:"link",href:r(window.location.href,{action:"create",model:"payment_method",...!1===(null===(i=this.subscription)||void 0===i?void 0:i.live_mode)?{live_mode:!1}:{},success_url:window.location.href})},e("sc-icon",{name:"plus",slot:"prefix"}),s("Add New","surecart")),this.renderContent(),this.busy&&e("sc-block-ui",{spinner:!0}))}get el(){return i(this)}};a.style=":host{display:block}";export{a as sc_subscription_payment_method}