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';
import { SynchronizeTaskQueue } from '../../../../../helpers/queue/synchronize-task-queue';
/** The `VegaTabGroupSliderStateController` class manages the animation state of a tab group slider component. */
export class VegaTabGroupSliderStateController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /* about 2 frames in the browser */
    this.UPDATE_ANIMATION_FLAG_DELAY = 32;
    this.goToPageTaskQueue = new SynchronizeTaskQueue();
    /**
     * The normal flow:
     * page index change
     *   set pending flag to true
     *   end animation
     *   create remove flag setTimeout
     *
     * Quickly switch pages flow:
     * page index change
     *   set pending flag to true
     * page index change
     *   set pending flag to true
     *   end animation
     *   create remove flag setTimeout
     * page index change
     *   set pending flag to true
     *   end animation
     *   create remove flag setTimeout
     *
     *   end animation
     *   create remove flag setTimeout
     *
     * Add a num records carousel action time, validate the num if want know the state
     */
    this.pendingCarouselActionCount = 0;
  }
  /**
   * goToPage method should be called after the carousel re-render which waiting for the carousel to render all pages,
   *    otherwise it may not jump to the current page.
   */
  async componentDidRender() {
    await this.goToPageTaskQueue.runTask();
  }
  /* istanbul ignore next */
  /**
   * update the page index when component call watchCurrentPageIndexChange method
   *
   *  {@link VegaTabGroup#watchCurrentPageIndexChange}
   */
  async handlePageIndexChange() {
    const carouselDom = this.tabListRenderer.getCarouselDomRef();
    if (carouselDom) {
      this.pendingCarouselActionCount += 1;
      setTimeout(() => {
        if (this && typeof this.pendingCarouselActionCount === 'number') {
          this.pendingCarouselActionCount -= 1;
        }
      }, this.UPDATE_ANIMATION_FLAG_DELAY);
      await this.goToPageTaskQueue.enqueue({
        /** Add task to the queue and run it until component did render */
        run: async () => await carouselDom.goToPage(this.currentPageIndex + 1),
      }, false);
    }
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-tab-group-with-button-click-normal}
   */
  /* istanbul ignore next */
  /**
   * The function isAnimationInProgress() returns a boolean value indicating whether there is a pending animation for a tab.
   *
   * @returns {boolean} A boolean value indicating whether there is a pending animation.
   */
  isAnimationInProgress() {
    return this.pendingCarouselActionCount > 0;
  }
}
__decorate([
  MapToComponentField({ componentFieldName: 'vegaTabGroupTabListRenderer' })
], VegaTabGroupSliderStateController.prototype, "tabListRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaTabGroupSliderStateController.prototype, "currentPageIndex", void 0);
__decorate([
  MapToComponentMethod('componentDidRender')
], VegaTabGroupSliderStateController.prototype, "componentDidRender", null);
__decorate([
  MapToComponentMethod('watchCurrentPageIndexChange')
], VegaTabGroupSliderStateController.prototype, "handlePageIndexChange", null);
