import{r as t,h as e,g as s}from"./p-dfbe004d.js";import{_ as r,s as i,a}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 l}from"./p-1c2e2695.js";import"./p-9a7a47e8.js";const c=class{constructor(e){t(this,e),this.query={page:1,per_page:10},this.orders=[],this.pagination={total:0,total_pages:0}}componentWillLoad(){n(this.el,(()=>{this.initialFetch()}))}async initialFetch(){try{this.loading=!0,await this.getOrders()}catch(t){console.error(this.error),this.error=(null==t?void 0:t.message)||r("Something went wrong","surecart")}finally{this.loading=!1}}async fetchOrders(){try{this.busy=!0,await this.getOrders()}catch(t){console.error(this.error),this.error=(null==t?void 0:t.message)||r("Something went wrong","surecart")}finally{this.busy=!1}}async getOrders(){if(!this.isCustomer)return;const t=await await o({path:l("surecart/v1/orders/",{expand:["checkout","checkout.line_items","checkout.charge"],...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.orders=await t.json(),this.orders}nextPage(){this.query.page=this.query.page+1,this.fetchOrders()}prevPage(){this.query.page=this.query.page-1,this.fetchOrders()}renderStatusBadge(t){const{status:s,checkout:i}=t,{charge:a}=i;if(a&&"object"==typeof a){if(null==a?void 0:a.fully_refunded)return e("sc-tag",{type:"danger"},r("Refunded","surecart"));if(null==a?void 0:a.refunded_amount)return e("sc-tag",{type:"info"},r("Partially Refunded","surecart"))}return e("sc-order-status-badge",{status:s})}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:"shopping-bag"},r("You don't have any orders.","surecart"))))}renderList(){return this.orders.map((t=>{var s,r;const{checkout:o,created_at:n,id:c}=t;if(!o)return null;const{line_items:d,amount_due:h,currency:u,charge:g}=o;return e("sc-stacked-list-row",{href:l(window.location.href,{action:"show",model:"order",id:c}),style:{"--columns":"4"},"mobile-size":500},e("div",null,"string"!=typeof g&&e("sc-format-date",{class:"order__date",date:1e3*((null==g?void 0:g.created_at)||n),month:"short",day:"numeric",year:"numeric"})),e("div",null,e("sc-text",{truncate:!0,style:{"--color":"var(--sc-color-gray-500)"}},i(a("%s item","%s items",(null===(s=null==d?void 0:d.pagination)||void 0===s?void 0:s.count)||0,"surecart"),(null===(r=null==d?void 0:d.pagination)||void 0===r?void 0:r.count)||0))),e("div",null,this.renderStatusBadge(t)),e("div",null,e("sc-format-number",{type:"currency",currency:u,value:h})))}))}renderContent(){var t;return this.loading?this.renderLoading():0===(null===(t=this.orders)||void 0===t?void 0:t.length)?this.renderEmpty():e("sc-card",{"no-padding":!0},e("sc-stacked-list",null,this.renderList()))}render(){var t,s;return e("sc-dashboard-module",{class:"orders-list",error:this.error},e("span",{slot:"heading"},e("slot",{name:"heading"},this.heading||r("Order History","surecart"))),!!this.allLink&&!!(null===(t=this.orders)||void 0===t?void 0:t.length)&&e("sc-button",{type:"link",href:this.allLink,slot:"end"},r("View all","surecart"),e("sc-icon",{name:"chevron-right",slot:"suffix"})),this.renderContent(),!this.allLink&&e("sc-pagination",{page:this.query.page,perPage:this.query.per_page,total:this.pagination.total,totalPages:this.pagination.total_pages,totalShowing:null===(s=null==this?void 0:this.orders)||void 0===s?void 0:s.length,onScNextPage:()=>this.nextPage(),onScPrevPage:()=>this.prevPage()}),this.busy&&e("sc-block-ui",null))}get el(){return s(this)}};c.style=":host{display:block}.orders-list{display:grid;gap:0.75em}.orders-list__heading{display:flex;flex-wrap:wrap;align-items:flex-end;justify-content:space-between}.orders-list__title{font-size:var(--sc-font-size-x-large);font-weight:var(--sc-font-weight-bold);line-height:var(--sc-line-height-dense)}.orders-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)}.order__row{color:var(--sc-color-gray-800);text-decoration:none;display:grid;align-items:center;justify-content:space-between;gap:0;grid-template-columns:1fr 1fr 1fr auto;margin:0;padding:var(--sc-spacing-small) var(--sc-spacing-large)}.order__row:not(:last-child){border-bottom:1px solid var(--sc-color-gray-200)}.order__row:hover{background:var(--sc-color-gray-50)}.order__date{font-weight:var(--sc-font-weight-semibold)}";export{c as sc_orders_list}