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 scSubscriptionRenewCss = ":host{display:block;position:relative}.subscription-renew{display:grid;gap:0.5em}";

const ScSubscriptionCancel = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
  }
  componentDidLoad() {
    this.fetchItems();
  }
  async fetchItems() {
    try {
      this.loading = true;
      await this.fetchSubscription();
    }
    catch (e) {
      console.error(this.error);
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.loading = false;
    }
  }
  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 renewSubscription() {
    try {
      this.error = '';
      this.busy = true;
      await apiFetch({
        path: `/surecart/v1/subscriptions/${this.subscriptionId}/renew/`,
        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;
    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 }), h("sc-alert", { type: "info", open: true }, __('This plan will no longer be canceled. It will renew on', 'surecart'), ' ', h("sc-format-date", { type: "timestamp", date: (_a = this === null || this === void 0 ? void 0 : this.subscription) === null || _a === void 0 ? void 0 : _a.current_period_end_at, month: "long", day: "numeric", year: "numeric" }), ".", ' ')));
  }
  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 || __('Renew 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.renewSubscription() }, __('Renew 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 = scSubscriptionRenewCss;

export { ScSubscriptionCancel as sc_subscription_renew };
