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';
/** The `VegaTimePickerKeyboardController` class is a TypeScript class that extends the `KeyboardManagerSlimmer` class and provides keyboard control functionality for a time picker component. */
export class VegaTimePickerKeyboardController extends KeyboardManagerSlimmer {
  /**
   * The constructor function sets up keyboard event listeners for a time picker component.
   */
  constructor() {
    /*
     * The below method is e2e-test covered in
     * @see{module:vega-time-picker-keyboard-control-e2e}
     */
    /* istanbul ignore next */
    super((target) => {
      const dropdownRef = target.singleDropdownRenderer.getDropdownRef();
      const startDropdownRef = target.rangeDropdownRenderer.getStartDropdownRef();
      const endDropdownRef = target.rangeDropdownRenderer.getEndDropdownRef();
      return [
        {
          keyCode: 'Enter',
          activeElementInScope: [target.host],
          callback: target.componentBlurAndHideDropdown,
        },
        {
          keyCode: ['ArrowUp', 'ArrowDown'],
          activeElementInScope: [dropdownRef, startDropdownRef, endDropdownRef],
          /**
           * switch the element focus when keyboard press
           *
           * @param {KeyboardEvent} event event object
           */
          callback: /* istanbul ignore next */ (event) => {
            target.switchHourOrMinuteFocus(event);
          },
        },
        {
          keyCode: 'Escape',
          activeElementInScope: [target.host, dropdownRef, startDropdownRef, endDropdownRef],
          callback: target.componentBlurAndHideDropdown,
        },
      ];
    });
    /* istanbul ignore next */
    /**
     * It is an asynchronous method that is used to blur the input field and hide the dropdown of the time picker component.
     */
    this.componentBlurAndHideDropdown = async () => {
      if (this.mode === 'single') {
        await this.singleInputRenderer.getInputRef().doBlur();
        this.singleDropdownController.hide();
      }
      else {
        await this.rangeInputRenderer.getStartInputRef().doBlur();
        await this.rangeInputRenderer.getEndInputRef().doBlur();
        this.startDropdownController.hide();
        this.endDropdownController.hide();
      }
    };
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-time-picker-keyboard-up-down-control-e2e}
   */
  /* istanbul ignore next */
  /**
   * It is used to switch the focus between different elements based on the arrow key pressed.
   *
   * @param {KeyboardEvent} event the event of target
   */
  switchHourOrMinuteFocus(event) {
    const keyCode = event.key;
    const activeElement = event.composedPath()[0];
    let nextActiveElement = null;
    if (activeElement.tagName === 'LI' && activeElement.getAttribute('role') === 'menuitem') {
      nextActiveElement =
        keyCode === 'ArrowDown'
          ? activeElement.nextElementSibling
          : activeElement.previousElementSibling;
    }
    if (nextActiveElement) {
      nextActiveElement.focus();
    }
  }
}
__decorate([
  MapToComponentField()
], VegaTimePickerKeyboardController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerKeyboardController.prototype, "mode", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerKeyboardController.prototype, "singleInputRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerKeyboardController.prototype, "singleDropdownRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerKeyboardController.prototype, "rangeDropdownRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerKeyboardController.prototype, "rangeInputRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerKeyboardController.prototype, "singleDropdownController", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerKeyboardController.prototype, "startDropdownController", void 0);
__decorate([
  MapToComponentField()
], VegaTimePickerKeyboardController.prototype, "endDropdownController", void 0);
