import { proxyCustomElement, HTMLElement, h } 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$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-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-skeleton2.js';
import { d as defineCustomElement$8 } from './sc-spinner2.js';
import { d as defineCustomElement$7 } from './sc-stacked-list2.js';
import { d as defineCustomElement$6 } from './sc-stacked-list-row2.js';
import { d as defineCustomElement$5 } from './sc-subscription-details2.js';
import { d as defineCustomElement$4 } from './sc-subscription-status-badge2.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 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, () => {
      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', 'latest_invoice', 'product'] }),
        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;
    }
  }
  /** Get all subscriptions */
  async getSubscription() {
    if (this.subscription)
      return;
    try {
      this.loading = true;
      this.subscription = (await await apiFetch({
        path: addQueryArgs(`surecart/v1/subscriptions/${this.subscriptionId}`, {
          expand: ['price', 'price.product', 'latest_invoice'],
          ...(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("sc-stacked-list-row", { "mobile-size": 0 }, 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" }, !!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", href: addQueryArgs(window.location.href, {
        action: 'renew',
      }) }, h("sc-icon", { name: "repeat", slot: "prefix" }), __('Renew 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", href: addQueryArgs(window.location.href, {
        action: 'cancel',
      }) }, h("sc-icon", { name: "x", slot: "prefix" }), __('Cancel Plan', 'surecart')))))), h("sc-card", { "no-padding": true, style: { '--overflow': 'hidden' } }, h("sc-stacked-list", null, this.renderContent())), this.busy && h("sc-block-ui", { spinner: true })));
  }
  get el() { return this; }
  static get style() { return scSubscriptionCss; }
}, [1, "sc-subscription", {
    "subscriptionId": [1, "subscription-id"],
    "showCancel": [4, "show-cancel"],
    "heading": [1],
    "query": [16],
    "subscription": [1040],
    "loading": [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-card", "sc-dashboard-module", "sc-flex", "sc-format-date", "sc-format-number", "sc-icon", "sc-skeleton", "sc-spinner", "sc-stacked-list", "sc-stacked-list-row", "sc-subscription-details", "sc-subscription-status-badge", "sc-tag", "sc-text"];
  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$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-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-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-stacked-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-stacked-list-row":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-subscription-details":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-subscription-status-badge":
      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 ScSubscription = ScSubscription$1;
const defineCustomElement = defineCustomElement$1;

export { ScSubscription, defineCustomElement };
