import { proxyCustomElement, HTMLElement, createEvent, h } 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$d } from './sc-alert2.js';
import { d as defineCustomElement$c } from './sc-button2.js';
import { d as defineCustomElement$b } from './sc-choice2.js';
import { d as defineCustomElement$a } from './sc-choices2.js';
import { d as defineCustomElement$9 } from './sc-dashboard-module2.js';
import { d as defineCustomElement$8 } from './sc-flex2.js';
import { d as defineCustomElement$7 } from './sc-form2.js';
import { d as defineCustomElement$6 } from './sc-form-control2.js';
import { d as defineCustomElement$5 } from './sc-icon2.js';
import { d as defineCustomElement$4 } from './sc-skeleton2.js';
import { d as defineCustomElement$3 } from './sc-spinner2.js';
import { d as defineCustomElement$2 } from './sc-textarea2.js';
import { d as defineCustomElement$1 } from './sc-tooltip2.js';

const scCancelSurveyCss = ".cancel-survey{color:var(--sc-color-gray-900)}.cancel-survey__abort-link{color:var(--sc-color-gray-500)}";

const ScCancelSurvey = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.scAbandon = createEvent(this, "scAbandon", 7);
    this.scSubmitReason = createEvent(this, "scSubmitReason", 7);
  }
  componentWillLoad() {
    if (!this.reasons) {
      this.fetchReasons();
    }
  }
  handleSelectedReasonChange() {
    var _a;
    if ((_a = this.selectedReason) === null || _a === void 0 ? void 0 : _a.comment_enabled) {
      setTimeout(() => {
        this.textArea.triggerFocus();
      }, 50);
    }
  }
  async fetchReasons() {
    try {
      this.loading = true;
      this.reasons = await apiFetch({
        path: 'surecart/v1/cancellation_reasons',
      });
    }
    catch (e) {
      console.error(e);
      this.error = e;
    }
    finally {
      this.loading = false;
    }
  }
  async handleSubmit(e) {
    e.preventDefault();
    e.stopImmediatePropagation();
    try {
      this.scSubmitReason.emit({ reason: this.selectedReason, comment: this.comment });
    }
    catch (e) {
      console.error(e);
      this.error = e;
    }
  }
  renderReasons() {
    if (this.loading) {
      return (h("sc-choice", null, h("sc-skeleton", null)));
    }
    return (this.reasons || []).map(reason => (h("sc-choice", { value: reason === null || reason === void 0 ? void 0 : reason.id, name: "reason", onScChange: e => {
        if (e.target.checked) {
          this.selectedReason = reason;
        }
      } }, reason === null || reason === void 0 ? void 0 : reason.label)));
  }
  render() {
    var _a, _b, _c;
    const { reasons_title, reasons_description, skip_link } = ((_a = this.protocol) === null || _a === void 0 ? void 0 : _a.preservation_locales) || {};
    if (this.loading) {
      return h("sc-skeleton", null);
    }
    return (h("div", { class: "cancel-survey" }, h("sc-dashboard-module", { heading: reasons_title, style: { '--sc-dashboard-module-spacing': '2em' } }, h("span", { slot: "description" }, reasons_description), h("sc-form", { onScSubmit: e => this.handleSubmit(e), style: { '--sc-form-row-spacing': '2em' } }, h("sc-choices", { showLabel: false, label: __('Choose a reason', 'surecart'), style: { '--columns': '2' }, required: true }, this.renderReasons()), ((_b = this.selectedReason) === null || _b === void 0 ? void 0 : _b.comment_enabled) && (h("sc-textarea", { label: ((_c = this.selectedReason) === null || _c === void 0 ? void 0 : _c.comment_prompt) || __('Additional Comments', 'surecart'), required: true, ref: el => (this.textArea = el), onScInput: e => (this.comment = e.target.value) })), h("sc-flex", { justifyContent: "flex-start" }, h("sc-button", { type: "primary", submit: true }, __('Continue', 'surecart'), h("sc-icon", { name: "arrow-right", slot: "suffix" })), !!skip_link && (h("sc-button", { class: "cancel-survey__abort-link", type: "text", onClick: () => this.scAbandon.emit() }, skip_link)))))));
  }
  static get watchers() { return {
    "selectedReason": ["handleSelectedReasonChange"]
  }; }
  static get style() { return scCancelSurveyCss; }
}, [1, "sc-cancel-survey", {
    "protocol": [16],
    "reasons": [1040],
    "loading": [32],
    "selectedReason": [32],
    "comment": [32],
    "error": [32]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-cancel-survey", "sc-alert", "sc-button", "sc-choice", "sc-choices", "sc-dashboard-module", "sc-flex", "sc-form", "sc-form-control", "sc-icon", "sc-skeleton", "sc-spinner", "sc-textarea", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-cancel-survey":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScCancelSurvey);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-choice":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-choices":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-form":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-skeleton":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-spinner":
      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 { ScCancelSurvey as S, defineCustomElement as d };
