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 { containerScrollToItem } from '../../../../utils/ui';
import ChangeManager from '../../../../helpers/change-manager/change-manager';
import DomNodeSubjectFactory from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalVirtualScrollToBottom } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
/**
 * The `VegaVirtualScrollScrollableController` class in TypeScript manages virtual scrolling behavior
 * by handling scroll events and scrolling to specific indexes within a container element.
 */
export class VegaVirtualScrollScrollableController extends VegaSlimmer {
  /**
   * The `componentDidLoad` function in TypeScript listens for scroll events on a container.
   */
  componentDidLoad() {
    this.listenScrollContainer();
  }
  /**
   * The `componentDidRender` function sets the scroll position of a container element to a specified value.
   * While dropdown select a item and hide dropdown and show dropdown again, the virtual scroll will be re-render
   * and dropdown will call scrollToIndex with same index param.
   * In this time, the value of scrollTop not change, but container scrollTop is become 0 by the re-render of virtual scroll,
   * so we should update container scrollTop to avoid inconsistencies between view and component.
   */
  componentDidRender() {
    this.renderer.getContainerRef().scrollTop = this.scrollTop;
  }
  /**
   * The function `watchScrollTop` in TypeScript calls the `handleScroll` method.
   */
  watchScrollTop() {
    this.handleScroll();
  }
  /**
   * watchSourceChange
   */
  watchSourceChange() {
    this.adjustScrollTopInVirtualMode();
  }
  /**
   * watchApproxItemHeight
   */
  watchApproxItemHeight() {
    this.adjustScrollTopInVirtualMode();
  }
  /**
   * watchViewportMaxHeight
   */
  watchViewportMaxHeight() {
    this.adjustScrollTopInVirtualMode();
  }
  /**
   * The scrollToIndex function scrolls to a specific index within a container element either by
   * directly scrolling to the item or by setting the scrollTop property based on the index and item
   * height.
   *
   * @param {number} index - The `index` parameter is a number that represents the position of the item
   * you want to scroll to within a list or container. It is used to determine which item should be
   * scrolled into view.
   */
  scrollToIndex(index) {
    const containerRef = this.renderer.getContainerRef();
    if (index >= 0) {
      if (this.modeController.getMode() === 'normal') {
        const selectedItem = containerRef.querySelector(`:nth-child(${index + 1})`);
        containerScrollToItem(containerRef, selectedItem);
      }
      else {
        this.scrollTop = index * this.approxItemHeight;
      }
    }
  }
  /**
   * The function `resetScrollTop` sets the `scrollTop` property to 0.
   */
  resetScrollTop() {
    this.scrollTop = 0;
  }
  /**
   * The function `listenScrollContainer` listens for scroll events on a container element and updates
   * the `scrollTop` property accordingly.
   */
  listenScrollContainer() {
    this.renderer.getContainerRef().addEventListener('scroll', () => {
      this.scrollTop = this.renderer.getContainerRef().scrollTop;
    });
  }
  /**
   * The handleScroll function sets the scrollTop property of a container element and updates the
   * position based on the scroll top value.
   */
  handleScroll() {
    const containerRef = this.renderer.getContainerRef();
    containerRef && (containerRef.scrollTop = this.scrollTop);
    this.positionController.setPositionByScrollTop();
    this.checkIsScrollToBottom();
  }
  /**
   * The function adjusts the scroll top position based on certain properties to ensure it stays within
   * valid bounds.
   */
  adjustScrollTopInVirtualMode() {
    if (this.modeController.getMode() === 'normal')
      return;
    this.scrollTop = Math.min((this.source.length - Math.floor(this.viewportMaxHeight / this.approxItemHeight)) *
      this.approxItemHeight, this.scrollTop);
  }
  /**
   * Checks if the page is at the bottom of the page and if so notifies the change manager
   */
  checkIsScrollToBottom() {
    if (!this.isLoading &&
      (this.isScrollToBottomInVirtualMode() || this.isScrollToBottomInNormalMode())) {
      ChangeManager.notify(DomNodeSubjectFactory.getSubject(this.host, VegaInternalVirtualScrollToBottom), {
        endIndex: this.source.length - 1,
      });
    }
  }
  /**
   * The function `isScrollToBottomInVirtualMode` checks if the `startIndex` is equal to `maxStartIndex`.
   *
   * @returns {boolean} The method `isScrollToBottomInVirtualMode()` is returning a boolean value based on the comparison of
   * `this.startIndex` and `this.maxStartIndex`. If they are equal, it will return `true`, otherwise
   * `false`.
   */
  isScrollToBottomInVirtualMode() {
    return (this.modeController.getMode() === 'virtual' &&
      this.positionConfig.startIndex === this.positionController.getMaxStartIndex());
  }
  /**
   * The function `isScrollToBottomInNormalMode` checks if the virtual scroll container is scrolled to the bottom based
   * on its scroll height, scroll top, and viewport max height.
   *
   * @returns {boolean} The function `isScrollToBottomInNormalMode()` is returning a boolean value based on the comparison of the
   * sum of `this.virtualScrollContainerRef.scrollHeight` and `this.virtualScrollContainerRef.scrollTop`
   * being equal to `this.viewportMaxHeight`.
   */
  isScrollToBottomInNormalMode() {
    const containerRef = this.renderer.getContainerRef();
    return (this.modeController.getMode() === 'normal' &&
      containerRef.scrollHeight - this.scrollTop >= this.viewportMaxHeight);
  }
}
__decorate([
  MapToComponentField()
], VegaVirtualScrollScrollableController.prototype, "sourceItemIdentifier", void 0);
__decorate([
  MapToComponentField()
], VegaVirtualScrollScrollableController.prototype, "source", void 0);
__decorate([
  MapToComponentField()
], VegaVirtualScrollScrollableController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaVirtualScrollScrollableController.prototype, "viewportMaxHeight", void 0);
__decorate([
  MapToComponentField()
], VegaVirtualScrollScrollableController.prototype, "approxItemHeight", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaVirtualScrollScrollableController.prototype, "scrollTop", void 0);
__decorate([
  MapToComponentField()
], VegaVirtualScrollScrollableController.prototype, "positionConfig", void 0);
__decorate([
  MapToComponentField()
], VegaVirtualScrollScrollableController.prototype, "isLoading", void 0);
__decorate([
  MapToComponentField()
], VegaVirtualScrollScrollableController.prototype, "renderer", void 0);
__decorate([
  MapToComponentField()
], VegaVirtualScrollScrollableController.prototype, "positionController", void 0);
__decorate([
  MapToComponentField()
], VegaVirtualScrollScrollableController.prototype, "modeController", void 0);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaVirtualScrollScrollableController.prototype, "componentDidLoad", null);
__decorate([
  MapToComponentMethod('componentDidRender')
], VegaVirtualScrollScrollableController.prototype, "componentDidRender", null);
__decorate([
  MapToComponentMethod('watchScrollTop')
], VegaVirtualScrollScrollableController.prototype, "watchScrollTop", null);
__decorate([
  MapToComponentMethod('watchSourceChange')
], VegaVirtualScrollScrollableController.prototype, "watchSourceChange", null);
__decorate([
  MapToComponentMethod('watchApproxItemHeight')
], VegaVirtualScrollScrollableController.prototype, "watchApproxItemHeight", null);
__decorate([
  MapToComponentMethod('watchViewportMaxHeight')
], VegaVirtualScrollScrollableController.prototype, "watchViewportMaxHeight", null);
__decorate([
  MapToComponentMethod('scrollToIndex')
], VegaVirtualScrollScrollableController.prototype, "scrollToIndex", null);
