import { r as registerInstance, c as createEvent, h } from './index-f1d2a96f.js';
import { c as createOrUpdateOrder } from './index-78c1ff29.js';
import { o as openWormhole } from './consumer-7a54eb9f.js';
import './fetch-c0c608db.js';
import './index-0a61debd.js';
import './_commonjsHelpers-9943807e.js';
import './add-query-args-f4c5962b.js';

const ScCustomerName = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    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"]
  }; }
};
openWormhole(ScCustomerName, ['order', 'customer'], false);

export { ScCustomerName as sc_customer_name };
