'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 addQueryArgs = require('./add-query-args-17c551b6.js');
require('./_commonjsHelpers-537d719a.js');

const scSubscriptionCancelCss = ":host{display:block;position:relative}.subscription-cancel{display:grid;gap:0.5em}";

const ScSubscriptionCancel = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
  }
  componentWillLoad() {
    this.fetchItems();
  }
  async fetchItems() {
    try {
      this.loading = true;
      await Promise.all([this.fetchProtocol(), this.fetchSubscription()]);
    }
    catch (e) {
      console.error(e);
      this.error = (e === null || e === void 0 ? void 0 : e.message) || defaultI18n.__('Something went wrong', 'surecart');
    }
    finally {
      this.loading = false;
    }
  }
  async fetchProtocol() {
    this.protocol = (await fetch.apiFetch({
      path: '/surecart/v1/subscription_protocol',
    }));
  }
  async fetchSubscription() {
    if (!this.subscriptionId)
      return;
    this.subscription = (await fetch.apiFetch({
      path: addQueryArgs.addQueryArgs(`/surecart/v1/subscriptions/${this.subscriptionId}`, {
        expand: ['price', 'price.product', 'latest_invoice', 'product'],
      }),
    }));
  }
  async cancelSubscription() {
    try {
      this.error = '';
      this.busy = true;
      await fetch.apiFetch({
        path: `/surecart/v1/subscriptions/${this.subscriptionId}/cancel`,
        method: 'PATCH',
      });
      if (this.successUrl) {
        window.location.assign(this.successUrl);
      }
      else {
        this.busy = false;
      }
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || defaultI18n.__('Something went wrong', 'surecart');
      this.busy = false;
    }
  }
  renderContent() {
    var _a, _b;
    if (this.loading) {
      return this.renderLoading();
    }
    return (index.h(index.Fragment, null, index.h("sc-subscription-details", { subscription: this === null || this === void 0 ? void 0 : this.subscription, hideRenewalText: true }), ((_a = this === null || this === void 0 ? void 0 : this.protocol) === null || _a === void 0 ? void 0 : _a.cancel_behavior) === 'pending' ? (index.h("sc-alert", { type: "info", open: true }, defaultI18n.__('Your plan will be canceled, but is still available until the end of your billing period on', 'surecart'), ' ', index.h("strong", null, index.h("sc-format-date", { type: "timestamp", date: (_b = this === null || this === void 0 ? void 0 : this.subscription) === null || _b === void 0 ? void 0 : _b.current_period_end_at, month: "long", day: "numeric", year: "numeric" })), ". ", defaultI18n.__('If you change your mind, you can renew your subscription.', 'surecart'))) : (index.h("sc-alert", { type: "info", open: true }, defaultI18n.__('Your plan will be canceled immediately. You cannot change your mind.', 'surecart')))));
  }
  renderLoading() {
    return (index.h("div", { style: { padding: '0.5em' } }, index.h("sc-skeleton", { style: { width: '30%', marginBottom: '0.75em' } }), index.h("sc-skeleton", { style: { width: '20%', marginBottom: '0.75em' } }), index.h("sc-skeleton", { style: { width: '40%' } })));
  }
  render() {
    return (index.h("sc-dashboard-module", { heading: this.heading || defaultI18n.__('Cancel your plan', 'surecart'), class: "subscription-cancel", error: this.error }, index.h("sc-card", null, this.renderContent(), index.h("sc-button", { type: "primary", full: true, loading: this.loading || this.busy, disabled: this.loading || this.busy, onClick: () => this.cancelSubscription() }, defaultI18n.__('Cancel Plan', 'surecart')), !!this.backUrl && (index.h("sc-button", { href: this.backUrl, full: true, loading: this.loading || this.busy, disabled: this.loading || this.busy }, defaultI18n.__('Go Back', 'surecart')))), this.busy && index.h("sc-block-ui", null)));
  }
};
ScSubscriptionCancel.style = scSubscriptionCancelCss;

exports.sc_subscription_cancel = ScSubscriptionCancel;
