import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { c as createOrUpdateOrder } from './index3.js';
import { o as openWormhole } from './consumer.js';
import { d as defineCustomElement$4 } from './sc-form-control2.js';
import { d as defineCustomElement$3 } from './sc-input2.js';
import { d as defineCustomElement$2 } from './sc-tooltip2.js';

const ScCustomerName$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.scChange = createEvent(this, "scChange", 7);
    this.scUpdateOrderState = createEvent(this, "scUpdateOrderState", 7);
    this.scClear = createEvent(this, "scClear", 7);
    this.scInput = createEvent(this, "scInput", 7);
    this.scFocus = createEvent(this, "scFocus", 7);
    this.scBlur = createEvent(this, "scBlur", 7);
    this.scUpdateCustomer = createEvent(this, "scUpdateCustomer", 7);
    /** The input's size. */
    this.size = 'medium';
    /** The input's value attribute. */
    this.value = '';
    /** Draws a pill-style input with rounded edges. */
    this.pill = false;
    /** Should we show the label */
    this.showLabel = true;
    /** The input's help text. */
    this.help = '';
    /** Disables the input. */
    this.disabled = false;
    /** Makes the input readonly. */
    this.readonly = false;
    /** Makes the input a required field. */
    this.required = false;
    /**
     * This will be true when the control is in an invalid state. Validity is determined by props such as `type`,
     * `required`, `minlength`, `maxlength`, and `pattern` using the browser's constraint validation API.
     */
    this.invalid = false;
  }
  async reportValidity() {
    return this.input.reportValidity();
  }
  async handleChange() {
    var _a;
    this.value = this.input.value;
    // update order.
    try {
      const order = await createOrUpdateOrder({ id: (_a = this.order) === null || _a === void 0 ? void 0 : _a.id, data: { name: this.input.value } });
      this.scUpdateOrderState.emit(order);
    }
    catch (error) {
      console.error(error);
    }
  }
  /** Sync customer email with session if it's updated by other means */
  handleSessionChange(val) {
    if (val === null || val === void 0 ? void 0 : val.name) {
      if (val.name !== this.value) {
        this.value = val === null || val === void 0 ? void 0 : val.name;
      }
    }
  }
  render() {
    var _a;
    return (h("sc-input", { type: "text", name: "name", ref: el => (this.input = el), value: ((_a = this.customer) === null || _a === void 0 ? void 0 : _a.name) || this.value, disabled: !!this.loggedIn, label: this.label, help: this.help, autocomplete: "name", placeholder: this.placeholder, readonly: this.readonly, required: this.required, invalid: this.invalid, autofocus: this.autofocus, hasFocus: this.hasFocus, onScChange: () => this.handleChange(), onScInput: () => this.scInput.emit(), onScFocus: () => this.scFocus.emit(), onScBlur: () => this.scBlur.emit() }));
  }
  static get watchers() { return {
    "order": ["handleSessionChange"]
  }; }
}, [1, "sc-customer-name", {
    "loggedIn": [4, "logged-in"],
    "order": [16],
    "customer": [16],
    "size": [513],
    "value": [1025],
    "pill": [516],
    "label": [1],
    "showLabel": [4, "show-label"],
    "help": [1],
    "placeholder": [1],
    "disabled": [516],
    "readonly": [516],
    "required": [516],
    "invalid": [1540],
    "autofocus": [4],
    "hasFocus": [1540, "has-focus"],
    "reportValidity": [64]
  }]);
openWormhole(ScCustomerName$1, ['order', 'customer'], false);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-customer-name", "sc-form-control", "sc-input", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-customer-name":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScCustomerName$1);
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-input":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScCustomerName = ScCustomerName$1;
const defineCustomElement = defineCustomElement$1;

export { ScCustomerName, defineCustomElement };
