var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { h } from '@stencil/core';
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import changeManager from '../../../../helpers/change-manager/change-manager';
import domNodeSubjectFactory from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalFocusVegaInput, VegaInternalSetupVegaInputMaskConfig, VegaInternalUpdateVegaInputValue, } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
export class VegaInputInputRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.setInputRef = (inputRef) => {
      if (inputRef && this.inputRef !== inputRef) {
        this.inputRef = inputRef;
        this.notifySetupMaskConfig();
      }
    };
    this.handleInputFocus = (e) => {
      this.focusEventEmitter.emit(undefined, e);
    };
    this.handleInputBlur = (e) => {
      this.blurEventEmitter.emit(undefined, e);
    };
    this.handleInputPaste = (e) => {
      const event = this.pasteEventEmitter.emit(e.clipboardData);
      /* istanbul ignore next */
      if (event.defaultPrevented) {
        e.preventDefault();
      }
    };
    /**
     * Handles input event and updates the value based on a specified trigger.
     * The below method is e2e-test covered in
     * @see{module:vega-input-on-value-input}
     */
    /* istanbul ignore next */
    this.onInputHandler = () => {
      if (this.valueUpdateTrigger !== 'change') {
        this.notifyInputValueChange();
      }
    };
    /**
     * Handles input change events and updates the value based on a specified trigger.
     * The below method is e2e-test covered in
     * @see{module:vega-input-on-value-change}
     */
    /* istanbul ignore next */
    this.inputChangeHandler = () => {
      if (this.valueUpdateTrigger === 'change') {
        this.notifyInputValueChange();
      }
    };
  }
  connectedCallback() {
    this.initInputFocusObserver();
  }
  disconnectedCallback() {
    this.removeInputFocusObserver();
  }
  doFocus() {
    this.focusInput();
  }
  doBlur() {
    this.inputRef.blur();
  }
  getInputRef() {
    return this.inputRef;
  }
  render() {
    return (h("div", { class: "vega-input-input" },
      h("input", Object.assign({ part: "input", name: this.name, type: this.getInputType(), autoComplete: this.autocomplete, inputmode: this.inputmode, enterkeyhint: this.enterkeyhint, readOnly: this.readOnly, value: this.value, ref: this.setInputRef, placeholder: this.placeholder, disabled: this.disabled, minLength: this.minLength, maxLength: this.maxLength, 
        /* eslint-disable-next-line react/jsx-no-bind */
        onBlur: (e) => this.handleInputBlur(e), 
        /* eslint-disable-next-line react/jsx-no-bind */
        onFocus: (e) => this.handleInputFocus(e), onChange: this.inputChangeHandler, onInput: this.onInputHandler, 
        /* eslint-disable-next-line react/jsx-no-bind */
        onPaste: (e) => this.handleInputPaste(e) }, this.copyPasteController.getCopyPasteProps()))));
  }
  getInputType() {
    switch (this.type) {
      case 'number':
        return 'text';
      case 'password':
        return this.isPasswordDisplay ? 'text' : 'password';
      default:
        return this.type;
    }
  }
  /** Notify to update current period */
  notifyInputValueChange() {
    if (this.inputRef.value !== this.value) {
      this.nativeInputChangeEventEmitter.emit(this.inputRef.value);
    }
    changeManager.notify(domNodeSubjectFactory.getSubject(this.host, VegaInternalUpdateVegaInputValue), this.inputRef.value);
  }
  notifySetupMaskConfig() {
    changeManager.notify(domNodeSubjectFactory.getSubject(this.host, VegaInternalSetupVegaInputMaskConfig), {});
  }
  /**
   * Initialize calendar clear observer
   */
  initInputFocusObserver() {
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(this.host, VegaInternalFocusVegaInput, this.focusInput.bind(this));
  }
  /**
   * Remove calendar clear observer
   */
  removeInputFocusObserver() {
    DomNodeSubjectObserverFactory.removeUniqueObserverFromNode(this.host, VegaInternalFocusVegaInput);
  }
  focusInput() {
    this.inputRef.focus();
  }
}
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "disabled", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "type", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "name", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "autocomplete", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "inputmode", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "enterkeyhint", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "readOnly", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "placeholder", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "minLength", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "maxLength", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "isPasswordDisplay", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "valueUpdateTrigger", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "focusEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "blurEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "pasteEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "copyPasteController", void 0);
__decorate([
  MapToComponentField()
], VegaInputInputRenderer.prototype, "nativeInputChangeEventEmitter", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaInputInputRenderer.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaInputInputRenderer.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('doFocus')
], VegaInputInputRenderer.prototype, "doFocus", null);
__decorate([
  MapToComponentMethod('doBlur')
], VegaInputInputRenderer.prototype, "doBlur", null);
