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 { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaInternalVirtualScrollToBottom } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
/**
 * VegaInputSelectLazyLoadController
 */
export class VegaInputSelectLazyLoadController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.hasMore = true;
  }
  /**
   * Observer scroll to bottom on vegaVirtualScroll
   */
  componentDidLoad() {
    const virtualScroll = this.getVirtualScroll();
    if (virtualScroll) {
      DomNodeSubjectObserverFactory.addUniqueObserverToNode(virtualScroll, VegaInternalVirtualScrollToBottom, this.onScrollToBottom.bind(this));
    }
  }
  /* istanbul ignore next */
  /**
   * Get virtualScroll
   *
   * @returns {Nullable<HTMLElement>} - virtualScroll
   */
  getVirtualScroll() {
    var _a, _b, _c;
    return (_c = (_b = (_a = this.vegaInputSelectDropdownRenderer
      .getDropdownRef()
      .shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector('vega-dropdown-content-box')) === null || _b === void 0 ? void 0 : _b.shadowRoot) === null || _c === void 0 ? void 0 : _c.querySelector('vega-virtual-scroll');
  }
  /**
   * The function `onScrollToBottom` logs a message and emits an event if lazy loading is enabled.
   *
   * @param {VegaVirtualScrollToBottomEvent} payload - payload
   */
  async onScrollToBottom(payload) {
    if (this.sourceLazyLoadCallback && this.hasMore) {
      this.isLazyLoading = true;
      const response = await this.sourceLazyLoadCallback(payload.endIndex);
      this.hasMore = response.hasMore;
      if (response.items.length > 0) {
        this.sourceController.appendSourceItems(response.items);
      }
      this.isLazyLoading = false;
    }
  }
}
__decorate([
  MapToComponentField()
], VegaInputSelectLazyLoadController.prototype, "sourceLazyLoadCallback", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaInputSelectLazyLoadController.prototype, "isLazyLoading", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectLazyLoadController.prototype, "sourceController", void 0);
__decorate([
  MapToComponentField()
], VegaInputSelectLazyLoadController.prototype, "vegaInputSelectDropdownRenderer", void 0);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaInputSelectLazyLoadController.prototype, "componentDidLoad", null);
