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, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
/** The `VegaTabGroupPageIndexController` class is a TypeScript class that manages the current page index of a tab group component. */
export class VegaTabGroupPageIndexController extends VegaSlimmer {
  /**
   * update the page index when component call watchTabPagesChange method
   */
  handleTabPagesChange() {
    if (this.selectedTabDataTarget) {
      this.setCurrentPageIndexBySelectedTab(this.selectedTabDataTarget);
    }
    else {
      this.setCurrentPageIndexWhenTabPagesChange();
    }
  }
  /**
   * update the page index when component call watchSelectedTabDataTargetChange method
   */
  handleSelectedDataTargetChange() {
    this.setCurrentPageIndexBySelectedTab(this.selectedTabDataTarget);
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-tab-group-with-click-tab-in-dropdown}
   */
  /* istanbul ignore next */
  /**
   * Update the page index after the tab click
   *
   * @param {string} selectedTabDataTarget the tab item property dataTarget
   */
  setCurrentPageIndexBySelectedTab(selectedTabDataTarget) {
    let pageIndex = -1;
    this.tabPages.forEach((tabPage, index) => {
      const tabIndex = tabPage.findIndex((tab) => tab.dataTarget === selectedTabDataTarget);
      if (tabIndex !== -1) {
        pageIndex = index;
      }
    });
    if (pageIndex >= 0) {
      this.currentPageIndex = pageIndex;
    }
  }
  /* istanbul ignore next */
  /**
   * The `setPageIndex` function sets the current page index to the specified value.
   *
   * @param {number} pageIndex - The pageIndex parameter is a number that represents the index of the page you want to set as the current page.
   */
  setPageIndex(pageIndex) {
    this.currentPageIndex = pageIndex;
  }
  /*
   * The below method is e2e-test covered in
   *
   * @see {module:vega-tab-group-with-tabPages-change}
   */
  /* istanbul ignore next */
  /**
   * Update the current page index if tab pages array length change when delete the tab items
   */
  setCurrentPageIndexWhenTabPagesChange() {
    if (this.tabPages && this.tabPages.length && !this.tabPages[this.currentPageIndex]) {
      this.currentPageIndex = this.tabPages.length - 1;
    }
  }
}
__decorate([
  MapToComponentField()
], VegaTabGroupPageIndexController.prototype, "tabPages", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaTabGroupPageIndexController.prototype, "currentPageIndex", void 0);
__decorate([
  MapToComponentField()
], VegaTabGroupPageIndexController.prototype, "selectedTabDataTarget", void 0);
__decorate([
  MapToComponentMethod('watchTabPagesChange')
], VegaTabGroupPageIndexController.prototype, "handleTabPagesChange", null);
__decorate([
  MapToComponentMethod('watchSelectedTabDataTargetChange')
], VegaTabGroupPageIndexController.prototype, "handleSelectedDataTargetChange", null);
