/**
 * DevExtreme (cjs/renovation/ui/grids/data_grid_next/scrolling/virtual_scrolling.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.viewFunction = exports.VirtualScrollingProps = exports.VirtualScrolling = void 0;
var _inferno = require("inferno");
var _inferno2 = require("@devextreme/runtime/inferno");
var _context = require("../../../../utils/plugin/context");
var _value_setter = require("../../../../utils/plugin/value_setter");
var _getter_extender = require("../../../../utils/plugin/getter_extender");
var _table_content = require("../views/table_content");
var _plugins = require("../plugins");
var _plugins2 = require("../paging/plugins");
var _plugins3 = require("./plugins");
var _classes = _interopRequireDefault(require("../classes"));
var _virtual_content = require("./virtual_content");
var _utils = require("../utils");
var _utils2 = require("./utils");
var _excluded = ["mode"];

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _objectWithoutProperties(source, excluded) {
    if (null == source) {
        return {}
    }
    var target = _objectWithoutPropertiesLoose(source, excluded);
    var key, i;
    if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
        for (i = 0; i < sourceSymbolKeys.length; i++) {
            key = sourceSymbolKeys[i];
            if (excluded.indexOf(key) >= 0) {
                continue
            }
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) {
                continue
            }
            target[key] = source[key]
        }
    }
    return target
}

function _objectWithoutPropertiesLoose(source, excluded) {
    if (null == source) {
        return {}
    }
    var target = {};
    var sourceKeys = Object.keys(source);
    var key, i;
    for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) {
            continue
        }
        target[key] = source[key]
    }
    return target
}

function _extends() {
    _extends = Object.assign ? Object.assign.bind() : function(target) {
        for (var i = 1; i < arguments.length; i++) {
            var source = arguments[i];
            for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key]
                }
            }
        }
        return target
    };
    return _extends.apply(this, arguments)
}

function _defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) {
            descriptor.writable = true
        }
        Object.defineProperty(target, descriptor.key, descriptor)
    }
}

function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) {
        _defineProperties(Constructor.prototype, protoProps)
    }
    if (staticProps) {
        _defineProperties(Constructor, staticProps)
    }
    Object.defineProperty(Constructor, "prototype", {
        writable: false
    });
    return Constructor
}

function _assertThisInitialized(self) {
    if (void 0 === self) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
    }
    return self
}

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}
var viewFunction = function(_ref) {
    var itemHeights = _ref.itemHeights,
        onRowsScrollPositionChange = _ref.onRowsScrollPositionChange,
        onRowsViewContentRender = _ref.onRowsViewContentRender,
        rowHeight = _ref.rowHeight,
        topScrollPosition = _ref.topScrollPosition;
    return (0, _inferno.createFragment)([(0, _inferno.createComponentVNode)(2, _value_setter.ValueSetter, {
        type: _table_content.SetRowsViewScrollPositionAction,
        value: onRowsScrollPositionChange
    }), (0, _inferno.createComponentVNode)(2, _value_setter.ValueSetter, {
        type: _table_content.SetRowsViewContentRenderAction,
        value: onRowsViewContentRender
    }), (0, _inferno.createComponentVNode)(2, _value_setter.ValueSetter, {
        type: _plugins3.TopScrollingPositionValue,
        value: topScrollPosition
    }), (0, _inferno.createComponentVNode)(2, _value_setter.ValueSetter, {
        type: _plugins3.RowHeightValue,
        value: rowHeight
    }), (0, _inferno.createComponentVNode)(2, _value_setter.ValueSetter, {
        type: _plugins3.ItemHeightsValue,
        value: itemHeights
    }), (0, _inferno.createComponentVNode)(2, _getter_extender.GetterExtender, {
        type: _plugins3.ViewportSkipValue,
        order: 0,
        value: _plugins3.CalculateViewportSkipValue
    }), (0, _inferno.createComponentVNode)(2, _getter_extender.GetterExtender, {
        type: _plugins3.ViewportTakeValue,
        order: 0,
        value: _plugins3.CalculateViewportTakeValue
    }), (0, _inferno.createComponentVNode)(2, _getter_extender.GetterExtender, {
        type: _plugins.VisibleRows,
        order: 1,
        value: _plugins3.AddLoadIndexToVisibleRows
    }), (0, _inferno.createComponentVNode)(2, _getter_extender.GetterExtender, {
        type: _plugins.VisibleRows,
        order: 2,
        value: _plugins3.CalculateVisibleRowsInViewport
    }), (0, _inferno.createComponentVNode)(2, _getter_extender.GetterExtender, {
        type: _plugins3.ViewportPageIndex,
        order: 0,
        value: _plugins3.CalculateViewportPageIndex
    }), (0, _inferno.createComponentVNode)(2, _getter_extender.GetterExtender, {
        type: _plugins3.ViewportLoadPageCount,
        order: 0,
        value: _plugins3.CalculateViewportLoadPageCount
    }), (0, _inferno.createComponentVNode)(2, _virtual_content.VirtualContent)], 4)
};
exports.viewFunction = viewFunction;
var VirtualScrollingProps = {
    mode: "virtual"
};
exports.VirtualScrollingProps = VirtualScrollingProps;
var VirtualScrolling = function(_InfernoComponent) {
    _inheritsLoose(VirtualScrolling, _InfernoComponent);

    function VirtualScrolling(props) {
        var _this;
        _this = _InfernoComponent.call(this, props) || this;
        _this.visibleItemHeights = {};
        _this.viewportPayload = {
            pageIndex: 0,
            topScrollPosition: 0
        };
        _this.state = {
            topScrollPosition: 0,
            rowHeight: _utils2.DEFAULT_ROW_HEIGHT,
            viewportState: "synchronized"
        };
        _this.watchViewportPageIndex = _this.watchViewportPageIndex.bind(_assertThisInitialized(_this));
        _this.watchViewportLoadPageCount = _this.watchViewportLoadPageCount.bind(_assertThisInitialized(_this));
        _this.watchPageIndex = _this.watchPageIndex.bind(_assertThisInitialized(_this));
        _this.onRowsScrollPositionChange = _this.onRowsScrollPositionChange.bind(_assertThisInitialized(_this));
        _this.onRowsViewContentRender = _this.onRowsViewContentRender.bind(_assertThisInitialized(_this));
        _this.updateVisibleItemHeights = _this.updateVisibleItemHeights.bind(_assertThisInitialized(_this));
        _this.updateRowHeight = _this.updateRowHeight.bind(_assertThisInitialized(_this));
        _this.updateViewportState = _this.updateViewportState.bind(_assertThisInitialized(_this));
        return _this
    }
    var _proto = VirtualScrolling.prototype;
    _proto.createEffects = function() {
        return [new _inferno2.InfernoEffect(this.watchViewportPageIndex, [this.plugins]), new _inferno2.InfernoEffect(this.watchViewportLoadPageCount, [this.plugins]), new _inferno2.InfernoEffect(this.watchPageIndex, [this.plugins, this.state.topScrollPosition, this.state.viewportState, this.state.rowHeight])]
    };
    _proto.updateEffects = function() {
        var _this$_effects$, _this$_effects$2, _this$_effects$3;
        null === (_this$_effects$ = this._effects[0]) || void 0 === _this$_effects$ ? void 0 : _this$_effects$.update([this.plugins]);
        null === (_this$_effects$2 = this._effects[1]) || void 0 === _this$_effects$2 ? void 0 : _this$_effects$2.update([this.plugins]);
        null === (_this$_effects$3 = this._effects[2]) || void 0 === _this$_effects$3 ? void 0 : _this$_effects$3.update([this.plugins, this.state.topScrollPosition, this.state.viewportState, this.state.rowHeight])
    };
    _proto.watchViewportPageIndex = function() {
        var _this2 = this;
        return this.plugins.watch(_plugins3.ViewportPageIndex, (function(pageIndex) {
            _this2.plugins.callAction(_plugins2.SetPageIndex, pageIndex)
        }))
    };
    _proto.watchViewportLoadPageCount = function() {
        var _this3 = this;
        return this.plugins.watch(_plugins3.ViewportLoadPageCount, (function(loadPageCount) {
            _this3.plugins.callAction(_plugins2.SetLoadPageCount, loadPageCount)
        }))
    };
    _proto.watchPageIndex = function() {
        var _this4 = this;
        return this.plugins.watch(_plugins2.PageIndex, (function(pageIndex) {
            _this4.updateViewportState({
                type: "paging",
                value: pageIndex
            })
        }))
    };
    _proto.onRowsScrollPositionChange = function(offset) {
        this.updateViewportState({
            type: "scrolling",
            value: offset.top
        })
    };
    _proto.onRowsViewContentRender = function(element) {
        var rowElements = Array.from(element.querySelectorAll("tr.".concat(_classes.default.row, ":not(.").concat(_classes.default.virtualRow, ")")));
        var visibleRowHeights = rowElements.map((function(el) {
            return (0, _utils.getElementHeight)(el)
        }));
        this.updateVisibleItemHeights(visibleRowHeights);
        this.updateRowHeight(visibleRowHeights)
    };
    _proto.updateVisibleItemHeights = function(visibleRowHeights) {
        var _this$plugins$getValu, _this$plugins$getValu2, _this5 = this;
        var visibleRows = null !== (_this$plugins$getValu = this.plugins.getValue(_plugins.VisibleRows)) && void 0 !== _this$plugins$getValu ? _this$plugins$getValu : [];
        var skip = null !== (_this$plugins$getValu2 = this.plugins.getValue(_plugins3.ViewportSkipValue)) && void 0 !== _this$plugins$getValu2 ? _this$plugins$getValu2 : 0;
        var calculatedRowHeights = (0, _utils2.calculateItemHeights)(visibleRows, visibleRowHeights);
        calculatedRowHeights.forEach((function(height, index) {
            _this5.visibleItemHeights[skip + index] = height
        }))
    };
    _proto.updateRowHeight = function(visibleRowHeights) {
        var newRowHeight = (0, _utils2.calculateRowHeight)(visibleRowHeights, this.visibleItemHeights);
        this.setState((function(__state_argument) {
            return {
                rowHeight: newRowHeight
            }
        }))
    };
    _proto.updateViewportState = function(action) {
        var topScrollPositionCurrent = "scrolling" === action.type ? Math.abs(action.value) : 0;
        var pageIndexCurrent = "paging" === action.type ? action.value : 0;
        var pageIndexState = this.plugins.getValue(_plugins2.PageIndex);
        var _this$viewportPayload = this.viewportPayload,
            pageIndexPayload = _this$viewportPayload.pageIndex,
            topScrollPositionPayload = _this$viewportPayload.topScrollPosition;
        var isEqual_pageIndexPayloadToState = pageIndexState === pageIndexPayload,
            isEqual_pageIndexCurrentToPayload = pageIndexCurrent === pageIndexPayload,
            isEqual_topScrollPayloadToState = topScrollPositionPayload === this.state.topScrollPosition,
            isEqual_topScrollCurrentToPayload = topScrollPositionCurrent === topScrollPositionPayload;
        switch (this.state.viewportState) {
            case "scrolling":
                if (!isEqual_topScrollPayloadToState) {
                    this.setState((function(__state_argument) {
                        return {
                            topScrollPosition: topScrollPositionPayload
                        }
                    }));
                    this.setState((function(__state_argument) {
                        return {
                            viewportState: "synchronized"
                        }
                    }))
                }
                break;
            case "paging":
                var _this$plugins$getValu3;
                var pageSize = (0, _utils2.getNormalizedPageSize)(null !== (_this$plugins$getValu3 = this.plugins.getValue(_plugins2.PageSize)) && void 0 !== _this$plugins$getValu3 ? _this$plugins$getValu3 : 0);
                if (pageSize > 0) {
                    var _this$plugins$getValu4;
                    var calculatedTopScrollPosition = Math.round((0, _utils2.getTopScrollPosition)(pageIndexPayload, pageSize, this.visibleItemHeights, this.state.rowHeight));
                    var viewportItemIndex = (0, _utils2.calculateViewportItemIndex)(this.state.topScrollPosition, this.state.rowHeight, this.visibleItemHeights);
                    var totalCount = null !== (_this$plugins$getValu4 = this.plugins.getValue(_plugins.TotalCount)) && void 0 !== _this$plugins$getValu4 ? _this$plugins$getValu4 : 0;
                    var calculatedPageIndex = (0, _utils2.calculatePageIndexByItemIndex)(viewportItemIndex, pageSize, totalCount);
                    if (isEqual_pageIndexPayloadToState && calculatedPageIndex !== pageIndexPayload || !isEqual_pageIndexPayloadToState) {
                        var offset = {
                            top: calculatedTopScrollPosition
                        };
                        this.plugins.callAction(_table_content.SetRowsViewOffsetAction, offset)
                    }
                }
                this.setState((function(__state_argument) {
                    return {
                        viewportState: "synchronized"
                    }
                }));
                break;
            default:
                if ("scrolling" === action.type) {
                    if (!isEqual_topScrollCurrentToPayload || !isEqual_topScrollPayloadToState) {
                        this.viewportPayload.topScrollPosition = topScrollPositionCurrent;
                        this.setState((function(__state_argument) {
                            return {
                                viewportState: "scrolling"
                            }
                        }))
                    }
                } else if (!isEqual_pageIndexPayloadToState || !isEqual_pageIndexCurrentToPayload) {
                    this.viewportPayload.pageIndex = pageIndexCurrent;
                    this.setState((function(__state_argument) {
                        return {
                            viewportState: "paging"
                        }
                    }))
                }
        }
    };
    _proto.render = function() {
        var props = this.props;
        return viewFunction({
            props: _extends({}, props),
            topScrollPosition: this.state.topScrollPosition,
            rowHeight: this.state.rowHeight,
            viewportState: this.state.viewportState,
            plugins: this.plugins,
            itemHeights: this.itemHeights,
            onRowsScrollPositionChange: this.onRowsScrollPositionChange,
            onRowsViewContentRender: this.onRowsViewContentRender,
            updateVisibleItemHeights: this.updateVisibleItemHeights,
            updateRowHeight: this.updateRowHeight,
            updateViewportState: this.updateViewportState,
            restAttributes: this.restAttributes
        })
    };
    _createClass(VirtualScrolling, [{
        key: "plugins",
        get: function() {
            if ("PluginsContext" in this.context) {
                return this.context.PluginsContext
            }
            return _context.PluginsContext
        }
    }, {
        key: "itemHeights",
        get: function() {
            return this.visibleItemHeights
        }
    }, {
        key: "restAttributes",
        get: function() {
            var _this$props = this.props,
                restProps = (_this$props.mode, _objectWithoutProperties(_this$props, _excluded));
            return restProps
        }
    }]);
    return VirtualScrolling
}(_inferno2.InfernoComponent);
exports.VirtualScrolling = VirtualScrolling;
VirtualScrolling.defaultProps = VirtualScrollingProps;
