import { proxyCustomElement, HTMLElement, createEvent, 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$9 } from './sc-alert2.js';
import { d as defineCustomElement$8 } from './sc-block-ui2.js';
import { d as defineCustomElement$7 } from './sc-button2.js';
import { d as defineCustomElement$6 } from './sc-dashboard-module2.js';
import { d as defineCustomElement$5 } from './sc-flex2.js';
import { d as defineCustomElement$4 } from './sc-format-date2.js';
import { d as defineCustomElement$3 } from './sc-icon2.js';
import { d as defineCustomElement$2 } from './sc-skeleton2.js';
import { d as defineCustomElement$1 } from './sc-spinner2.js';
import { a as addQueryArgs } from './add-query-args.js';

const scSubscriptionCancelCss = ":host{display:block;position:relative}.subscription-cancel{display:grid;gap:0.5em}.subscription-cancel__terms{color:var(--sc-color-gray-600);font-size:var(--sc-font-size-small)}";

const ScSubscriptionCancel = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.scAbandon = createEvent(this, "scAbandon", 7);
    this.scCancelled = createEvent(this, "scCancelled", 7);
  }
  async cancelSubscription() {
    var _a, _b;
    try {
      this.error = '';
      this.busy = true;
      await apiFetch({
        path: addQueryArgs(`/surecart/v1/subscriptions/${(_a = this.subscription) === null || _a === void 0 ? void 0 : _a.id}/cancel`, {
          cancellation_act: {
            ...(!!this.comment ? { comment: this.comment } : {}),
            cancellation_reason_id: (_b = this.reason) === null || _b === void 0 ? void 0 : _b.id,
          },
        }),
        method: 'PATCH',
      });
      this.scCancelled.emit();
    }
    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, ((_a = this === null || this === void 0 ? void 0 : this.protocol) === null || _a === void 0 ? void 0 : _a.cancel_behavior) === 'pending' ? (h("div", { slot: "description" }, __('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("div", { slot: "description" }, __('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, style: { '--sc-dashboard-module-spacing': '1em' } }, this.renderContent(), h("sc-flex", { justifyContent: "flex-start" }, h("sc-button", { type: "primary", loading: this.loading || this.busy, disabled: this.loading || this.busy, onClick: () => this.cancelSubscription() }, __('Cancel Plan', 'surecart')), h("sc-button", { style: { color: 'var(--sc-color-gray-500' }, type: "text", onClick: () => this.scAbandon.emit(), loading: this.loading || this.busy, disabled: this.loading || this.busy }, __('Keep My Plan', 'surecart'))), this.busy && h("sc-block-ui", null)));
  }
  static get style() { return scSubscriptionCancelCss; }
}, [1, "sc-subscription-cancel", {
    "heading": [1],
    "backUrl": [1, "back-url"],
    "successUrl": [1, "success-url"],
    "subscription": [16],
    "protocol": [16],
    "reason": [16],
    "comment": [1],
    "loading": [32],
    "busy": [32],
    "error": [32]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-subscription-cancel", "sc-alert", "sc-block-ui", "sc-button", "sc-dashboard-module", "sc-flex", "sc-format-date", "sc-icon", "sc-skeleton", "sc-spinner"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-subscription-cancel":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScSubscriptionCancel);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-format-date":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}
defineCustomElement();

export { ScSubscriptionCancel as S, defineCustomElement as d };
