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';
/** Provides keyboard control functionality for vega-radio. */
export class VegaRadioGroupKeyboardController extends KeyboardManagerSlimmer {
  /**
   * The constructor function sets up keyboard event listeners for a radio component.
   */
  constructor() {
    /* istanbul ignore next */
    super((target) => {
      target.setRadioArrayTabIndex();
      return [
        {
          keyCode: ['ArrowUp', 'ArrowDown'],
          activeElementInScope: [target.host],
          /**
           * switch the element focus when keyboard press
           *
           * @param {KeyboardEvent} event event object
           */
          callback: /* istanbul ignore next */ (event) => {
            this.updateRadioFocusStates(event);
          },
        },
      ];
    });
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-radio-group-keyboard-control-e2e}
   */
  /* istanbul ignore next */
  /**
   * It dynamically sets the value of the element tabIndex.
   */
  setRadioArrayTabIndex() {
    const radioArray = this.getNotDisabledRadioArray();
    if (radioArray) {
      const checkedItemIndex = radioArray.findIndex((radio) => radio.checked);
      const defaultFocusIndex = checkedItemIndex > -1 ? checkedItemIndex : 0;
      radioArray.forEach((radio, index) => {
        const tabIndex = index === defaultFocusIndex ? '0' : '-1';
        radio.shadowRoot.querySelector('input').setAttribute('tabIndex', tabIndex);
      });
    }
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-radio-group-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 key code up or down
   */
  updateRadioFocusStates(event) {
    const keyCode = event.key;
    const radioArray = this.getNotDisabledRadioArray();
    if (!radioArray || radioArray.length === 0)
      return;
    radioArray.forEach((item) => item.shadowRoot.querySelector('input').setAttribute('tabIndex', '-1'));
    const activeIndex = radioArray.findIndex((radio) => radio === event.target);
    let nextActiveElement;
    if (keyCode === 'ArrowDown') {
      nextActiveElement =
        activeIndex === radioArray.length - 1 ? radioArray[0] : radioArray[activeIndex + 1];
    }
    else if (keyCode === 'ArrowUp') {
      nextActiveElement =
        activeIndex === 0 ? radioArray[radioArray.length - 1] : radioArray[activeIndex - 1];
    }
    if (nextActiveElement) {
      nextActiveElement.shadowRoot.querySelector('input').focus();
      nextActiveElement.checked = true;
      nextActiveElement.shadowRoot.querySelector('input').tabIndex = 0;
    }
  }
  /**
   * filter out disabled elements.
   *
   * @returns {Nullable<HTMLVegaRadioElement[]> } radios element
   */
  getNotDisabledRadioArray() {
    const radios = Array.from(this.host.querySelectorAll('vega-radio'));
    if (radios.length > 0) {
      return radios.filter((item) => !item.disabled);
    }
  }
}
__decorate([
  MapToComponentField()
], VegaRadioGroupKeyboardController.prototype, "host", void 0);
