import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
import { a as apiFetch } from './fetch.js';
import { o as openWormhole } from './consumer.js';
import { _ as __ } from './default-i18n.js';
import './index2.js';
import { d as defineCustomElement$a } from './sc-alert2.js';
import { d as defineCustomElement$9 } from './sc-block-ui2.js';
import { d as defineCustomElement$8 } from './sc-form-control2.js';
import { d as defineCustomElement$7 } from './sc-icon2.js';
import { d as defineCustomElement$6 } from './sc-input2.js';
import { d as defineCustomElement$5 } from './sc-price-input2.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-tooltip2.js';

const scCustomOrderPriceInputCss = "sc-custom-order-price-input{display:block}";

const ScCustomOrderPriceInput$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.scUpdateLineItem = createEvent(this, "scUpdateLineItem", 7);
    /** Is this loading */
    this.loading = false;
    /** Is this busy */
    this.busy = false;
    /** Label for the choice. */
    this.lineItems = [];
    /** Internal fetching state. */
    this.fetching = false;
  }
  handleBlur(e) {
    var _a;
    const ad_hoc_amount = parseInt(e.target.value);
    if (isNaN(ad_hoc_amount))
      return;
    if (((_a = this.lineItem) === null || _a === void 0 ? void 0 : _a.ad_hoc_amount) === ad_hoc_amount)
      return;
    this.scUpdateLineItem.emit({ price_id: this.priceId, quantity: 1, ad_hoc_amount });
  }
  /** Store current line item in state. */
  handleLineItemsChange() {
    var _a;
    if (!((_a = this.lineItems) === null || _a === void 0 ? void 0 : _a.length))
      return;
    this.lineItem = (this.lineItems || []).find(lineItem => lineItem.price.id === this.priceId);
  }
  componentDidLoad() {
    if (!this.price) {
      this.fetchPrice();
    }
  }
  /** Fetch prices and products */
  async fetchPrice() {
    if (!this.priceId)
      return;
    try {
      this.fetching = true;
      this.price = (await apiFetch({
        path: `surecart/v1/prices/${this.priceId}`,
      }));
    }
    catch (err) {
    }
    finally {
      this.fetching = false;
    }
  }
  renderEmpty() {
    var _a;
    if ((_a = window === null || window === void 0 ? void 0 : window.wp) === null || _a === void 0 ? void 0 : _a.blocks) {
      return (h("sc-alert", { type: "danger", open: true, style: { margin: '0px' } }, __('This price has been archived.', 'surecart')));
    }
    return h(Host, { style: { display: 'none' } });
  }
  render() {
    var _a, _b, _c, _d, _e, _f;
    if (this.loading || this.fetching) {
      return (h("div", null, h("sc-skeleton", { style: { width: '20%', marginBottom: '0.75em' } }), h("sc-skeleton", { style: { width: '100%' } })));
    }
    // Price needs to be active.
    if (!((_a = this === null || this === void 0 ? void 0 : this.price) === null || _a === void 0 ? void 0 : _a.id) || ((_b = this.price) === null || _b === void 0 ? void 0 : _b.archived))
      return this.renderEmpty();
    return (h("div", { class: "sc-custom-order-price-input" }, h("sc-price-input", { "currency-code": ((_c = this.price) === null || _c === void 0 ? void 0 : _c.currency) || 'usd', label: this.label, min: (_d = this === null || this === void 0 ? void 0 : this.price) === null || _d === void 0 ? void 0 : _d.ad_hoc_min_amount, max: (_e = this === null || this === void 0 ? void 0 : this.price) === null || _e === void 0 ? void 0 : _e.ad_hoc_max_amount, placeholder: this.placeholder, required: this.required, value: (_f = this.lineItem) === null || _f === void 0 ? void 0 : _f.ad_hoc_amount.toString(), "show-code": this.showCode, help: this.help }), this.busy && h("sc-block-ui", { style: { zIndex: '9' } })));
  }
  static get watchers() { return {
    "lineItems": ["handleLineItemsChange"]
  }; }
  static get style() { return scCustomOrderPriceInputCss; }
}, [0, "sc-custom-order-price-input", {
    "priceId": [513, "price-id"],
    "price": [1040],
    "loading": [4],
    "busy": [4],
    "label": [1],
    "placeholder": [1],
    "required": [4],
    "help": [1],
    "showCode": [516, "show-code"],
    "lineItems": [16],
    "fetching": [32],
    "lineItem": [32]
  }, [[0, "scBlur", "handleBlur"]]]);
openWormhole(ScCustomOrderPriceInput$1, ['busy', 'lineItems'], false);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-custom-order-price-input", "sc-alert", "sc-block-ui", "sc-form-control", "sc-icon", "sc-input", "sc-price-input", "sc-skeleton", "sc-spinner", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-custom-order-price-input":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScCustomOrderPriceInput$1);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-input":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-price-input":
      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-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScCustomOrderPriceInput = ScCustomOrderPriceInput$1;
const defineCustomElement = defineCustomElement$1;

export { ScCustomOrderPriceInput, defineCustomElement };
