import{r as t,h as e,g as s}from"./p-b891c2f1.js";import{_ as i}from"./p-684c85b4.js";import"./p-aafd8ea4.js";import{a as n}from"./p-6fd3642a.js";import{o as a}from"./p-c2833cb1.js";import{a as o}from"./p-1c2e2695.js";import"./p-112455b1.js";const l=class{constructor(e){t(this,e),this.paymentMethods=[]}componentWillLoad(){a(this.el,(()=>{this.getPaymentMethods()})),this.handleSlotChange()}handleSlotChange(){this.hasTitleSlot=!!this.el.querySelector('[slot="title"]')}async deleteMethod(t){if(confirm(i("Are you sure you want to remove this payment method?","surecart")))try{this.busy=!0,await n({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){alert((null==t?void 0:t.messsage)||i("Something went wrong","surecart"))}finally{this.busy=!1}}async setDefault(t){var e;try{this.busy=!0,await n({path:`surecart/v1/customers/${null===(e=null==t?void 0:t.customer)||void 0===e?void 0:e.id}`,method:"PATCH",data:{default_payment_method:t.id}}),this.paymentMethods=await await n({path:o("surecart/v1/payment_methods/",{expand:["card","customer","billing_agreement"],...this.query})})}catch(t){alert((null==t?void 0:t.messsage)||i("Something went wrong","surecart"))}finally{this.busy=!1}}async getPaymentMethods(){try{this.loading=!0,this.paymentMethods=await await n({path:o("surecart/v1/payment_methods/",{expand:["card","customer","billing_agreement"],...this.query})})}catch(t){this.error=(null==t?void 0:t.message)?t.message:i("Something went wrong","surecart"),console.error(this.error)}finally{this.loading=!1}}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("div",null,e("sc-divider",{style:{"--spacing":"0"}}),e("slot",{name:"empty"},e("sc-empty",{icon:"credit-card"},i("You don't have any saved payment methods.","surecart"))))}renderList(){return this.paymentMethods.map((t=>{var s,n;const{id:a,card:o,customer:l,live_mode:r,billing_agreement:c}=t;return e("sc-stacked-list-row",{style:{"--columns":c?"2":"3"}},e("sc-flex",{"justify-content":"flex-start","align-items":"center",style:{"--spacing":"0.5em"}},e("div",{style:{width:"50px",lineHeight:"0"}},!!(null==o?void 0:o.brand)&&e("sc-cc-logo",{style:{fontSize:"36px"},brand:null==o?void 0:o.brand}),c&&e("sc-icon",{style:{width:"50px"},name:"paypal"})),!!(null==o?void 0:o.last4)&&e("sc-flex",{"justify-content":"flex-start","align-items":"center",style:{"--spacing":"0.5em"}},e("span",{style:{fontSize:"7px",whiteSpace:"nowrap"}},"⬤"," ","⬤"," ","⬤"," ","⬤"),e("span",null,null==o?void 0:o.last4)),!!(null===(s=c)||void 0===s?void 0:s.email)&&e("span",{style:{"--spacing":"0.5em",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"}},null===(n=c)||void 0===n?void 0:n.email)),!!(null==o?void 0:o.exp_month)&&e("div",null,i("Exp.","surecart")," ",null==o?void 0:o.exp_month,"/",null==o?void 0:o.exp_year),e("sc-flex",{"justify-content":"flex-start","align-items":"center",style:{"--spacing":"0.5em",marginLeft:"auto"}},"string"!=typeof l&&(null==l?void 0:l.default_payment_method)===a&&e("sc-tag",{type:"info"},i("Default","surecart")),!r&&e("sc-tag",{type:"warning"},i("Test","surecart"))),e("sc-dropdown",{placement:"bottom-end",slot:"suffix"},e("sc-icon",{name:"more-horizontal",slot:"trigger"}),e("sc-menu",null,"string"!=typeof l&&(null==l?void 0:l.default_payment_method)!==a&&e("sc-menu-item",{onClick:()=>this.setDefault(t)},i("Make Default","surecart")),e("sc-menu-item",{onClick:()=>this.deleteMethod(t)},i("Delete","surecart")))))}))}renderContent(){var t;return this.loading?this.renderLoading():0===(null===(t=this.paymentMethods)||void 0===t?void 0:t.length)?this.renderEmpty():e("sc-card",{"no-padding":!0},e("sc-stacked-list",null,this.renderList()))}render(){return e("sc-dashboard-module",{class:"payment-methods-list",error:this.error},e("span",{slot:"heading"},e("slot",{name:"heading"},this.heading||i("Payment Methods","surecart"))),e("sc-flex",{slot:"end"},e("sc-button",{type:"link",href:o(window.location.href,{action:"index",model:"charge"})},e("sc-icon",{name:"clock",slot:"prefix"}),i("Payment History","surecart")),e("sc-button",{type:"link",href:o(window.location.href,{action:"create",model:"payment_method"})},e("sc-icon",{name:"plus",slot:"prefix"}),i("Add","surecart"))),this.renderContent(),this.busy&&e("sc-block-ui",{spinner:!0}))}get el(){return s(this)}};l.style=":host{display:block;position:relative}.payment-methods-list{display:grid;gap:0.5em}.payment-methods-list sc-heading a{text-decoration:none;font-weight:var(--sc-font-weight-semibold);display:inline-flex;align-items:center;gap:0.25em;color:var(--sc-color-primary-500)}";export{l as sc_payment_methods_list}