import { proxyCustomElement, HTMLElement, h, Fragment } from '@stencil/core/internal/client';
import { _ as __, s as sprintf } 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$B } from './sc-alert2.js';
import { d as defineCustomElement$A } from './sc-block-ui2.js';
import { d as defineCustomElement$z } from './sc-button2.js';
import { d as defineCustomElement$y } from './sc-cancel-dialog2.js';
import { d as defineCustomElement$x } from './sc-cancel-discount2.js';
import { d as defineCustomElement$w } from './sc-cancel-survey2.js';
import { d as defineCustomElement$v } from './sc-card2.js';
import { d as defineCustomElement$u } from './sc-cc-logo2.js';
import { d as defineCustomElement$t } from './sc-choice2.js';
import { d as defineCustomElement$s } from './sc-choices2.js';
import { d as defineCustomElement$r } from './sc-dashboard-module2.js';
import { d as defineCustomElement$q } from './sc-dialog2.js';
import { d as defineCustomElement$p } from './sc-divider2.js';
import { d as defineCustomElement$o } from './sc-flex2.js';
import { d as defineCustomElement$n } from './sc-form2.js';
import { d as defineCustomElement$m } from './sc-form-control2.js';
import { d as defineCustomElement$l } from './sc-format-date2.js';
import { d as defineCustomElement$k } from './sc-format-number2.js';
import { d as defineCustomElement$j } from './sc-icon2.js';
import { d as defineCustomElement$i } from './sc-line-item2.js';
import { d as defineCustomElement$h } from './sc-payment-method2.js';
import { d as defineCustomElement$g } from './sc-product-line-item2.js';
import { d as defineCustomElement$f } from './sc-quantity-select2.js';
import { d as defineCustomElement$e } from './sc-skeleton2.js';
import { d as defineCustomElement$d } from './sc-spinner2.js';
import { d as defineCustomElement$c } from './sc-stacked-list2.js';
import { d as defineCustomElement$b } from './sc-stacked-list-row2.js';
import { d as defineCustomElement$a } from './sc-subscription-cancel2.js';
import { d as defineCustomElement$9 } from './sc-subscription-details2.js';
import { d as defineCustomElement$8 } from './sc-subscription-next-payment2.js';
import { d as defineCustomElement$7 } from './sc-subscription-status-badge2.js';
import { d as defineCustomElement$6 } from './sc-tag2.js';
import { d as defineCustomElement$5 } from './sc-text2.js';
import { d as defineCustomElement$4 } from './sc-textarea2.js';
import { d as defineCustomElement$3 } from './sc-toggle2.js';
import { d as defineCustomElement$2 } from './sc-tooltip2.js';
import { a as addQueryArgs } from './add-query-args.js';
import { g as getQueryArg } from './get-query-arg.js';

const scSubscriptionCss = ":host{display:block}.subscription{display:grid;gap:0.5em}.subscription 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)}.subscription a.cancel{color:var(--sc-color-danger-500)}";

const ScSubscription$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
  }
  componentWillLoad() {
    onFirstVisible(this.el, () => {
      if (!this.subscription) {
        this.getSubscription();
      }
    });
  }
  async cancelPendingUpdate() {
    var _a;
    const r = confirm(__('Are you sure you want to cancel the pending update to your plan?', 'surecart'));
    if (!r)
      return;
    try {
      this.busy = true;
      this.subscription = (await apiFetch({
        path: addQueryArgs(`surecart/v1/subscriptions/${(_a = this.subscription) === null || _a === void 0 ? void 0 : _a.id}/`, {
          expand: ['price', 'price.product', 'current_period', 'period.checkout', 'purchase', 'purchase.license', 'license.activations', 'discount', 'discount.coupon'],
        }),
        method: 'PATCH',
        data: {
          purge_pending_update: true,
        },
      }));
    }
    catch (e) {
      if (e === null || e === void 0 ? void 0 : e.message) {
        this.error = e.message;
      }
      else {
        this.error = __('Something went wrong', 'surecart');
      }
      console.error(this.error);
    }
    finally {
      this.busy = false;
    }
  }
  async renewSubscription() {
    var _a;
    try {
      this.error = '';
      this.busy = true;
      this.subscription = (await apiFetch({
        path: addQueryArgs(`surecart/v1/subscriptions/${(_a = this.subscription) === null || _a === void 0 ? void 0 : _a.id}/renew`, {
          expand: ['price', 'price.product', 'current_period', 'period.checkout', 'purchase', 'purchase.license', 'license.activations', 'discount', 'discount.coupon'],
        }),
        method: 'PATCH',
      }));
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.busy = false;
    }
  }
  /** Get all subscriptions */
  async getSubscription() {
    var _a;
    try {
      this.loading = true;
      this.subscription = (await await apiFetch({
        path: addQueryArgs(`surecart/v1/subscriptions/${this.subscriptionId || ((_a = this.subscription) === null || _a === void 0 ? void 0 : _a.id)}`, {
          expand: ['price', 'price.product', 'current_period'],
          ...(this.query || {}),
        }),
      }));
    }
    catch (e) {
      if (e === null || e === void 0 ? void 0 : e.message) {
        this.error = e.message;
      }
      else {
        this.error = __('Something went wrong', 'surecart');
      }
      console.error(this.error);
    }
    finally {
      this.loading = false;
    }
  }
  renderName(subscription) {
    var _a, _b, _c;
    if (typeof ((_a = subscription === null || subscription === void 0 ? void 0 : subscription.price) === null || _a === void 0 ? void 0 : _a.product) !== 'string') {
      return (_c = (_b = subscription === null || subscription === void 0 ? void 0 : subscription.price) === null || _b === void 0 ? void 0 : _b.product) === null || _c === void 0 ? void 0 : _c.name;
    }
    return __('Subscription', 'surecart');
  }
  renderRenewalText(subscription) {
    const tag = h("sc-subscription-status-badge", { subscription: subscription });
    if ((subscription === null || subscription === void 0 ? void 0 : subscription.cancel_at_period_end) && subscription.current_period_end_at) {
      return (h("span", null, tag, " ", sprintf(__('Your plan will be canceled on', 'surecart')), ' ', h("sc-format-date", { date: subscription.current_period_end_at * 1000, month: "long", day: "numeric", year: "numeric" })));
    }
    if (subscription.status === 'trialing' && subscription.trial_end_at) {
      return (h("span", null, tag, " ", sprintf(__('Your plan begins on', 'surecart')), " ", h("sc-format-date", { date: subscription.trial_end_at * 1000, month: "long", day: "numeric", year: "numeric" })));
    }
    if (subscription.status === 'active' && subscription.current_period_end_at) {
      return (h("span", null, tag, " ", sprintf(__('Your plan renews on', 'surecart')), ' ', h("sc-format-date", { date: subscription.current_period_end_at * 1000, month: "long", day: "numeric", year: "numeric" })));
    }
    return tag;
  }
  renderEmpty() {
    return h("slot", { name: "empty" }, __('This subscription does not exist.', 'surecart'));
  }
  renderLoading() {
    return (h("sc-stacked-list-row", { style: { '--columns': '2' }, "mobile-size": 0 }, h("div", { style: { padding: '0.5em' } }, h("sc-skeleton", { style: { width: '30%', marginBottom: '0.75em' } }), h("sc-skeleton", { style: { width: '20%', marginBottom: '0.75em' } }), h("sc-skeleton", { style: { width: '40%' } }))));
  }
  renderContent() {
    if (this.loading) {
      return this.renderLoading();
    }
    if (!this.subscription) {
      return this.renderEmpty();
    }
    return (h(Fragment, null, h("sc-subscription-next-payment", { subscription: this.subscription }, h("sc-subscription-details", { subscription: this.subscription }))));
  }
  render() {
    var _a, _b, _c, _d;
    return (h("sc-dashboard-module", { heading: this.heading || __('Current Plan', 'surecart'), class: "subscription", error: this.error }, !!this.subscription && (h("sc-flex", { slot: "end" }, getQueryArg(window.location.href, 'action') !== 'update_payment_method' && (h("sc-button", { type: "link", href: addQueryArgs(window.location.href, {
        action: 'update_payment_method',
      }) }, h("sc-icon", { name: "credit-card", slot: "prefix" }), __('Update Payment Method', 'surecart'))), !!Object.keys((_a = this.subscription) === null || _a === void 0 ? void 0 : _a.pending_update).length && (h("sc-button", { type: "link", onClick: () => this.cancelPendingUpdate() }, h("sc-icon", { name: "x-octagon", slot: "prefix" }), __('Cancel Scheduled Update', 'surecart'))), ((_b = this === null || this === void 0 ? void 0 : this.subscription) === null || _b === void 0 ? void 0 : _b.cancel_at_period_end) ? (h("sc-button", { type: "link", onClick: () => this.renewSubscription() }, h("sc-icon", { name: "repeat", slot: "prefix" }), __('Restore Plan', 'surecart'))) : (((_c = this.subscription) === null || _c === void 0 ? void 0 : _c.status) !== 'canceled' &&
      ((_d = this.subscription) === null || _d === void 0 ? void 0 : _d.current_period_end_at) &&
      this.showCancel && (h("sc-button", { type: "link", onClick: () => (this.cancelModal = true) }, h("sc-icon", { name: "x", slot: "prefix" }), __('Cancel Plan', 'surecart')))))), h("sc-card", { style: { '--overflow': 'hidden' }, noPadding: true }, this.renderContent()), this.busy && h("sc-block-ui", { spinner: true }), h("sc-cancel-dialog", { subscription: this.subscription, protocol: this.protocol, open: this.cancelModal, onScRequestClose: () => (this.cancelModal = false), onScRefresh: () => this.getSubscription() })));
  }
  get el() { return this; }
  static get style() { return scSubscriptionCss; }
}, [1, "sc-subscription", {
    "subscriptionId": [1, "subscription-id"],
    "showCancel": [4, "show-cancel"],
    "heading": [1],
    "query": [16],
    "protocol": [16],
    "subscription": [1040],
    "loading": [32],
    "cancelModal": [32],
    "busy": [32],
    "error": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-subscription", "sc-alert", "sc-block-ui", "sc-button", "sc-cancel-dialog", "sc-cancel-discount", "sc-cancel-survey", "sc-card", "sc-cc-logo", "sc-choice", "sc-choices", "sc-dashboard-module", "sc-dialog", "sc-divider", "sc-flex", "sc-form", "sc-form-control", "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-cancel", "sc-subscription-details", "sc-subscription-next-payment", "sc-subscription-status-badge", "sc-tag", "sc-text", "sc-textarea", "sc-toggle", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-subscription":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScSubscription$1);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$B();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$A();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$z();
      }
      break;
    case "sc-cancel-dialog":
      if (!customElements.get(tagName)) {
        defineCustomElement$y();
      }
      break;
    case "sc-cancel-discount":
      if (!customElements.get(tagName)) {
        defineCustomElement$x();
      }
      break;
    case "sc-cancel-survey":
      if (!customElements.get(tagName)) {
        defineCustomElement$w();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$v();
      }
      break;
    case "sc-cc-logo":
      if (!customElements.get(tagName)) {
        defineCustomElement$u();
      }
      break;
    case "sc-choice":
      if (!customElements.get(tagName)) {
        defineCustomElement$t();
      }
      break;
    case "sc-choices":
      if (!customElements.get(tagName)) {
        defineCustomElement$s();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$r();
      }
      break;
    case "sc-dialog":
      if (!customElements.get(tagName)) {
        defineCustomElement$q();
      }
      break;
    case "sc-divider":
      if (!customElements.get(tagName)) {
        defineCustomElement$p();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$o();
      }
      break;
    case "sc-form":
      if (!customElements.get(tagName)) {
        defineCustomElement$n();
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$m();
      }
      break;
    case "sc-format-date":
      if (!customElements.get(tagName)) {
        defineCustomElement$l();
      }
      break;
    case "sc-format-number":
      if (!customElements.get(tagName)) {
        defineCustomElement$k();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$j();
      }
      break;
    case "sc-line-item":
      if (!customElements.get(tagName)) {
        defineCustomElement$i();
      }
      break;
    case "sc-payment-method":
      if (!customElements.get(tagName)) {
        defineCustomElement$h();
      }
      break;
    case "sc-product-line-item":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "sc-quantity-select":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-stacked-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-stacked-list-row":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-subscription-cancel":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-subscription-details":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-subscription-next-payment":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-subscription-status-badge":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-tag":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-text":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-textarea":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-toggle":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScSubscription = ScSubscription$1;
const defineCustomElement = defineCustomElement$1;

export { ScSubscription, defineCustomElement };
