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';
import { getActiveElement } from '../../../../../utils/component';
import { findParent } from '../../../../../utils/ui';
import { CalendarDate } from '../../../../../helpers/calendar';
import { tryGetDocument } from '../../../../../utils/try-get-document';
/* istanbul ignore next */
/**
 * The `VegaCalendarKeyboardController` class is responsible for managing keyboard events
 * for a calendar component.
 */
export class VegaCalendarKeyboardController extends KeyboardManagerSlimmer {
  /**
   * The constructor function sets up keyboard event listeners for a calendar component.
   */
  constructor() {
    super((target) => {
      return [
        {
          keyCode: ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'],
          // eslint-disable-next-line jsdoc/require-jsdoc
          activeElementInScope: () => [
            target.monthViewRenderer.getMonthContainerRef(),
          ],
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: (event) => target.onDaysDirectionKeyDown(event),
        },
        {
          keyCode: ['ArrowUp', 'ArrowDown'],
          // eslint-disable-next-line jsdoc/require-jsdoc
          activeElementInScope: () => [
            target.monthEventRenderer.getMoreEventsBoxRef(),
          ],
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: (event) => target.onEventsDirectionKeyDown(event),
        },
        {
          keyCode: ['Enter'],
          // eslint-disable-next-line jsdoc/require-jsdoc
          activeElementInScope: () => [
            target.monthViewRenderer.getMonthContainerRef(),
          ],
          eventName: 'keydown',
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: (event) => target.onDayEnter(event),
        },
      ];
    });
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-calendar-day-click-keyboard-e2e}
   */
  /**
   * It handles the keyboard event when the user presses the Enter key on a day cell in the month view.
   *
   * @param {KeyboardEvent} event - The keyboard event object.
   */
  onDayEnter(event) {
    const target = event.composedPath()[0];
    if (target.classList.contains('grid-col') && target.dataset['date'] && this.showSwitchView) {
      const date = CalendarDate.fromString(target.dataset['date']);
      date && this.monthViewRenderer.onDayItemKeyDown(event, date);
    }
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-calendar-event-direction-switch-keyboard-e2e}
   */
  /**
   * It handles the keyboard event when the user presses the Arrow Up or Arrow Down key on an event item in the more events popover.
   *
   * @param {KeyboardEvent} event - The keyboard event object.
   */
  onEventsDirectionKeyDown(event) {
    const document = tryGetDocument();
    const currentItem = document === null || document === void 0 ? void 0 : document.activeElement;
    if (!currentItem)
      return;
    const boxRef = this.monthEventRenderer.getMoreEventsBoxRef();
    if (!boxRef)
      return;
    const items = Array.from(boxRef.querySelectorAll('vega-calendar-event'));
    const currentIndex = items.findIndex((item) => item === currentItem);
    const nextIndex = event.code === 'ArrowDown' ? currentIndex + 1 : currentIndex - 1;
    if (nextIndex >= 0 && nextIndex < items.length) {
      items[nextIndex].focus();
    }
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-calendar-day-direction-switch-keyboard-e2e}
   */
  /**
   * It handles the keyboard event when the user presses the Arrow keys on a day cell in the month view to navigate between days.
   *
   * @param {KeyboardEvent} event - The keyboard event object.
   */
  onDaysDirectionKeyDown(event) {
    const activeElement = getActiveElement(this.host.shadowRoot);
    if (!activeElement)
      return;
    const currentItem = findParent(activeElement, '.row-cell');
    if (!currentItem)
      return;
    const container = this.monthViewRenderer.getMonthContainerRef();
    if (!container)
      return;
    const items = Array.from(container.querySelectorAll('.row-cell'));
    const currentIndex = items.findIndex((item) => item === currentItem);
    let nextIndex = -1;
    const verticalStep = this.showWeekends ? 7 : 5;
    switch (event.code) {
      case 'ArrowRight':
        nextIndex = currentIndex + 1;
        break;
      case 'ArrowLeft':
        nextIndex = currentIndex - 1;
        break;
      case 'ArrowDown':
        nextIndex = currentIndex + verticalStep;
        break;
      case 'ArrowUp':
        nextIndex = currentIndex - verticalStep;
        break;
      default:
        break;
    }
    if (nextIndex >= 0 && nextIndex < items.length) {
      items[nextIndex].querySelector('.grid-col').focus();
    }
  }
}
__decorate([
  MapToComponentField()
], VegaCalendarKeyboardController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarKeyboardController.prototype, "showWeekends", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarKeyboardController.prototype, "monthViewRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarKeyboardController.prototype, "monthEventRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarKeyboardController.prototype, "showSwitchView", void 0);
