/**
 * DevExtreme (renovation/ui/grids/data_grid_next/scrolling/plugins.js)
 * Version: 22.1.3 (build 22180-0310)
 * Build date: Wed Jun 29 2022
 *
 * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.ViewportTakeValue = exports.ViewportSkipValue = exports.ViewportPageIndex = exports.ViewportLoadPageCount = exports.TopVirtualRowHeightValue = exports.TopScrollingPositionValue = exports.ScrollingModeValue = exports.RowHeightValue = exports.ItemHeightsValue = exports.CalculateVisibleRowsInViewport = exports.CalculateViewportTakeValue = exports.CalculateViewportSkipValue = exports.CalculateViewportPageIndex = exports.CalculateViewportLoadPageCount = exports.CalculateTopVirtualRowHeight = exports.CalculateBottomVirtualRowHeight = exports.BottomVirtualRowHeightValue = exports.AddLoadIndexToVisibleRows = void 0;
var _context = require("../../../../utils/plugin/context");
var _plugins = require("../plugins");
var _plugins2 = require("../paging/plugins");
var _table_content = require("../views/table_content");
var _utils = require("./utils");
var ScrollingModeValue = (0, _context.createValue)();
exports.ScrollingModeValue = ScrollingModeValue;
var TopScrollingPositionValue = (0, _context.createValue)();
exports.TopScrollingPositionValue = TopScrollingPositionValue;
var ViewportSkipValue = (0, _context.createGetter)(0);
exports.ViewportSkipValue = ViewportSkipValue;
var ViewportTakeValue = (0, _context.createGetter)(0);
exports.ViewportTakeValue = ViewportTakeValue;
var ItemHeightsValue = (0, _context.createValue)();
exports.ItemHeightsValue = ItemHeightsValue;
var RowHeightValue = (0, _context.createValue)();
exports.RowHeightValue = RowHeightValue;
var TopVirtualRowHeightValue = (0, _context.createGetter)(0);
exports.TopVirtualRowHeightValue = TopVirtualRowHeightValue;
var BottomVirtualRowHeightValue = (0, _context.createGetter)(0);
exports.BottomVirtualRowHeightValue = BottomVirtualRowHeightValue;
var ViewportPageIndex = (0, _context.createGetter)(0);
exports.ViewportPageIndex = ViewportPageIndex;
var ViewportLoadPageCount = (0, _context.createGetter)(0);
exports.ViewportLoadPageCount = ViewportLoadPageCount;
var CalculateViewportSkipValue = (0, _context.createSelector)([TopScrollingPositionValue, RowHeightValue, ItemHeightsValue, _plugins.TotalCount], (function(topScrollingPosition, rowHeight, itemHeights, totalCount) {
    var topScrollPosition = null !== topScrollingPosition && void 0 !== topScrollingPosition ? topScrollingPosition : 0;
    var topIndex = (0, _utils.calculateViewportItemIndex)(topScrollPosition, rowHeight, itemHeights);
    var skip = topIndex > totalCount ? totalCount : topIndex;
    return Math.floor(skip)
}));
exports.CalculateViewportSkipValue = CalculateViewportSkipValue;
var CalculateViewportTakeValue = (0, _context.createSelector)([ScrollingModeValue, _plugins.TotalCount, _table_content.RowsViewHeightValue, TopScrollingPositionValue, ViewportSkipValue, RowHeightValue, ItemHeightsValue], (function(scrollingMode, totalCount, rowsViewHeight, topScrollingPosition, skip, rowHeight, itemHeights) {
    var virtualMode = "virtual" === scrollingMode;
    var totalItemsCount = null !== totalCount && void 0 !== totalCount ? totalCount : 0;
    var viewportHeight = null !== rowsViewHeight && void 0 !== rowsViewHeight ? rowsViewHeight : 0;
    var topScrollPosition = null !== topScrollingPosition && void 0 !== topScrollingPosition ? topScrollingPosition : 0;
    var bottomIndex = (0, _utils.calculateViewportItemIndex)(topScrollPosition + viewportHeight, rowHeight, itemHeights);
    var take = Math.ceil(bottomIndex - skip);
    if (virtualMode) {
        var remainedItems = Math.max(0, totalItemsCount - skip);
        take = Math.min(take, remainedItems)
    }
    return take
}));
exports.CalculateViewportTakeValue = CalculateViewportTakeValue;
var AddLoadIndexToVisibleRows = (0, _context.createSelector)([_plugins.VisibleRows, _plugins.DataStateValue], (function(visibleRows, dataState) {
    var _dataState$dataOffset;
    var loadIndex = (null !== (_dataState$dataOffset = dataState.dataOffset) && void 0 !== _dataState$dataOffset ? _dataState$dataOffset : 0) - 1;
    var newRows = visibleRows.map((function(row) {
        var r = row;
        if ("data" === r.rowType) {
            loadIndex += 1
        }
        r.loadIndex = loadIndex;
        return r
    }));
    return newRows
}));
exports.AddLoadIndexToVisibleRows = AddLoadIndexToVisibleRows;
var CalculateVisibleRowsInViewport = (0, _context.createSelector)([_plugins.VisibleRows, ViewportSkipValue, ViewportTakeValue], (function(visibleRows, skip, take) {
    return visibleRows.filter((function(row) {
        var isLoadIndexGreaterStart = void 0 !== row.loadIndex && row.loadIndex >= skip;
        var isLoadIndexLessEnd = void 0 !== row.loadIndex && row.loadIndex < skip + take;
        return isLoadIndexGreaterStart && isLoadIndexLessEnd
    }))
}));
exports.CalculateVisibleRowsInViewport = CalculateVisibleRowsInViewport;
var CalculateTopVirtualRowHeight = (0, _context.createSelector)([ViewportSkipValue, RowHeightValue, _plugins.TotalCount, ItemHeightsValue], (function(skip, rowHeight, totalCount, itemHeights) {
    return (0, _utils.getVirtualContentOffset)("top", skip, totalCount, itemHeights, rowHeight)
}));
exports.CalculateTopVirtualRowHeight = CalculateTopVirtualRowHeight;
var CalculateBottomVirtualRowHeight = (0, _context.createSelector)([ViewportSkipValue, _plugins.TotalCount, RowHeightValue, ItemHeightsValue, _plugins.VisibleRows], (function(skip, totalCount, rowHeight, itemHeights, visibleRows) {
    var rowCount = visibleRows.filter((function(r) {
        return "data" === r.rowType
    })).length;
    return (0, _utils.getVirtualContentOffset)("bottom", totalCount - skip - rowCount, totalCount, itemHeights, rowHeight)
}));
exports.CalculateBottomVirtualRowHeight = CalculateBottomVirtualRowHeight;
var CalculateViewportPageIndex = (0, _context.createSelector)([ViewportSkipValue, _plugins2.PageSize, _plugins.TotalCount], (function(skip, pageSize, totalCount) {
    var pSize = (0, _utils.getNormalizedPageSize)(pageSize);
    return (0, _utils.calculatePageIndexByItemIndex)(skip, pSize, totalCount)
}));
exports.CalculateViewportPageIndex = CalculateViewportPageIndex;
var CalculateViewportLoadPageCount = (0, _context.createSelector)([ViewportSkipValue, ViewportTakeValue, _plugins2.PageSize, ViewportPageIndex], (function(skip, take, pageSize, viewportPageIndex) {
    var pSize = (0, _utils.getNormalizedPageSize)(pageSize);
    var pageOffset = viewportPageIndex * pSize;
    var skipForCurrentPage = skip - pageOffset;
    return Math.ceil(pageSize > 0 ? (take + skipForCurrentPage) / pageSize : 0)
}));
exports.CalculateViewportLoadPageCount = CalculateViewportLoadPageCount;
