import { r as registerInstance, h, F as Fragment } from './index-f1d2a96f.js';
import { _ as __, s as sprintf } from './default-i18n-91ef32a0.js';
import './index-0a61debd.js';
import { a as apiFetch } from './fetch-c0c608db.js';
import { i as intervalString } from './price-593b80d5.js';
import { a as addQueryArgs } from './add-query-args-f4c5962b.js';
import './_commonjsHelpers-9943807e.js';

const scSubscriptionDetailsCss = ":host{display:block}.subscription-details{display:grid;gap:0.25em;color:var(--sc-input-label-color)}";

const ScSubscriptionDetails = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
  }
  renderName() {
    var _a, _b, _c, _d, _e;
    if (typeof ((_b = (_a = this.subscription) === null || _a === void 0 ? void 0 : _a.price) === null || _b === void 0 ? void 0 : _b.product) !== 'string') {
      return (_e = (_d = (_c = this.subscription) === null || _c === void 0 ? void 0 : _c.price) === null || _d === void 0 ? void 0 : _d.product) === null || _e === void 0 ? void 0 : _e.name;
    }
    return __('Subscription', 'surecart');
  }
  async handleSubscriptionChange() {
    var _a, _b, _c, _d;
    this.hasPendingUpdate = !!((_b = Object.keys(((_a = this === null || this === void 0 ? void 0 : this.subscription) === null || _a === void 0 ? void 0 : _a.pending_update) || {})) === null || _b === void 0 ? void 0 : _b.length);
    if (((_d = (_c = this === null || this === void 0 ? void 0 : this.subscription) === null || _c === void 0 ? void 0 : _c.pending_update) === null || _d === void 0 ? void 0 : _d.price) && !(this === null || this === void 0 ? void 0 : this.pendingPrice) && !this.hideRenewalText) {
      this.pendingPrice = await this.fetchPrice(this.subscription.pending_update.price);
    }
  }
  componentWillLoad() {
    this.handleSubscriptionChange();
  }
  async fetchPrice(price_id) {
    try {
      this.loading = true;
      const price = await apiFetch({
        path: addQueryArgs(`surecart/v1/prices/${price_id}`, {
          expand: ['product'],
        }),
      });
      return price;
    }
    catch (e) {
      console.error(e);
    }
    finally {
      this.loading = false;
    }
  }
  renderRenewalText() {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
    const tag = h("sc-subscription-status-badge", { subscription: this === null || this === void 0 ? void 0 : this.subscription });
    if (((_a = this === null || this === void 0 ? void 0 : this.subscription) === null || _a === void 0 ? void 0 : _a.cancel_at_period_end) && ((_b = this === null || this === void 0 ? void 0 : this.subscription) === null || _b === void 0 ? void 0 : _b.current_period_end_at)) {
      return (h("span", null, tag, " ", sprintf(__('Your plan will be canceled on', 'surecart')), ' ', h("sc-format-date", { date: this.subscription.current_period_end_at * 1000, month: "long", day: "numeric", year: "numeric" })));
    }
    if (this.hasPendingUpdate) {
      if (!this.pendingPrice && !((_d = (_c = this.subscription) === null || _c === void 0 ? void 0 : _c.pending_update) === null || _d === void 0 ? void 0 : _d.ad_hoc_amount)) {
        return h("sc-skeleton", null);
      }
      if ((_f = (_e = this.subscription) === null || _e === void 0 ? void 0 : _e.pending_update) === null || _f === void 0 ? void 0 : _f.ad_hoc_amount) {
        return (h("span", null, __('Your plan switches to', 'surecart'), ' ', h("strong", null, h("sc-format-number", { type: "currency", currency: ((_g = this.pendingPrice) === null || _g === void 0 ? void 0 : _g.currency) || ((_j = (_h = this.subscription) === null || _h === void 0 ? void 0 : _h.price) === null || _j === void 0 ? void 0 : _j.currency), value: (_l = (_k = this.subscription) === null || _k === void 0 ? void 0 : _k.pending_update) === null || _l === void 0 ? void 0 : _l.ad_hoc_amount }), ' ', intervalString(this.pendingPrice || ((_m = this.subscription) === null || _m === void 0 ? void 0 : _m.price))), ' ', __('on', 'surecart'), ' ', h("sc-format-date", { date: this.subscription.current_period_end_at, type: "timestamp", month: "long", day: "numeric", year: "numeric" })));
      }
      return (h("span", null, __('Your plan switches to', 'surecart'), " ", h("strong", null, this.pendingPrice.product.name), " ", __('on', 'surecart'), ' ', h("sc-format-date", { date: this.subscription.current_period_end_at, type: "timestamp", month: "long", day: "numeric", year: "numeric" })));
    }
    if (((_o = this === null || this === void 0 ? void 0 : this.subscription) === null || _o === void 0 ? void 0 : _o.status) === 'trialing' && ((_p = this === null || this === void 0 ? void 0 : this.subscription) === null || _p === void 0 ? void 0 : _p.trial_end_at)) {
      return (h("span", null, tag, " ", sprintf(__('Your plan begins on', 'surecart')), ' ', h("sc-format-date", { date: (_q = this === null || this === void 0 ? void 0 : this.subscription) === null || _q === void 0 ? void 0 : _q.trial_end_at, type: "timestamp", month: "long", day: "numeric", year: "numeric" })));
    }
    if (((_r = this.subscription) === null || _r === void 0 ? void 0 : _r.status) === 'active' && ((_s = this.subscription) === null || _s === void 0 ? void 0 : _s.current_period_end_at)) {
      return (h("span", null, tag, " ", ((_t = this.subscription) === null || _t === void 0 ? void 0 : _t.remaining_period_count) === null ? __('Your plan renews on', 'surecart') : __('Your next payment is on'), ' ', h("sc-format-date", { date: (_u = this === null || this === void 0 ? void 0 : this.subscription) === null || _u === void 0 ? void 0 : _u.current_period_end_at, type: "timestamp", month: "long", day: "numeric", year: "numeric" })));
    }
    return tag;
  }
  render() {
    var _a, _b, _c, _d, _e;
    return (h("div", { class: "subscription-details" }, h("sc-text", { style: { '--font-weight': 'var(--sc-font-weight-bold)' } }, this.renderName(), " ", this.hasPendingUpdate && h("sc-tag", { size: "small" }, __('Update Scheduled', 'surecart'))), h("div", null, h("sc-format-number", { type: "currency", currency: (_b = (_a = this.subscription) === null || _a === void 0 ? void 0 : _a.latest_invoice) === null || _b === void 0 ? void 0 : _b.currency, value: (_d = (_c = this.subscription) === null || _c === void 0 ? void 0 : _c.latest_invoice) === null || _d === void 0 ? void 0 : _d.total_amount }), ' ', intervalString((_e = this.subscription) === null || _e === void 0 ? void 0 : _e.price)), !this.hideRenewalText && h("div", null, this.renderRenewalText())));
  }
  static get watchers() { return {
    "subscription": ["handleSubscriptionChange"]
  }; }
};
ScSubscriptionDetails.style = scSubscriptionDetailsCss;

const scSubscriptionStatusBadgeCss = ":host{display:inline-block}";

const ScSubscriptionStatusBadge = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    /** The tag's size. */
    this.size = 'medium';
    /** Draws a pill-style tag with rounded edges. */
    this.pill = false;
    /** Makes the tag clearable. */
    this.clearable = false;
  }
  getType() {
    var _a, _b;
    if ((_a = this.subscription) === null || _a === void 0 ? void 0 : _a.cancel_at_period_end) {
      return 'info';
    }
    switch (this.status || ((_b = this.subscription) === null || _b === void 0 ? void 0 : _b.status)) {
      case 'incomplete':
        return 'warning';
      case 'trialing':
        return 'info';
      case 'active':
        return 'success';
      case 'completed':
        return 'success';
      case 'past_due':
        return 'warning';
      case 'canceled':
        return 'danger';
      case 'unpaid':
        return 'warning';
    }
  }
  getText() {
    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) {
      return (h(Fragment, null, __('Cancels', 'surecart'), " ", h("sc-format-date", { type: "timestamp", date: this.subscription.current_period_end_at, month: "short", day: "numeric" })));
    }
    switch (this.status || ((_b = this.subscription) === null || _b === void 0 ? void 0 : _b.status)) {
      case 'incomplete':
        return __('Incomplete', 'surecart');
      case 'trialing':
        return __('Trialing', 'surecart');
      case 'active':
        if (Number.isInteger((_c = this.subscription) === null || _c === void 0 ? void 0 : _c.remaining_period_count)) {
          return __('Payment Plan', 'surecart');
        }
        return __('Active', 'surecart');
      case 'past_due':
        return __('Past Due', 'surecart');
      case 'canceled':
        return __('Canceled', 'surecart');
      case 'completed':
        return __('Completed', 'surecart');
      case 'unpaid':
        return __('Unpaid', 'surecart');
    }
  }
  render() {
    return h("sc-tag", { type: this.getType() }, this.getText());
  }
};
ScSubscriptionStatusBadge.style = scSubscriptionStatusBadgeCss;

export { ScSubscriptionDetails as sc_subscription_details, ScSubscriptionStatusBadge as sc_subscription_status_badge };
