import { proxyCustomElement, HTMLElement, h } from '@stencil/core/internal/client';
import { _ as __ } 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$i } from './sc-alert2.js';
import { d as defineCustomElement$h } from './sc-block-ui2.js';
import { d as defineCustomElement$g } from './sc-button2.js';
import { d as defineCustomElement$f } from './sc-card2.js';
import { d as defineCustomElement$e } from './sc-dashboard-module2.js';
import { d as defineCustomElement$d } from './sc-divider2.js';
import { d as defineCustomElement$c } from './sc-downloads-list2.js';
import { d as defineCustomElement$b } from './sc-empty2.js';
import { d as defineCustomElement$a } from './sc-flex2.js';
import { d as defineCustomElement$9 } from './sc-format-bytes2.js';
import { d as defineCustomElement$8 } from './sc-icon2.js';
import { d as defineCustomElement$7 } from './sc-pagination2.js';
import { d as defineCustomElement$6 } from './sc-skeleton2.js';
import { d as defineCustomElement$5 } from './sc-spacing2.js';
import { d as defineCustomElement$4 } from './sc-spinner2.js';
import { d as defineCustomElement$3 } from './sc-stacked-list2.js';
import { d as defineCustomElement$2 } from './sc-stacked-list-row2.js';
import { a as addQueryArgs } from './add-query-args.js';

const scDashboardDownloadsListCss = ":host{display:block}.download__details{opacity:0.75}";

const ScDownloadsList = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    /** Customer id to fetch subscriptions */
    this.query = {
      page: 1,
      per_page: 10,
    };
    this.purchases = [];
    this.pagination = {
      total: 0,
      total_pages: 0,
    };
  }
  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 subscriptions */
  async getItems() {
    const response = (await await apiFetch({
      path: addQueryArgs(`surecart/v1/purchases/`, {
        expand: ['product', 'product.downloads', 'download.media'],
        downloadable: true,
        revoked: false,
        ...this.query,
      }),
      parse: false,
    }));
    this.pagination = {
      total: parseInt(response.headers.get('X-WP-Total')),
      total_pages: parseInt(response.headers.get('X-WP-TotalPages')),
    };
    this.purchases = (await response.json());
    return this.purchases;
  }
  nextPage() {
    this.query.page = this.query.page + 1;
    this.fetchItems();
  }
  prevPage() {
    this.query.page = this.query.page - 1;
    this.fetchItems();
  }
  render() {
    var _a;
    return (h("sc-downloads-list", { heading: this.heading, allLink: this.allLink && this.pagination.total_pages > 1 ? this.allLink : '', loading: this.loading, busy: this.busy, requestNonce: this.requestNonce, error: this.error, purchases: this.purchases }, h("span", { slot: "heading" }, h("slot", { name: "heading" }, this.heading || __('Downloads', 'surecart'))), h("sc-pagination", { slot: "after", page: this.query.page, perPage: this.query.per_page, total: this.pagination.total, totalPages: this.pagination.total_pages, totalShowing: (_a = this === null || this === void 0 ? void 0 : this.purchases) === null || _a === void 0 ? void 0 : _a.length, onScNextPage: () => this.nextPage(), onScPrevPage: () => this.prevPage() })));
  }
  get el() { return this; }
  static get style() { return scDashboardDownloadsListCss; }
}, [1, "sc-dashboard-downloads-list", {
    "query": [1040],
    "allLink": [1, "all-link"],
    "heading": [1],
    "requestNonce": [1, "request-nonce"],
    "purchases": [32],
    "loading": [32],
    "busy": [32],
    "error": [32],
    "pagination": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-dashboard-downloads-list", "sc-alert", "sc-block-ui", "sc-button", "sc-card", "sc-dashboard-module", "sc-divider", "sc-downloads-list", "sc-empty", "sc-flex", "sc-format-bytes", "sc-icon", "sc-pagination", "sc-skeleton", "sc-spacing", "sc-spinner", "sc-stacked-list", "sc-stacked-list-row"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-dashboard-downloads-list":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScDownloadsList);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$i();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$h();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "sc-divider":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-downloads-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-empty":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-format-bytes":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-pagination":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-spacing":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-stacked-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-stacked-list-row":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScDashboardDownloadsList = ScDownloadsList;
const defineCustomElement = defineCustomElement$1;

export { ScDashboardDownloadsList, defineCustomElement };
