import { proxyCustomElement, HTMLElement, h } from '@stencil/core/internal/client';
import { _ as __, s as sprintf, a as _n } from './default-i18n.js';
import './index2.js';
import { a as apiFetch } from './fetch.js';
import { o as onFirstVisible } from './lazy.js';
import { d as defineCustomElement$k } from './sc-alert2.js';
import { d as defineCustomElement$j } from './sc-block-ui2.js';
import { d as defineCustomElement$i } from './sc-button2.js';
import { d as defineCustomElement$h } from './sc-card2.js';
import { d as defineCustomElement$g } from './sc-dashboard-module2.js';
import { d as defineCustomElement$f } from './sc-divider2.js';
import { d as defineCustomElement$e } from './sc-empty2.js';
import { d as defineCustomElement$d } from './sc-flex2.js';
import { d as defineCustomElement$c } from './sc-format-date2.js';
import { d as defineCustomElement$b } from './sc-format-number2.js';
import { d as defineCustomElement$a } from './sc-icon2.js';
import { d as defineCustomElement$9 } from './sc-order-status-badge2.js';
import { d as defineCustomElement$8 } from './sc-pagination2.js';
import { d as defineCustomElement$7 } from './sc-skeleton2.js';
import { d as defineCustomElement$6 } from './sc-spinner2.js';
import { d as defineCustomElement$5 } from './sc-stacked-list2.js';
import { d as defineCustomElement$4 } from './sc-stacked-list-row2.js';
import { d as defineCustomElement$3 } from './sc-tag2.js';
import { d as defineCustomElement$2 } from './sc-text2.js';
import { a as addQueryArgs } from './add-query-args.js';

const scInvoicesListCss = ":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)}";

const ScInvoicesList$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    /** Query to fetch invoices */
    this.query = {
      page: 1,
      per_page: 10,
    };
    this.invoices = [];
    this.pagination = {
      total: 0,
      total_pages: 0,
    };
  }
  /** Only fetch if visible */
  componentWillLoad() {
    onFirstVisible(this.el, () => {
      this.initialFetch();
    });
  }
  async initialFetch() {
    try {
      this.loading = true;
      await this.getItems();
    }
    catch (e) {
      console.error(this.error);
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.loading = false;
    }
  }
  async fetchItems() {
    try {
      this.busy = true;
      await this.getItems();
    }
    catch (e) {
      console.error(this.error);
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.busy = false;
    }
  }
  /** Get all orders */
  async getItems() {
    const response = (await await apiFetch({
      path: addQueryArgs(`surecart/v1/invoices/`, {
        expand: ['invoice_items', 'charge'],
        ...this.query,
      }),
      parse: false,
    }));
    this.pagination = {
      total: parseInt(response.headers.get('X-WP-Total')),
      total_pages: parseInt(response.headers.get('X-WP-TotalPages')),
    };
    this.invoices = (await response.json());
    return this.invoices;
  }
  nextPage() {
    this.query.page = this.query.page + 1;
    this.fetchItems();
  }
  prevPage() {
    this.query.page = this.query.page - 1;
    this.fetchItems();
  }
  renderStatusBadge(invoice) {
    const { status, charge } = invoice;
    if (typeof charge === 'object') {
      if (charge === null || charge === void 0 ? void 0 : charge.fully_refunded) {
        return h("sc-tag", { type: "danger" }, __('Refunded', 'surecart'));
      }
      if (charge === null || charge === void 0 ? void 0 : charge.refunded_amount) {
        return h("sc-tag", { type: "info" }, __('Partially Refunded', 'surecart'));
      }
    }
    return h("sc-order-status-badge", { status: status });
  }
  renderLoading() {
    return (h("sc-card", { noPadding: true }, h("sc-stacked-list", null, h("sc-stacked-list-row", { style: { '--columns': '4' }, "mobile-size": 500 }, [...Array(4)].map(() => (h("sc-skeleton", { style: { width: '100px', display: 'inline-block' } })))))));
  }
  renderEmpty() {
    return (h("div", null, h("sc-divider", { style: { '--spacing': '0' } }), h("slot", { name: "empty" }, h("sc-empty", { icon: "tag" }, __("You don't have any invoices.", 'surecart')))));
  }
  renderList() {
    return this.invoices.map(invoice => {
      var _a, _b;
      const { invoice_items, total_amount, currency, created_at, url } = invoice;
      return (h("sc-stacked-list-row", { href: url, style: { '--columns': '4' }, "mobile-size": 500 }, h("div", null, h("sc-format-date", { class: "order__date", date: created_at, type: "timestamp", month: "short", day: "numeric", year: "numeric" })), h("div", null, h("sc-text", { truncate: true, style: {
          '--color': 'var(--sc-color-gray-500)',
        } }, sprintf(_n('%s item', '%s items', ((_a = invoice_items === null || invoice_items === void 0 ? void 0 : invoice_items.pagination) === null || _a === void 0 ? void 0 : _a.count) || 0, 'surecart'), ((_b = invoice_items === null || invoice_items === void 0 ? void 0 : invoice_items.pagination) === null || _b === void 0 ? void 0 : _b.count) || 0))), h("div", null, this.renderStatusBadge(invoice)), h("div", null, h("sc-format-number", { type: "currency", currency: currency, value: total_amount }))));
    });
  }
  renderContent() {
    var _a;
    if (this.loading) {
      return this.renderLoading();
    }
    if (((_a = this.invoices) === null || _a === void 0 ? void 0 : _a.length) === 0) {
      return this.renderEmpty();
    }
    return (h("sc-card", { "no-padding": true }, h("sc-stacked-list", null, this.renderList())));
  }
  render() {
    var _a, _b;
    return (h("sc-dashboard-module", { class: "invoices-list", error: this.error }, h("span", { slot: "heading" }, h("slot", { name: "heading" }, this.heading || __('Invoice History', 'surecart'))), !!this.allLink && !!((_a = this.invoices) === null || _a === void 0 ? void 0 : _a.length) && (h("sc-button", { type: "link", href: this.allLink, slot: "end" }, __('View all', 'surecart'), h("sc-icon", { name: "chevron-right", slot: "suffix" }))), this.renderContent(), !this.allLink && (h("sc-pagination", { page: this.query.page, perPage: this.query.per_page, total: this.pagination.total, totalPages: this.pagination.total_pages, totalShowing: (_b = this === null || this === void 0 ? void 0 : this.invoices) === null || _b === void 0 ? void 0 : _b.length, onScNextPage: () => this.nextPage(), onScPrevPage: () => this.prevPage() })), this.busy && h("sc-block-ui", null)));
  }
  get el() { return this; }
  static get style() { return scInvoicesListCss; }
}, [1, "sc-invoices-list", {
    "query": [1040],
    "allLink": [1, "all-link"],
    "heading": [1],
    "invoices": [32],
    "loading": [32],
    "busy": [32],
    "error": [32],
    "pagination": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-invoices-list", "sc-alert", "sc-block-ui", "sc-button", "sc-card", "sc-dashboard-module", "sc-divider", "sc-empty", "sc-flex", "sc-format-date", "sc-format-number", "sc-icon", "sc-order-status-badge", "sc-pagination", "sc-skeleton", "sc-spinner", "sc-stacked-list", "sc-stacked-list-row", "sc-tag", "sc-text"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-invoices-list":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScInvoicesList$1);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$k();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$j();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$i();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$h();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "sc-divider":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-empty":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-format-date":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-format-number":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-order-status-badge":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-pagination":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-stacked-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-stacked-list-row":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-tag":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-text":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScInvoicesList = ScInvoicesList$1;
const defineCustomElement = defineCustomElement$1;

export { ScInvoicesList, defineCustomElement };
