'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-cd6fab62.js');
const fetch = require('./fetch-a8709225.js');
const consumer = require('./consumer-1372755b.js');
const defaultI18n = require('./default-i18n-072e75d7.js');
require('./index-20b57ec7.js');
require('./add-query-args-17c551b6.js');
require('./_commonjsHelpers-537d719a.js');

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

const ScCustomOrderPriceInput = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.scUpdateLineItem = index.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 fetch.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 (index.h("sc-alert", { type: "danger", open: true, style: { margin: '0px' } }, defaultI18n.__('This price has been archived.', 'surecart')));
    }
    return index.h(index.Host, { style: { display: 'none' } });
  }
  render() {
    var _a, _b, _c, _d, _e, _f;
    if (this.loading || this.fetching) {
      return (index.h("div", null, index.h("sc-skeleton", { style: { width: '20%', marginBottom: '0.75em' } }), index.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 (index.h("div", { class: "sc-custom-order-price-input" }, index.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 && index.h("sc-block-ui", { style: { zIndex: '9' } })));
  }
  static get watchers() { return {
    "lineItems": ["handleLineItemsChange"]
  }; }
};
consumer.openWormhole(ScCustomOrderPriceInput, ['busy', 'lineItems'], false);
ScCustomOrderPriceInput.style = scCustomOrderPriceInputCss;

exports.sc_custom_order_price_input = ScCustomOrderPriceInput;
