import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client';
import { _ as __ } from './default-i18n.js';
import './index2.js';
import { a as apiFetch } from './fetch.js';
import { b as translateRemainingPayments, i as intervalString } from './price.js';
import { f as formatTaxDisplay } from './tax.js';
import { d as defineCustomElement$n } from './sc-button2.js';
import { d as defineCustomElement$m } from './sc-card2.js';
import { d as defineCustomElement$l } from './sc-cc-logo2.js';
import { d as defineCustomElement$k } from './sc-dialog2.js';
import { d as defineCustomElement$j } from './sc-divider2.js';
import { d as defineCustomElement$i } from './sc-flex2.js';
import { d as defineCustomElement$h } from './sc-format-date2.js';
import { d as defineCustomElement$g } from './sc-format-number2.js';
import { d as defineCustomElement$f } from './sc-icon2.js';
import { d as defineCustomElement$e } from './sc-line-item2.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-quantity-select2.js';
import { d as defineCustomElement$a } from './sc-skeleton2.js';
import { d as defineCustomElement$9 } from './sc-spinner2.js';
import { d as defineCustomElement$8 } from './sc-stacked-list2.js';
import { d as defineCustomElement$7 } from './sc-stacked-list-row2.js';
import { d as defineCustomElement$6 } from './sc-subscription-details2.js';
import { d as defineCustomElement$5 } from './sc-subscription-status-badge2.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-toggle2.js';
import { d as defineCustomElement$1 } from './sc-tooltip2.js';
import { a as addQueryArgs } from './add-query-args.js';

const ScSubscriptionNextPayment = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.loading = true;
  }
  componentWillLoad() {
    this.fetch();
  }
  handleSubscriptionChange() {
    this.fetch();
  }
  async fetch() {
    var _a, _b, _c;
    if (((_a = this.subscription) === null || _a === void 0 ? void 0 : _a.cancel_at_period_end) && this.subscription.current_period_end_at) {
      this.loading = false;
      return;
    }
    if (((_b = this.subscription) === null || _b === void 0 ? void 0 : _b.status) === 'canceled') {
      this.loading = false;
      return;
    }
    try {
      this.loading = true;
      this.period = (await apiFetch({
        method: 'PATCH',
        path: addQueryArgs(`surecart/v1/subscriptions/${(_c = this.subscription) === null || _c === void 0 ? void 0 : _c.id}/upcoming_period`, {
          skip_product_group_validation: true,
          expand: [
            'period.checkout',
            'checkout.line_items',
            'checkout.payment_method',
            'payment_method.card',
            'payment_method.payment_instrument',
            'payment_method.paypal_account',
            'payment_method.bank_account',
            'line_item.price',
            'price.product',
            'period.subscription',
          ],
        }),
        data: {
          purge_pending_update: false,
        },
      }));
    }
    catch (e) {
      console.error(e);
      this.error = e;
    }
    finally {
      this.loading = false;
    }
  }
  render() {
    var _a, _b, _c, _d;
    if (this.loading) {
      return (h("sc-toggle", { borderless: true, disabled: true }, h("sc-flex", { slot: "summary", flexDirection: "column" }, h("sc-skeleton", { style: { width: '200px' } }), h("sc-skeleton", { style: { width: '400px' } }), h("sc-skeleton", { style: { width: '300px' } }))));
    }
    const checkout = (_a = this === null || this === void 0 ? void 0 : this.period) === null || _a === void 0 ? void 0 : _a.checkout;
    if (!checkout)
      return (h("div", { style: { padding: 'var(--sc-spacing-medium)' } }, h("sc-subscription-details", { slot: "summary", subscription: this.subscription })));
    return (h(Host, null, h("sc-toggle", { borderless: true, shady: true }, h("span", { slot: "summary" }, h("sc-subscription-details", { subscription: this.subscription }, h("div", { style: { fontSize: 'var(--sc-font-size-small)' } }, __('Your next payment is', 'surecart'), ' ', h("strong", null, 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 })), ' ', !!((_b = this.subscription) === null || _b === void 0 ? void 0 : _b.finite) && ' - ' + translateRemainingPayments((_c = this.subscription) === null || _c === void 0 ? void 0 : _c.remaining_period_count)))), h("sc-card", { noPadding: true, borderless: true }, (_d = checkout === null || checkout === void 0 ? void 0 : checkout.line_items) === null || _d === void 0 ? void 0 :
      _d.data.map(item => {
        var _a, _b, _c, _d, _e;
        return (h("sc-product-line-item", { imageUrl: (_b = (_a = item.price) === null || _a === void 0 ? void 0 : _a.product) === null || _b === void 0 ? void 0 : _b.image_url, name: (_d = (_c = item.price) === null || _c === void 0 ? void 0 : _c.product) === null || _d === void 0 ? void 0 : _d.name, editable: false, removable: false, quantity: item === null || item === void 0 ? void 0 : item.quantity, amount: item === null || item === void 0 ? void 0 : item.total_amount, currency: (_e = item === null || item === void 0 ? void 0 : item.price) === null || _e === void 0 ? void 0 : _e.currency, interval: intervalString(item === null || item === void 0 ? void 0 : item.price) }));
      }), h("sc-line-item", null, h("span", { slot: "description" }, __('Subtotal', 'surecart')), h("sc-format-number", { slot: "price", type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.subtotal_amount })), !!checkout.proration_amount && (h("sc-line-item", null, h("span", { slot: "description" }, __('Proration Credit', 'surecart')), h("sc-format-number", { slot: "price", type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: -(checkout === null || checkout === void 0 ? void 0 : checkout.proration_amount) }))), !!checkout.applied_balance_amount && (h("sc-line-item", null, h("span", { slot: "description" }, __('Applied Balance', 'surecart')), h("sc-format-number", { slot: "price", 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.trial_amount && (h("sc-line-item", null, h("span", { slot: "description" }, __('Free Trial', 'surecart')), h("sc-format-number", { slot: "price", type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.trial_amount }))), !!(checkout === null || checkout === void 0 ? void 0 : checkout.discount_amount) && (h("sc-line-item", null, h("span", { slot: "description" }, __('Discounts', 'surecart')), h("sc-format-number", { slot: "price", type: "currency", currency: checkout === null || checkout === void 0 ? void 0 : checkout.currency, value: checkout === null || checkout === void 0 ? void 0 : checkout.discount_amount }))), !!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", 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': '0' } }), h("sc-line-item", null, h("span", { slot: "description" }, __('Payment', 'surecart')), h("a", { href: addQueryArgs(window.location.href, {
        action: 'update_payment_method',
      }), slot: "price-description" }, h("sc-flex", { "justify-content": "flex-start", "align-items": "center", style: { '--spacing': '0.5em' } }, h("sc-payment-method", { paymentMethod: checkout === null || checkout === void 0 ? void 0 : checkout.payment_method }), h("sc-icon", { name: "edit-3" })))), h("sc-line-item", { style: { '--price-size': 'var(--sc-font-size-x-large)' } }, h("span", { slot: "title" }, __('Total Due', 'surecart')), h("sc-format-number", { slot: "price", 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.currency))))));
  }
  static get watchers() { return {
    "subscription": ["handleSubscriptionChange"]
  }; }
}, [1, "sc-subscription-next-payment", {
    "subscription": [16],
    "period": [32],
    "loading": [32],
    "error": [32],
    "details": [32]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-subscription-next-payment", "sc-button", "sc-card", "sc-cc-logo", "sc-dialog", "sc-divider", "sc-flex", "sc-format-date", "sc-format-number", "sc-icon", "sc-line-item", "sc-payment-method", "sc-product-line-item", "sc-quantity-select", "sc-skeleton", "sc-spinner", "sc-stacked-list", "sc-stacked-list-row", "sc-subscription-details", "sc-subscription-status-badge", "sc-tag", "sc-text", "sc-toggle", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-subscription-next-payment":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScSubscriptionNextPayment);
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$n();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$m();
      }
      break;
    case "sc-cc-logo":
      if (!customElements.get(tagName)) {
        defineCustomElement$l();
      }
      break;
    case "sc-dialog":
      if (!customElements.get(tagName)) {
        defineCustomElement$k();
      }
      break;
    case "sc-divider":
      if (!customElements.get(tagName)) {
        defineCustomElement$j();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$i();
      }
      break;
    case "sc-format-date":
      if (!customElements.get(tagName)) {
        defineCustomElement$h();
      }
      break;
    case "sc-format-number":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-line-item":
      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-quantity-select":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-stacked-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-stacked-list-row":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-subscription-details":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-subscription-status-badge":
      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-toggle":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}
defineCustomElement();

export { ScSubscriptionNextPayment as S, defineCustomElement as d };
