import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { a as apiFetch } from './fetch.js';
import { g as getHumanDiscount } from './price.js';
import { d as defineCustomElement$7 } from './sc-alert2.js';
import { d as defineCustomElement$6 } from './sc-block-ui2.js';
import { d as defineCustomElement$5 } from './sc-button2.js';
import { d as defineCustomElement$4 } from './sc-dashboard-module2.js';
import { d as defineCustomElement$3 } from './sc-flex2.js';
import { d as defineCustomElement$2 } from './sc-icon2.js';
import { d as defineCustomElement$1 } from './sc-spinner2.js';
import { a as addQueryArgs } from './add-query-args.js';

const replaceAmount = (string, replace, name = 'amount') => {
  return string.replaceAll('{{' + name + '}}', replace).replaceAll('{{ ' + name + ' }}', replace);
};

const scCancelDiscountCss = ".cancel-discount__abort-link{color:var(--sc-color-gray-500)}";

const ScCancelDiscount = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.scCancel = createEvent(this, "scCancel", 7);
    this.scPreserved = createEvent(this, "scPreserved", 7);
  }
  replaceAmount(string) {
    var _a, _b;
    if (!((_a = this.protocol) === null || _a === void 0 ? void 0 : _a.preservation_coupon)) {
      return string;
    }
    return replaceAmount(string, getHumanDiscount((_b = this.protocol) === null || _b === void 0 ? void 0 : _b.preservation_coupon));
  }
  async addDiscount() {
    var _a, _b;
    try {
      this.loading = true;
      this.subscription = (await apiFetch({
        method: 'PATCH',
        path: addQueryArgs(`surecart/v1/subscriptions/${(_a = this.subscription) === null || _a === void 0 ? void 0 : _a.id}/preserve`, {
          cancellation_act: {
            ...(!!this.comment ? { comment: this.comment } : {}),
            cancellation_reason_id: (_b = this.reason) === null || _b === void 0 ? void 0 : _b.id,
          },
        }),
      }));
      this.scPreserved.emit();
    }
    catch (e) {
      console.error(e);
      this.error = e;
    }
    finally {
      this.loading = false;
    }
  }
  render() {
    var _a;
    const { preserve_title, preserve_description, preserve_button, cancel_link } = ((_a = this.protocol) === null || _a === void 0 ? void 0 : _a.preservation_locales) || {};
    return (h("div", { class: "cancel-discount" }, h("sc-dashboard-module", { heading: this.replaceAmount(preserve_title), style: { '--sc-dashboard-module-spacing': '2em' } }, h("span", { slot: "description" }, this.replaceAmount(preserve_description)), h("sc-flex", { justifyContent: "flex-start" }, h("sc-button", { type: "primary", onClick: () => this.addDiscount() }, preserve_button), h("sc-button", { class: "cancel-discount__abort-link", type: "text", onClick: () => this.scCancel.emit() }, cancel_link)), !!this.loading && h("sc-block-ui", { spinner: true }))));
  }
  static get style() { return scCancelDiscountCss; }
}, [1, "sc-cancel-discount", {
    "subscription": [16],
    "reason": [16],
    "comment": [1],
    "protocol": [16],
    "loading": [32],
    "error": [32]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-cancel-discount", "sc-alert", "sc-block-ui", "sc-button", "sc-dashboard-module", "sc-flex", "sc-icon", "sc-spinner"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-cancel-discount":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScCancelDiscount);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}
defineCustomElement();

export { ScCancelDiscount as S, defineCustomElement as d };
