import { proxyCustomElement, HTMLElement, h, Fragment } from '@stencil/core/internal/client';
import { _ as __ } from './default-i18n.js';
import './index2.js';
import { a as apiFetch } from './fetch.js';
import { d as defineCustomElement$f } from './sc-alert2.js';
import { d as defineCustomElement$e } from './sc-block-ui2.js';
import { d as defineCustomElement$d } from './sc-button2.js';
import { d as defineCustomElement$c } from './sc-card2.js';
import { d as defineCustomElement$b } from './sc-dashboard-module2.js';
import { d as defineCustomElement$a } from './sc-format-date2.js';
import { d as defineCustomElement$9 } from './sc-format-number2.js';
import { d as defineCustomElement$8 } from './sc-icon2.js';
import { d as defineCustomElement$7 } from './sc-skeleton2.js';
import { d as defineCustomElement$6 } from './sc-spinner2.js';
import { d as defineCustomElement$5 } from './sc-subscription-details2.js';
import { d as defineCustomElement$4 } from './sc-subscription-status-badge2.js';
import { d as defineCustomElement$3 } from './sc-tag2.js';
import { d as defineCustomElement$2 } from './sc-text2.js';
import { a as addQueryArgs } from './add-query-args.js';

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

const ScSubscriptionCancel = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
  }
  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)));
  }
  static get style() { return scSubscriptionRenewCss; }
}, [1, "sc-subscription-renew", {
    "heading": [1],
    "subscriptionId": [1, "subscription-id"],
    "backUrl": [1, "back-url"],
    "successUrl": [1, "success-url"],
    "subscription": [1040],
    "loading": [32],
    "busy": [32],
    "error": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-subscription-renew", "sc-alert", "sc-block-ui", "sc-button", "sc-card", "sc-dashboard-module", "sc-format-date", "sc-format-number", "sc-icon", "sc-skeleton", "sc-spinner", "sc-subscription-details", "sc-subscription-status-badge", "sc-tag", "sc-text"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-subscription-renew":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScSubscriptionCancel);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-format-date":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-format-number":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-subscription-details":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-subscription-status-badge":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-tag":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-text":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScSubscriptionRenew = ScSubscriptionCancel;
const defineCustomElement = defineCustomElement$1;

export { ScSubscriptionRenew, defineCustomElement };
