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 changeManager from '../../../../../helpers/change-manager/change-manager';
import { Observer } from '../../../../../helpers/change-manager/observer/observer';
import { TAB_PANEL_ADD_OR_DEL_TO_GROUP } from '../../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { findParent } from '../../../../../utils/ui';
/** The `VegaTabGroupPanelController` class is responsible for managing the panels within a tab group component, including initializing the panels, setting the default active panel, and updating the active panel based on user selection. */
export class VegaTabGroupPanelController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.tabPanels = [];
    /**
     * It is used to handle updates to the tab panels within the tab group component.
     *
     * @param {PanelUpdatePayload} payload the panel element and the action type
     */
    this.handlePanelUpdate = (payload) => {
      const { host, type } = payload;
      if (type === 'ADD') {
        this.tabPanels.push(host);
      }
      else {
        this.tabPanels = this.tabPanels.filter((panel) => {
          return panel !== host;
        });
      }
    };
  }
  /**
   * register the observer when component connected
   */
  connectedCallback() {
    this.tabPanelUpdateObserver = new Observer(this.canAcceptPanelUpdate.bind(this), this.handlePanelUpdate);
    changeManager.register(TAB_PANEL_ADD_OR_DEL_TO_GROUP, this.tabPanelUpdateObserver);
  }
  /**
   * un-register the observer when component remove, clear the tab panel map
   */
  disconnectedCallback() {
    changeManager.unregister(TAB_PANEL_ADD_OR_DEL_TO_GROUP, this.tabPanelUpdateObserver);
    this.tabPanels = [];
  }
  /**
   * initial the select tab data target after tap panel render
   */
  componentDidLoad() {
    this.initSelectedTabDataTarget();
  }
  /**
   * update the panels status when component call watchSelectedTabDataTargetChange method
   */
  watchSelectedTabDataTargetChange() {
    this.updateTabPanelsStatus(this.selectedTabDataTarget);
  }
  /**
   * The function `canAcceptPanelUpdate` checks if a panel update can be accepted based on the payload's type and host.
   *
   * @param {PanelUpdatePayload} payload the panel element and the action type
   * @returns {boolean} a boolean value.
   */
  canAcceptPanelUpdate(payload) {
    const { host, type } = payload;
    if (type === 'ADD') {
      return findParent(payload.host, 'VEGA-TAB-GROUP') === this.host && !this.tabPanels.includes(host);
    }
    else {
      return this.tabPanels && this.tabPanels.includes(host);
    }
  }
  /**
   * The function initializes the selected tab data target by setting it to the default active panel ID
   * or the first tab item's data target if no default active panel ID is available.
   */
  initSelectedTabDataTarget() {
    if (!this.selectedTabDataTarget) {
      let defaultActivePanelId = this.getDefaultActivePanelId();
      if (!defaultActivePanelId && this.tabItems && this.tabItems.length) {
        defaultActivePanelId = this.tabItems[0].dataTarget;
      }
      if (defaultActivePanelId) {
        this.selectTargetController.setSelectedDataTarget(defaultActivePanelId);
      }
    }
    else {
      this.updateTabPanelsStatus(this.selectedTabDataTarget);
    }
  }
  /**
   * get the default active panel element if user set
   *
   * @returns {Nullable<string>} the default active panel attribute id
   */
  getDefaultActivePanelId() {
    if (this.tabPanels && this.tabPanels.length) {
      const activePanel = this.tabPanels.find((tabPanel) => tabPanel.isActive === true);
      return activePanel ? activePanel.getAttribute('id') : null;
    }
  }
  /**
   * update the active panel by selected panel id
   *
   * @param {string} selectedTabDataTarget selected panel id
   */
  updateTabPanelsStatus(selectedTabDataTarget) {
    if (this.tabPanels.length && selectedTabDataTarget) {
      this.tabPanels.forEach((item) => {
        const paneId = item.getAttribute('id');
        const isActivePanel = paneId === selectedTabDataTarget;
        const tabItem = this.tabItems.find((tab) => tab.dataTarget === paneId);
        item.setAttribute('role', 'tabpanel');
        if (tabItem === null || tabItem === void 0 ? void 0 : tabItem.label) {
          item.setAttribute('aria-label', tabItem.label);
        }
        else {
          item.removeAttribute('aria-label');
        }
        if (isActivePanel) {
          item.isActive = true;
          item.setAttribute('tabindex', '0');
          item.setAttribute('aria-hidden', 'false');
        }
        else {
          item.isActive = false;
          item.setAttribute('tabindex', '-1');
          item.setAttribute('aria-hidden', 'true');
        }
      });
    }
  }
}
__decorate([
  MapToComponentField()
], VegaTabGroupPanelController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaTabGroupPanelController.prototype, "selectedTabDataTarget", void 0);
__decorate([
  MapToComponentField()
], VegaTabGroupPanelController.prototype, "tabItems", void 0);
__decorate([
  MapToComponentField({ componentFieldName: 'vegaTabGroupSelectedTargetController' })
], VegaTabGroupPanelController.prototype, "selectTargetController", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaTabGroupPanelController.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaTabGroupPanelController.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaTabGroupPanelController.prototype, "componentDidLoad", null);
__decorate([
  MapToComponentMethod('watchSelectedTabDataTargetChange')
], VegaTabGroupPanelController.prototype, "watchSelectedTabDataTargetChange", null);
