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 { KeyboardManagerSlimmer } from '../../../../helpers/slimmers/keyboard-manager-slimmer';
import { MapToComponentField } from 'vega-slimmer/core';
/**
 * The `VegaDropdownKeyboardController` class is responsible for managing keyboard events
 * for a dropdown component.
 */
export class VegaDropdownKeyboardController extends KeyboardManagerSlimmer {
  /**
   * The constructor function sets up keyboard event listeners for a dropdown component.
   */
  constructor() {
    super((target) => {
      const dropdownRef = this.renderer.getDropdownRef();
      const virtualContainerRef = this.renderer.getVirtualContainerRef();
      return [
        {
          keyCode: 'Tab',
          activeElementInScope: [target.host],
          callback: target.showDropdownIfHover,
        },
        {
          keyCode: 'Escape',
          activeElementInScope: [target.host, dropdownRef, virtualContainerRef],
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: /* istanbul ignore next */ () => target.appenderController.hide(),
        },
        {
          keyCode: ['ArrowUp', 'ArrowDown'],
          activeElementInScope: [target.host, dropdownRef],
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: /* istanbul ignore next */ (event) => target.controlDirectionByKeyboard(event.code),
        },
        {
          keyCode: 'Tab',
          activeElementOutScope: [target.host, dropdownRef, virtualContainerRef],
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: /* istanbul ignore next */ () => target.isDropdownShow && target.appenderController.hide(),
        },
      ];
    });
    /* istanbul ignore next */
    /**
     * The below method is e2e-test covered in @see{module:vega-dropdown-keyboard-control-e2e}
     */
    this.showDropdownIfHover = async () => {
      if (this.trigger === 'hover') {
        this.appenderController.show();
      }
    };
    /* istanbul ignore next */
    /**
     * The below method is e2e-test covered in @see{module:vega-dropdown-keyboard-control-e2e}
     *
     * @param {string} code - current keyboard code.
     */
    this.controlDirectionByKeyboard = (code) => {
      var _a;
      (_a = this.renderer.getNextActiveItem(code)) === null || _a === void 0 ? void 0 : _a.shadowRoot.querySelector('li').focus();
    };
  }
}
__decorate([
  MapToComponentField()
], VegaDropdownKeyboardController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownKeyboardController.prototype, "trigger", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownKeyboardController.prototype, "isDropdownShow", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownKeyboardController.prototype, "renderer", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownKeyboardController.prototype, "appenderController", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownKeyboardController.prototype, "listRenderer", void 0);
