import { calculateAxis } from './axisCalculation.mjs';
import { ViewportBaseCalculator } from './viewportBase.mjs';
/**
 * @typedef {object} ViewportRowsCalculatorOptions
 * @property {Map<string, ViewportBaseCalculator>} calculationTypes The calculation types to be performed.
 * @property {number} viewportHeight Height of the viewport.
 * @property {number} scrollOffset Current vertical scroll position of the viewport.
 * @property {number} totalRows Total number of rows.
 * @property {Function} overrideFn Function that allows to adjust the `startRow` and `endRow` parameters.
 * @property {number} horizontalScrollbarHeight The scrollbar height.
 * @property {PositionCache} rowHeightCache A built prefix sum cache. The single source of truth for row sizes.
 */ /**
 * Calculates indexes of rows to render OR rows that are visible OR partially visible in the viewport.
 *
 * @class ViewportRowsCalculator
 */ export class ViewportRowsCalculator extends ViewportBaseCalculator {
    /**
   * Calculates viewport.
   */ calculate() {
        calculateAxis(this, {
            totalCount: this.totalRows,
            zeroBasedScrollOffset: this.zeroBasedScrollOffset,
            scrollEnd: this.innerViewportHeight,
            positionCache: this.positionCache,
            setSizeField: (ctx, size)=>{
                ctx.rowHeight = size;
            },
            setTotalCalculated: (ctx, v)=>{
                ctx.totalCalculatedHeight = v;
            },
            getTotalCalculated: (ctx)=>ctx.totalCalculatedHeight
        });
    }
    /**
   * Gets the row height at the specified row index.
   *
   * @param {number} row Row index.
   * @returns {number}
   */ getRowHeight(row) {
        return this.positionCache.getSizeAt(row);
    }
    /**
   * @param {ViewportRowsCalculatorOptions} options Object with all options specified for row viewport calculation.
   */ constructor({ calculationTypes, viewportHeight, scrollOffset, totalRows, overrideFn, horizontalScrollbarHeight, rowHeightCache }){
        super(calculationTypes), this.viewportHeight = 0, this.scrollOffset = 0, this.zeroBasedScrollOffset = 0, this.totalRows = 0, this.rowHeight = 0, this.overrideFn = null, this.horizontalScrollbarHeight = 0, this.innerViewportHeight = 0, this.totalCalculatedHeight = 0, this.needReverse = true;
        this.viewportHeight = viewportHeight;
        this.scrollOffset = scrollOffset;
        this.zeroBasedScrollOffset = Math.max(scrollOffset, 0);
        this.totalRows = totalRows;
        this.overrideFn = overrideFn;
        this.horizontalScrollbarHeight = horizontalScrollbarHeight ?? 0;
        this.innerViewportHeight = this.zeroBasedScrollOffset + this.viewportHeight - this.horizontalScrollbarHeight;
        this.positionCache = rowHeightCache;
        this.calculate();
    }
}
