import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { a as apiFetch } from './fetch.js';
import { d as defineCustomElement$j } from './sc-alert2.js';
import { d as defineCustomElement$i } from './sc-block-ui2.js';
import { d as defineCustomElement$h } from './sc-button2.js';
import { d as defineCustomElement$g } from './sc-cancel-discount2.js';
import { d as defineCustomElement$f } from './sc-cancel-survey2.js';
import { d as defineCustomElement$e } from './sc-choice2.js';
import { d as defineCustomElement$d } from './sc-choices2.js';
import { d as defineCustomElement$c } from './sc-dashboard-module2.js';
import { d as defineCustomElement$b } from './sc-dialog2.js';
import { d as defineCustomElement$a } from './sc-flex2.js';
import { d as defineCustomElement$9 } from './sc-form2.js';
import { d as defineCustomElement$8 } from './sc-form-control2.js';
import { d as defineCustomElement$7 } from './sc-format-date2.js';
import { d as defineCustomElement$6 } from './sc-icon2.js';
import { d as defineCustomElement$5 } from './sc-skeleton2.js';
import { d as defineCustomElement$4 } from './sc-spinner2.js';
import { d as defineCustomElement$3 } from './sc-subscription-cancel2.js';
import { d as defineCustomElement$2 } from './sc-textarea2.js';
import { d as defineCustomElement$1 } from './sc-tooltip2.js';

const scCancelDialogCss = ":host{display:block;font-size:var(--sc-font-size-medium)}.close__button{position:absolute;top:0;right:0;font-size:22px}";

const ScCancelDialog = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.scRequestClose = createEvent(this, "scRequestClose", 7);
    this.scRefresh = createEvent(this, "scRefresh", 7);
    this.step = 'cancel';
  }
  close() {
    this.reset();
    this.trackAttempt();
    this.scRequestClose.emit('close-button');
  }
  reset() {
    var _a;
    this.reason = null;
    this.step = ((_a = this.protocol) === null || _a === void 0 ? void 0 : _a.preservation_enabled) ? 'survey' : 'cancel';
  }
  async trackAttempt() {
    var _a, _b;
    if (!((_a = this.protocol) === null || _a === void 0 ? void 0 : _a.preservation_enabled))
      return;
    await apiFetch({
      method: 'PATCH',
      path: `surecart/v1/subscriptions/${(_b = this.subscription) === null || _b === void 0 ? void 0 : _b.id}/preserve`,
    });
  }
  componentWillLoad() {
    this.reset();
  }
  render() {
    return (h("sc-dialog", { style: {
        '--width': this.step === 'survey' ? '675px' : '500px',
        '--body-spacing': 'var(--sc-spacing-xxx-large)',
      }, noHeader: true, open: this.open, onScRequestClose: () => this.close() }, h("div", { class: {
        cancel: true,
      } }, h("sc-button", { class: "close__button", type: "text", circle: true, onClick: () => this.close() }, h("sc-icon", { name: "x" })), this.step === 'cancel' && (h("sc-subscription-cancel", { subscription: this.subscription, protocol: this.protocol, reason: this.reason, comment: this.comment, onScAbandon: () => this.close(), onScCancelled: () => {
        this.scRefresh.emit();
        this.reset();
        this.scRequestClose.emit('close-button');
      } })), this.step === 'survey' && (h("sc-cancel-survey", { protocol: this.protocol, onScAbandon: () => this.close(), onScSubmitReason: e => {
        const { comment, reason } = e.detail;
        this.reason = reason;
        this.comment = comment;
        this.step = (reason === null || reason === void 0 ? void 0 : reason.coupon_enabled) ? 'discount' : 'cancel';
      } })), this.step === 'discount' && (h("sc-cancel-discount", { protocol: this.protocol, subscription: this.subscription, reason: this.reason, comment: this.comment, onScCancel: () => (this.step = 'cancel'), onScPreserved: () => {
        this.scRefresh.emit();
        this.reset();
        this.scRequestClose.emit('close-button');
      } })))));
  }
  static get style() { return scCancelDialogCss; }
}, [1, "sc-cancel-dialog", {
    "open": [4],
    "protocol": [16],
    "subscription": [16],
    "reasons": [32],
    "reason": [32],
    "step": [32],
    "comment": [32]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-cancel-dialog", "sc-alert", "sc-block-ui", "sc-button", "sc-cancel-discount", "sc-cancel-survey", "sc-choice", "sc-choices", "sc-dashboard-module", "sc-dialog", "sc-flex", "sc-form", "sc-form-control", "sc-format-date", "sc-icon", "sc-skeleton", "sc-spinner", "sc-subscription-cancel", "sc-textarea", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-cancel-dialog":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScCancelDialog);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$j();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$i();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$h();
      }
      break;
    case "sc-cancel-discount":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "sc-cancel-survey":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "sc-choice":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "sc-choices":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-dialog":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-form":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-format-date":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-subscription-cancel":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-textarea":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}
defineCustomElement();

export { ScCancelDialog as S, defineCustomElement as d };
