'use strict';

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

const index = require('./index-cd6fab62.js');
const consumer = require('./consumer-1372755b.js');
const index$1 = require('./index-c1a1f929.js');
require('./fetch-a8709225.js');
require('./index-20b57ec7.js');
require('./_commonjsHelpers-537d719a.js');
require('./add-query-args-17c551b6.js');

const scCustomerEmailCss = "sc-customer-email{display:block}";

const ScCustomerEmail = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.scChange = index.createEvent(this, "scChange", 7);
    this.scClear = index.createEvent(this, "scClear", 7);
    this.scInput = index.createEvent(this, "scInput", 7);
    this.scFocus = index.createEvent(this, "scFocus", 7);
    this.scBlur = index.createEvent(this, "scBlur", 7);
    this.scUpdateOrderState = index.createEvent(this, "scUpdateOrderState", 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 handleChange() {
    var _a;
    this.value = this.input.value;
    this.scChange.emit();
    // update order state.
    try {
      const order = (await index$1.createOrUpdateOrder({ id: (_a = this.order) === null || _a === void 0 ? void 0 : _a.id, data: { email: 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.email) {
      if (val.email !== this.value) {
        this.value = val.email;
      }
    }
  }
  async reportValidity() {
    return this.input.reportValidity();
  }
  render() {
    var _a;
    return (index.h("sc-input", { type: "email", name: "email", ref: el => (this.input = el), value: ((_a = this.customer) === null || _a === void 0 ? void 0 : _a.email) || this.value, label: this.label, help: this.help, autocomplete: 'email', placeholder: this.placeholder, disabled: !!this.loggedIn, readonly: this.readonly, required: true, 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"]
  }; }
};
consumer.openWormhole(ScCustomerEmail, ['order', 'customer', 'loggedIn'], false);
ScCustomerEmail.style = scCustomerEmailCss;

exports.sc_customer_email = ScCustomerEmail;
