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 { Fragment, h } from '@stencil/core';
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaChange } from '../../../../helpers/event-manager/event-id/vega-event-id';
export class VegaDropdownSearchRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.clear = () => {
      this.searchString = null;
      if (this.searchInputRef)
        this.searchInputRef.value = '';
    };
    this.onInputSearch = (node) => {
      if (node) {
        this.searchInputRef = node;
      }
      DomNodeSubjectObserverFactory.addUniqueObserverToNode(node, VegaChange, (input) => {
        this.search(input.detail, 'input');
      });
    };
    this.search = (input, searchTrigger) => {
      if (this.searchTriggerBy && this.searchTriggerBy !== searchTrigger) {
        return;
      }
      this.searchString = input;
      if (this.renderModeController.isSourceRenderMode()) {
        if (this.useDefaultFilter) {
          this.sourceController.setRenderSource();
          this.focus();
        }
        else if (this.isDropdownShow) {
          this.searchEventEmitter.emit(input);
          this.searchTriggerEventEmitter.emit({
            trigger: searchTrigger,
            value: input,
          });
        }
      }
      else {
        this.searchEventEmitter.emit(input);
        this.searchTriggerEventEmitter.emit({
          trigger: searchTrigger,
          value: input,
        });
      }
    };
  }
  userSearch(input) {
    this.search(input, 'input');
  }
  focus() {
    if (this.searchInputRef) {
      this.searchInputRef.shadowRoot.querySelector('input').focus();
    }
  }
  render() {
    return (this.searchable && (h(Fragment, null,
      h("div", { class: "vega-dropdown-search" },
        h("vega-input", { "prefix-icon": "search", size: "small", autoValidation: false, ref: this.onInputSearch, 
          /*
           * The below method is e2e-test covered in
           * @see{module:vega-dropdown-enter-key-trigger-search-e2e}
           */
          /* eslint-disable-next-line react/jsx-no-bind */
          onKeyDown: 
          /* istanbul ignore next */ (e) => {
            if (e.code === 'Enter') {
              this.search(this.searchInputRef.value, 'enter');
            }
          } })),
      h("vega-divider", { margin: { y: 'size-4' } }))));
  }
}
__decorate([
  MapToComponentField()
], VegaDropdownSearchRenderer.prototype, "searchable", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaDropdownSearchRenderer.prototype, "searchString", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchRenderer.prototype, "source", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchRenderer.prototype, "useDefaultFilter", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchRenderer.prototype, "searchEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchRenderer.prototype, "searchTriggerEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchRenderer.prototype, "isDropdownShow", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchRenderer.prototype, "sourceController", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchRenderer.prototype, "renderModeController", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownSearchRenderer.prototype, "searchTriggerBy", void 0);
__decorate([
  MapToComponentMethod('search')
], VegaDropdownSearchRenderer.prototype, "userSearch", null);
