import { r as registerInstance, h, F as Fragment, g as getElement } from './index-fd4790f6.js';
import { _ as __, s as sprintf } from './default-i18n-2e663329.js';
import './index-53ddb833.js';
import { a as apiFetch } from './fetch-460a8bc3.js';
import { o as onFirstVisible } from './lazy-64c2bf3b.js';
import { a as addQueryArgs } from './add-query-args-f4c5962b.js';
import { g as getQueryArg } from './get-query-arg-cb6b8763.js';
import './index-6db066f7.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 = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
  }
  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 getElement(this); }
};
ScSubscription.style = scSubscriptionCss;

export { ScSubscription as sc_subscription };
