'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-cd6fab62.js');
const defaultI18n = require('./default-i18n-072e75d7.js');
require('./index-20b57ec7.js');
const fetch = require('./fetch-a8709225.js');
const price = require('./price-4a45d4fd.js');
const addQueryArgs = require('./add-query-args-17c551b6.js');
require('./_commonjsHelpers-537d719a.js');

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

const ScSubscriptionDetails = class {
  constructor(hostRef) {
    index.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 defaultI18n.__('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 fetch.apiFetch({
        path: addQueryArgs.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 = index.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 (index.h("span", null, tag, " ", defaultI18n.sprintf(defaultI18n.__('Your plan will be canceled on', 'surecart')), ' ', index.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 index.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 (index.h("span", null, defaultI18n.__('Your plan switches to', 'surecart'), ' ', index.h("strong", null, index.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 }), ' ', price.intervalString(this.pendingPrice || ((_m = this.subscription) === null || _m === void 0 ? void 0 : _m.price))), ' ', defaultI18n.__('on', 'surecart'), ' ', index.h("sc-format-date", { date: this.subscription.current_period_end_at, type: "timestamp", month: "long", day: "numeric", year: "numeric" })));
      }
      return (index.h("span", null, defaultI18n.__('Your plan switches to', 'surecart'), " ", index.h("strong", null, this.pendingPrice.product.name), " ", defaultI18n.__('on', 'surecart'), ' ', index.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 (index.h("span", null, tag, " ", defaultI18n.sprintf(defaultI18n.__('Your plan begins on', 'surecart')), ' ', index.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 (index.h("span", null, tag, " ", ((_t = this.subscription) === null || _t === void 0 ? void 0 : _t.remaining_period_count) === null ? defaultI18n.__('Your plan renews on', 'surecart') : defaultI18n.__('Your next payment is on'), ' ', index.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 (index.h("div", { class: "subscription-details" }, index.h("sc-text", { style: { '--font-weight': 'var(--sc-font-weight-bold)' } }, this.renderName(), " ", this.hasPendingUpdate && index.h("sc-tag", { size: "small" }, defaultI18n.__('Update Scheduled', 'surecart'))), index.h("div", null, index.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 }), ' ', price.intervalString((_e = this.subscription) === null || _e === void 0 ? void 0 : _e.price)), !this.hideRenewalText && index.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) {
    index.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 (index.h(index.Fragment, null, defaultI18n.__('Cancels', 'surecart'), " ", index.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 defaultI18n.__('Incomplete', 'surecart');
      case 'trialing':
        return defaultI18n.__('Trialing', 'surecart');
      case 'active':
        if (Number.isInteger((_c = this.subscription) === null || _c === void 0 ? void 0 : _c.remaining_period_count)) {
          return defaultI18n.__('Payment Plan', 'surecart');
        }
        return defaultI18n.__('Active', 'surecart');
      case 'past_due':
        return defaultI18n.__('Past Due', 'surecart');
      case 'canceled':
        return defaultI18n.__('Canceled', 'surecart');
      case 'completed':
        return defaultI18n.__('Completed', 'surecart');
      case 'unpaid':
        return defaultI18n.__('Unpaid', 'surecart');
    }
  }
  render() {
    return index.h("sc-tag", { type: this.getType() }, this.getText());
  }
};
ScSubscriptionStatusBadge.style = scSubscriptionStatusBadgeCss;

exports.sc_subscription_details = ScSubscriptionDetails;
exports.sc_subscription_status_badge = ScSubscriptionStatusBadge;
