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

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

const ScSubscriptionCancel = class {
  constructor(hostRef) {
    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) || __('Something went wrong', 'surecart');
    }
    finally {
      this.loading = false;
    }
  }
  async fetchProtocol() {
    this.protocol = (await apiFetch({
      path: '/surecart/v1/subscription_protocol',
    }));
  }
  async fetchSubscription() {
    if (!this.subscriptionId)
      return;
    this.subscription = (await apiFetch({
      path: addQueryArgs(`/surecart/v1/subscriptions/${this.subscriptionId}`, {
        expand: ['price', 'price.product', 'latest_invoice', 'product'],
      }),
    }));
  }
  async cancelSubscription() {
    try {
      this.error = '';
      this.busy = true;
      await 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) || __('Something went wrong', 'surecart');
      this.busy = false;
    }
  }
  renderContent() {
    var _a, _b;
    if (this.loading) {
      return this.renderLoading();
    }
    return (h(Fragment, null, 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' ? (h("sc-alert", { type: "info", open: true }, __('Your plan will be canceled, but is still available until the end of your billing period on', 'surecart'), ' ', h("strong", null, 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" })), ". ", __('If you change your mind, you can renew your subscription.', 'surecart'))) : (h("sc-alert", { type: "info", open: true }, __('Your plan will be canceled immediately. You cannot change your mind.', 'surecart')))));
  }
  renderLoading() {
    return (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%' } })));
  }
  render() {
    return (h("sc-dashboard-module", { heading: this.heading || __('Cancel your plan', 'surecart'), class: "subscription-cancel", error: this.error }, h("sc-card", null, this.renderContent(), h("sc-button", { type: "primary", full: true, loading: this.loading || this.busy, disabled: this.loading || this.busy, onClick: () => this.cancelSubscription() }, __('Cancel Plan', 'surecart')), !!this.backUrl && (h("sc-button", { href: this.backUrl, full: true, loading: this.loading || this.busy, disabled: this.loading || this.busy }, __('Go Back', 'surecart')))), this.busy && h("sc-block-ui", null)));
  }
};
ScSubscriptionCancel.style = scSubscriptionCancelCss;

export { ScSubscriptionCancel as sc_subscription_cancel };
