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, VegaSlimmer } from 'vega-slimmer/core';
import { checkIsVoiceOverClick, getFirstFocusableElement } from '../../../../../utils/ui';
/**
 * Controller responsible for handling VoiceOver focus transfer to the active tab panel.
 */
export class VegaTabGroupVoiceOverFocusController extends VegaSlimmer {
  /**
   * If the triggering click is a VoiceOver click, moves focus to the first focusable element
   * inside the panel identified by `dataTarget`, or to the panel itself when no focusable child exists.
   *
   * @param {MouseEvent} event - The click event to inspect.
   * @param {string} dataTarget - The id attribute of the target vega-tab-group-panel element.
   * @returns {void}
   */
  handleVoiceOverPanelFocus(event, dataTarget) {
    if (!checkIsVoiceOverClick(event)) {
      return;
    }
    requestAnimationFrame(() => {
      const activePanel = this.findPanelById(dataTarget);
      const focusTarget = (activePanel && getFirstFocusableElement(activePanel)) || activePanel;
      focusTarget === null || focusTarget === void 0 ? void 0 : focusTarget.focus();
    });
  }
  /* eslint-disable jsdoc/no-undefined-types */
  /**
   * Finds a vega-tab-group-panel element whose id attribute matches `dataTarget`.
   *
   * @param {string} dataTarget - The id to search for.
   * @returns {Nullable<HTMLVegaTabGroupPanelElement>} The matching panel, or null/undefined when not found.
   */
  findPanelById(dataTarget) {
    /* eslint-enable jsdoc/no-undefined-types */
    return Array.from(this.host.querySelectorAll('vega-tab-group-panel')).find((panel) => (panel === null || panel === void 0 ? void 0 : panel.getAttribute('id')) === dataTarget);
  }
}
__decorate([
  MapToComponentField()
], VegaTabGroupVoiceOverFocusController.prototype, "host", void 0);
