import{r as t,h as s,g as i}from"./p-dfbe004d.js";import{_ as e}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(s){t(this,s),this.query={page:1,per_page:10},this.cancelBehavior="period_end",this.subscriptions=[],this.pagination={total:0,total_pages:0}}componentWillLoad(){n(this.el,(()=>{this.initialFetch()}))}async initialFetch(){try{this.loading=!0,await this.getSubscriptions()}catch(t){console.error(this.error),this.error=(null==t?void 0:t.message)||e("Something went wrong","surecart")}finally{this.loading=!1}}async fetchSubscriptions(){try{this.busy=!0,await this.getSubscriptions()}catch(t){console.error(this.error),this.error=(null==t?void 0:t.message)||e("Something went wrong","surecart")}finally{this.busy=!1}}async getSubscriptions(){if(!this.isCustomer)return;const t=await await o({path:r("surecart/v1/subscriptions/",{expand:["price","price.product","current_period","period.checkout","purchase","purchase.license","license.activations","discount","discount.coupon"],...this.query}),parse:!1});return this.pagination={total:parseInt(t.headers.get("X-WP-Total")),total_pages:parseInt(t.headers.get("X-WP-TotalPages"))},this.subscriptions=await t.json(),this.subscriptions}nextPage(){this.query.page=this.query.page+1,this.fetchSubscriptions()}prevPage(){this.query.page=this.query.page-1,this.fetchSubscriptions()}renderEmpty(){return s("div",null,s("sc-divider",{style:{"--spacing":"0"}}),s("slot",{name:"empty"},s("sc-empty",{icon:"repeat"},e("You don't have any subscriptions.","surecart"))))}renderLoading(){return s("sc-card",{"no-padding":!0,style:{"--overflow":"hidden"}},s("sc-stacked-list",null,s("sc-stacked-list-row",{style:{"--columns":"2"},"mobile-size":0},s("div",{style:{padding:"0.5em"}},s("sc-skeleton",{style:{width:"30%",marginBottom:"0.75em"}}),s("sc-skeleton",{style:{width:"20%",marginBottom:"0.75em"}}),s("sc-skeleton",{style:{width:"40%"}})))))}renderList(){return this.subscriptions.map((t=>s("sc-stacked-list-row",{href:r(window.location.href,{action:"edit",model:"subscription",id:t.id}),key:t.id,"mobile-size":0},s("sc-subscription-details",{subscription:t}),s("sc-icon",{name:"chevron-right",slot:"suffix"}))))}renderContent(){var t;return this.loading?this.renderLoading():0===(null===(t=this.subscriptions)||void 0===t?void 0:t.length)?this.renderEmpty():s("sc-card",{"no-padding":!0,style:{"--overflow":"hidden"}},s("sc-stacked-list",null,this.renderList()))}render(){var t,i;return s("sc-dashboard-module",{class:"subscriptions-list",error:this.error},s("span",{slot:"heading"},s("slot",{name:"heading"},this.heading||e("Subscriptions","surecart"))),!!this.allLink&&!!(null===(t=this.subscriptions)||void 0===t?void 0:t.length)&&s("sc-button",{type:"link",href:this.allLink,slot:"end"},e("View all","surecart"),s("sc-icon",{name:"chevron-right",slot:"suffix"})),this.renderContent(),!this.allLink&&s("sc-pagination",{page:this.query.page,perPage:this.query.per_page,total:this.pagination.total,totalPages:this.pagination.total_pages,totalShowing:null===(i=null==this?void 0:this.subscriptions)||void 0===i?void 0:i.length,onScNextPage:()=>this.nextPage(),onScPrevPage:()=>this.prevPage()}),this.busy&&s("sc-block-ui",null))}get el(){return i(this)}};a.style=":host{display:block}.subscriptions-list{display:grid;gap:0.5em}.subscriptions-list__heading{display:flex;flex-wrap:wrap;align-items:flex-end;justify-content:space-between;margin-bottom:0.5em}.subscriptions-list__title{font-size:var(--sc-font-size-x-large);font-weight:var(--sc-font-weight-bold);line-height:var(--sc-line-height-dense)}.subscriptions-list 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)}.subscriptions__title{display:none}.subscriptions--has-title-slot .subscriptions__title{display:block}";export{a as sc_subscriptions_list}