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 { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaDropdownClick, VegaDropdownHide, VegaDropdownSearch, VegaDropdownShow, VegaReset, VegaSearchTrigger, } from '../../../../helpers/event-manager/event-id/vega-event-id';
import { VegaInternalDropdownTemplateItemsChange } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
/** The `VegaInputSelectDropdownController` class is a TypeScript React component that controls the behavior of a dropdown select input. */
export class VegaInputSelectDropdownController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /** The `onDropdownShow` function is setting the `isDropdownShow` property of the`VegaInputSelectDropdownController` class to `true`. This function is called when the dropdown select input is shown. */
    this.onDropdownShow = () => {
      this.isDropdownShow = true;
    };
    /** The `onDropdownHide` function is setting the `isDropdownShow` property of the `VegaInputSelectDropdownController` class to `false`. This function is called when the dropdown select input is hidden. */
    this.onDropdownHide = () => {
      this.isDropdownShow = false;
    };
  }
  /**
   * The function `addDropdownObserver` adds event observers to a dropdown element.
   *
   * @param {HTMLElement} dropdownRef - The `dropdownRef` parameter is a reference to the HTML element representing the dropdown component.
   */
  addDropdownObserver(dropdownRef) {
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(dropdownRef, VegaDropdownShow, (payload) => {
      this.onDropdownShow();
      this.showEventEmitter.emit(payload.detail);
    });
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(dropdownRef, VegaDropdownHide, (payload) => {
      this.onDropdownHide();
      this.hideEventEmitter.emit(payload.detail);
    });
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(dropdownRef, VegaDropdownClick, async (payload) => {
      this.vegaInputSelectValueController.setValue(await payload.host.getSelection());
    });
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(dropdownRef, VegaDropdownSearch, (payload) => {
      this.searchEventEmitter.emit(payload.detail);
    });
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(dropdownRef, VegaSearchTrigger, (payload) => {
      this.searchTriggerEventEmitter.emit(payload.detail);
    });
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(dropdownRef, VegaInternalDropdownTemplateItemsChange, 
    /*
     * The below method is e2e-test covered in
     * @see{module:vega-input-select-template-single-select-e2e}
     * @see{module:vega-input-select-template-multiple-select-e2e}
     */
    /* istanbul ignore next */
    (payload) => {
      const addedItems = Array.from(payload)
        .filter((item) => !this.source.find((sourceItem) => sourceItem.id === item.itemKey))
        .map((item) => ({
        id: item.itemKey,
        displayName: item.label,
      }));
      this.sourceController.appendSourceItems(addedItems);
    });
    DomNodeSubjectObserverFactory.addUniqueObserverToNode(dropdownRef, VegaReset, async () => {
      this.vegaInputSelectValueController.setValue(this.selectType === 'multiple' ? [] : '');
    });
  }
}
__decorate([
  MapToComponentField({ writable: true })
], VegaInputSelectDropdownController.prototype, "isDropdownShow", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectDropdownController.prototype, "vegaSearch", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectDropdownController.prototype, "searchEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectDropdownController.prototype, "searchTriggerEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectDropdownController.prototype, "vegaInputSelectValueController", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectDropdownController.prototype, "sourceController", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectDropdownController.prototype, "source", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectDropdownController.prototype, "showEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectDropdownController.prototype, "hideEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectDropdownController.prototype, "selectType", void 0);
