import { proxyCustomElement, HTMLElement, h, Fragment } 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 { i as intervalString } from './price.js';
import { f as formatTaxDisplay } from './tax.js';
import { d as defineCustomElement$t } from './sc-alert2.js';
import { d as defineCustomElement$s } from './sc-block-ui2.js';
import { d as defineCustomElement$r } from './sc-button2.js';
import { d as defineCustomElement$q } from './sc-card2.js';
import { d as defineCustomElement$p } from './sc-cc-logo2.js';
import { d as defineCustomElement$o } from './sc-dashboard-module2.js';
import { d as defineCustomElement$n } from './sc-divider2.js';
import { d as defineCustomElement$m } from './sc-empty2.js';
import { d as defineCustomElement$l } from './sc-flex2.js';
import { d as defineCustomElement$k } from './sc-format-bytes2.js';
import { d as defineCustomElement$j } from './sc-format-date2.js';
import { d as defineCustomElement$i } from './sc-format-number2.js';
import { d as defineCustomElement$h } from './sc-icon2.js';
import { d as defineCustomElement$g } from './sc-line-item2.js';
import { d as defineCustomElement$f } from './sc-order-manual-instructions2.js';
import { d as defineCustomElement$e } from './sc-order-status-badge2.js';
import { d as defineCustomElement$d } from './sc-payment-method2.js';
import { d as defineCustomElement$c } from './sc-product-line-item2.js';
import { d as defineCustomElement$b } from './sc-purchase-downloads-list2.js';
import { d as defineCustomElement$a } from './sc-quantity-select2.js';
import { d as defineCustomElement$9 } from './sc-skeleton2.js';
import { d as defineCustomElement$8 } from './sc-spacing2.js';
import { d as defineCustomElement$7 } from './sc-spinner2.js';
import { d as defineCustomElement$6 } from './sc-stacked-list2.js';
import { d as defineCustomElement$5 } from './sc-stacked-list-row2.js';
import { d as defineCustomElement$4 } from './sc-tag2.js';
import { d as defineCustomElement$3 } from './sc-text2.js';
import { d as defineCustomElement$2 } from './sc-tooltip2.js';
import { a as addQueryArgs } from './add-query-args.js';

const scOrderCss = ":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 ScOrder$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
  }
  /** Only fetch if visible */
  componentDidLoad() {
    onFirstVisible(this.el, () => {
      this.fetchOrder();
      this.fetchDownloads();
    });
  }
  async fetchOrder() {
    try {
      this.loading = true;
      await this.getOrder();
    }
    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 fetchDownloads() {
    try {
      this.busy = true;
      this.purchases = (await apiFetch({
        path: addQueryArgs(`surecart/v1/purchases`, {
          expand: ['product', 'product.downloads', 'download.media'],
          order_ids: [this.orderId],
          customer_ids: this.customerIds,
          downloadable: true,
        }),
      }));
    }
    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 order */
  async getOrder() {
    this.order = (await apiFetch({
      path: addQueryArgs(`surecart/v1/orders/${this.orderId}`, {
        expand: [
          'checkout',
          'checkout.line_items',
          'line_item.price',
          'price.product',
          'checkout.manual_payment_method',
          'checkout.payment_method',
          'payment_method.card',
          'payment_method.payment_instrument',
          'payment_method.paypal_account',
          'payment_method.bank_account',
        ],
      }),
    }));
  }
  renderLoading() {
    return (h("sc-flex", { flexDirection: "column", style: { gap: '1em' } }, h("sc-skeleton", { style: { width: '20%', display: 'inline-block' } }), h("sc-skeleton", { style: { width: '60%', display: 'inline-block' } }), h("sc-skeleton", { style: { width: '40%', display: 'inline-block' } })));
  }
  renderEmpty() {
    return h("sc-empty", { icon: "shopping-bag" }, __('Order not found.', 'surecart'));
  }
  renderContent() {
    var _a, _b, _c;
    if (this.loading) {
      return this.renderLoading();
    }
    if (!((_a = this.order) === null || _a === void 0 ? void 0 : _a.id)) {
      return this.renderEmpty();
    }
    const checkout = (_b = this.order) === null || _b === void 0 ? void 0 : _b.checkout;
    return (h(Fragment, null, (((_c = checkout === null || checkout === void 0 ? void 0 : checkout.line_items) === null || _c === void 0 ? void 0 : _c.data) || []).map(item => {
      var _a, _b, _c, _d, _e, _f;
      return (h("sc-product-line-item", { key: item.id, imageUrl: (_b = (_a = item === null || item === void 0 ? void 0 : item.price) === null || _a === void 0 ? void 0 : _a.product) === null || _b === void 0 ? void 0 : _b.image_url, name: (_d = (_c = item === null || item === void 0 ? void 0 : item.price) === null || _c === void 0 ? void 0 : _c.product) === null || _d === void 0 ? void 0 : _d.name, editable: false, removable: false, quantity: item.quantity, amount: item.subtotal_amount, currency: (_e = item === null || item === void 0 ? void 0 : item.price) === null || _e === void 0 ? void 0 : _e.currency, trialDurationDays: (_f = item === null || item === void 0 ? void 0 : item.price) === null || _f === void 0 ? void 0 : _f.trial_duration_days, interval: intervalString(item === null || item === void 0 ? void 0 : item.price) }));
    }), h("sc-divider", { style: { '--spacing': 'var(--sc-spacing-x-small)' } }), h("sc-line-item", null, h("span", { slot: "description" }, __('Subtotal', 'surecart')), h("sc-format-number", { slot: "price", style: {
        'font-weight': 'var(--sc-font-weight-semibold)',
        'color': 'var(--sc-color-gray-800)',
      }, type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.subtotal_amount })), !!(checkout === null || checkout === void 0 ? void 0 : checkout.proration_amount) && (h("sc-line-item", null, h("span", { slot: "description" }, __('Proration', 'surecart')), h("sc-format-number", { slot: "price", style: {
        'font-weight': 'var(--sc-font-weight-semibold)',
        'color': 'var(--sc-color-gray-800)',
      }, type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.proration_amount }))), !!(checkout === null || checkout === void 0 ? void 0 : checkout.applied_balance_amount) && (h("sc-line-item", null, h("span", { slot: "description" }, __('Applied Balance', 'surecart')), h("sc-format-number", { slot: "price", style: {
        'font-weight': 'var(--sc-font-weight-semibold)',
        'color': 'var(--sc-color-gray-800)',
      }, type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.applied_balance_amount }))), !!(checkout === null || checkout === void 0 ? void 0 : checkout.discounts) && (h("sc-line-item", null, h("span", { slot: "description" }, __('Discount', 'surecart')), h("sc-format-number", { slot: "price", style: {
        'font-weight': 'var(--sc-font-weight-semibold)',
        'color': 'var(--sc-color-gray-800)',
      }, type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.discounts }))), !!(checkout === null || checkout === void 0 ? void 0 : checkout.tax_amount) && (h("sc-line-item", null, h("span", { slot: "description" }, formatTaxDisplay(checkout === null || checkout === void 0 ? void 0 : checkout.tax_label)), h("sc-format-number", { slot: "price", style: {
        'font-weight': 'var(--sc-font-weight-semibold)',
        'color': 'var(--sc-color-gray-800)',
      }, type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.tax_amount }))), h("sc-divider", { style: { '--spacing': 'var(--sc-spacing-x-small)' } }), h("sc-line-item", { style: {
        'width': '100%',
        '--price-size': 'var(--sc-font-size-x-large)',
      } }, h("span", { slot: "title" }, __('Total', 'surecart')), h("span", { slot: "price" }, h("sc-format-number", { type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.amount_due })), h("span", { slot: "currency" }, checkout === null || checkout === void 0 ? void 0 : checkout.currency))));
  }
  render() {
    var _a, _b, _c, _d, _e, _f, _g;
    const checkout = (_a = this === null || this === void 0 ? void 0 : this.order) === null || _a === void 0 ? void 0 : _a.checkout;
    const manualPaymentMethod = checkout === null || checkout === void 0 ? void 0 : checkout.manual_payment_method;
    return (h("sc-spacing", { style: { '--spacing': 'var(--sc-spacing-large)' } }, h("sc-dashboard-module", { error: this.error }, h("span", { slot: "heading" }, this.loading ? h("sc-skeleton", { style: { width: '120px' } }) : `#${(_b = this === null || this === void 0 ? void 0 : this.order) === null || _b === void 0 ? void 0 : _b.number}`), !this.loading && !(checkout === null || checkout === void 0 ? void 0 : checkout.live_mode) && (h("sc-tag", { type: "warning", slot: "end" }, __('Test Mode', 'surecart'))), !!(manualPaymentMethod === null || manualPaymentMethod === void 0 ? void 0 : manualPaymentMethod.name) && !!(manualPaymentMethod === null || manualPaymentMethod === void 0 ? void 0 : manualPaymentMethod.instructions) && (h("sc-order-manual-instructions", { manualPaymentTitle: manualPaymentMethod === null || manualPaymentMethod === void 0 ? void 0 : manualPaymentMethod.name, manualPaymentInstructions: manualPaymentMethod === null || manualPaymentMethod === void 0 ? void 0 : manualPaymentMethod.instructions })), h("sc-card", { "no-padding": !this.loading }, this.loading ? (this.renderLoading()) : (h(Fragment, null, h("sc-stacked-list", null, h("sc-stacked-list-row", { style: { '--columns': '2' } }, h("div", null, __('Order Status', 'surecart')), h("sc-order-status-badge", { status: (_c = this === null || this === void 0 ? void 0 : this.order) === null || _c === void 0 ? void 0 : _c.status })), h("sc-stacked-list-row", { style: { '--columns': '2' } }, h("div", null, __('Date', 'surecart')), h("sc-format-date", { type: "timestamp", date: (_d = this.order) === null || _d === void 0 ? void 0 : _d.created_at, month: "short", day: "numeric", year: "numeric" })), h("sc-stacked-list-row", { style: { '--columns': '2' } }, h("div", null, __('Payment Method', 'surecart')), h("sc-payment-method", { paymentMethod: checkout === null || checkout === void 0 ? void 0 : checkout.payment_method }))))))), ((_e = this.order) === null || _e === void 0 ? void 0 : _e.statement_url) && (h("sc-button", { type: "primary", href: (_f = this.order) === null || _f === void 0 ? void 0 : _f.statement_url, target: "_blank" }, h("sc-icon", { name: "inbox", slot: "prefix" }), __('Download Receipt/Invoice', 'surecart'))), !!((_g = this.purchases) === null || _g === void 0 ? void 0 : _g.length) && h("sc-purchase-downloads-list", { heading: __('Downloads', 'surecart'), purchases: this.purchases })));
  }
  get el() { return this; }
  static get style() { return scOrderCss; }
}, [1, "sc-order", {
    "orderId": [1, "order-id"],
    "customerIds": [16],
    "heading": [1],
    "order": [32],
    "purchases": [32],
    "loading": [32],
    "busy": [32],
    "error": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-order", "sc-alert", "sc-block-ui", "sc-button", "sc-card", "sc-cc-logo", "sc-dashboard-module", "sc-divider", "sc-empty", "sc-flex", "sc-format-bytes", "sc-format-date", "sc-format-number", "sc-icon", "sc-line-item", "sc-order-manual-instructions", "sc-order-status-badge", "sc-payment-method", "sc-product-line-item", "sc-purchase-downloads-list", "sc-quantity-select", "sc-skeleton", "sc-spacing", "sc-spinner", "sc-stacked-list", "sc-stacked-list-row", "sc-tag", "sc-text", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-order":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScOrder$1);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$t();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$s();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$r();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$q();
      }
      break;
    case "sc-cc-logo":
      if (!customElements.get(tagName)) {
        defineCustomElement$p();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$o();
      }
      break;
    case "sc-divider":
      if (!customElements.get(tagName)) {
        defineCustomElement$n();
      }
      break;
    case "sc-empty":
      if (!customElements.get(tagName)) {
        defineCustomElement$m();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$l();
      }
      break;
    case "sc-format-bytes":
      if (!customElements.get(tagName)) {
        defineCustomElement$k();
      }
      break;
    case "sc-format-date":
      if (!customElements.get(tagName)) {
        defineCustomElement$j();
      }
      break;
    case "sc-format-number":
      if (!customElements.get(tagName)) {
        defineCustomElement$i();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$h();
      }
      break;
    case "sc-line-item":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "sc-order-manual-instructions":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-order-status-badge":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "sc-payment-method":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-product-line-item":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-purchase-downloads-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-quantity-select":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-spacing":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-stacked-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-stacked-list-row":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-tag":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-text":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScOrder = ScOrder$1;
const defineCustomElement = defineCustomElement$1;

export { ScOrder, defineCustomElement };
