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 } from 'vega-slimmer/core';
import { KeyboardManagerSlimmer } from '../../../../helpers/slimmers/keyboard-manager-slimmer';
import { getActiveElement } from '../../../../utils/component';
import { findParent } from '../../../../utils/ui';
/**
 * Keyboard controller for vega-combo-box
 */
export class VegaComboBoxKeyboardController extends KeyboardManagerSlimmer {
  /** The function sets up the functionality of key board control */
  constructor() {
    super((target) => [
      {
        keyCode: 'Backspace',
        activeElementInScope: [target.dropdownRenderer.getSearchInputRef()],
        eventName: 'keydown',
        callback: target.deleteLastSelectedItem,
      },
      {
        keyCode: 'ArrowLeft',
        activeElementInScope: [target.host],
        callback: 
        /* istanbul ignore next */
        /* eslint-disable jsdoc/require-jsdoc */
        () => target.controlDirectionByKeyboard('ArrowLeft'),
      },
      {
        keyCode: 'ArrowRight',
        activeElementInScope: [target.host],
        callback: 
        /* istanbul ignore next */
        /* eslint-disable jsdoc/require-jsdoc */
        () => target.controlDirectionByKeyboard('ArrowRight'),
      },
    ]);
    /*
     * The below method is e2e-test covered in
     * @see{module:vega-combo-box-keyboard-control-e2e}
     * @see{module:vega-combo-box-keyboard-control-for-selected-items-e2e}
     */
    /* istanbul ignore next */
    /**
     * Delete last item when press `Backspace` key
     */
    this.deleteLastSelectedItem = () => {
      if (this.dropdownRenderer.getSearchInputRef().value === '') {
        this.value.pop();
        this.valueController.setValue([...this.value]);
      }
    };
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-combo-box-keyboard-control-e2e}
   * @see{module:vega-combo-box-keyboard-control-for-selected-items-e2e}
   */
  /* istanbul ignore next */
  /**
   * Handles keyboard input to navigate between elements, focusing on the previous or next element based on the key pressed.
   *
   * @param {KeyboardCodeType} code - code
   */
  controlDirectionByKeyboard(code) {
    const activeElement = getActiveElement();
    if (activeElement && activeElement.classList.contains('vega-chip-close-icon')) {
      const currentChip = findParent(activeElement, 'vega-chip');
      if (code === 'ArrowLeft')
        this.focusItemByKeyboard(currentChip.previousSibling);
      if (code === 'ArrowRight')
        this.focusItemByKeyboard(currentChip.nextSibling);
    }
    else if (code === 'ArrowLeft' && this.dropdownRenderer.getSearchInputRef().selectionStart === 0) {
      this.focusItemByKeyboard(this.host.shadowRoot.querySelector('vega-chip:last-of-type'));
    }
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-combo-box-keyboard-control-e2e}
   * @see{module:vega-combo-box-keyboard-control-for-selected-items-e2e}
   */
  /* istanbul ignore next */
  /**
   * The function is used to focus on a specific element based on its class name.
   *
   * @param {HTMLElement} item - item
   */
  focusItemByKeyboard(item) {
    if (item) {
      if (item.classList.contains('vega-combo-box-input-container')) {
        item.querySelector('input').focus();
      }
      else {
        item.shadowRoot.querySelector('.vega-chip-close-icon').focus();
      }
    }
  }
}
__decorate([
  MapToComponentField()
], VegaComboBoxKeyboardController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaComboBoxKeyboardController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaComboBoxKeyboardController.prototype, "dropdownRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaComboBoxKeyboardController.prototype, "valueController", void 0);
