/**
 * DevExtreme (renovation/ui/grids/data_grid_next/views/table_content.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.TopRowPlaceholder = exports.TableContentProps = exports.TableContent = exports.SetRowsViewScrollPositionAction = exports.SetRowsViewOffsetAction = exports.SetRowsViewContentRenderAction = exports.RowsViewScroll = exports.RowsViewHeightValue = exports.RowsViewHeight = exports.RowClick = exports.BottomRowPlaceholder = void 0;
var _inferno = require("inferno");
var _inferno2 = require("@devextreme/runtime/inferno");
var _resize_observer = _interopRequireDefault(require("../../../../../core/resize_observer"));
var _value_setter = require("../../../../utils/plugin/value_setter");
var _table = require("../widgets/table");
var _data_row = require("../widgets/data_row");
var _no_data_text = require("../widgets/no_data_text");
var _context = require("../../../../utils/plugin/context");
var _events_engine = _interopRequireDefault(require("../../../../../events/core/events_engine"));
var _click = require("../../../../../events/click");
var _classes = _interopRequireDefault(require("../classes"));
var _utils = require("../utils");
var _combine_classes = require("../../../../utils/combine_classes");
var _placeholder = require("../../../../utils/plugin/placeholder");
var _scrollable = require("../../../scroll_view/scrollable");
var _plugins = require("../plugins");
var _excluded = ["columns", "noDataTemplate", "visibleRows"];

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 _defineProperty(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        })
    } else {
        obj[key] = value
    }
    return obj
}

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)
}

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)
}
var TopRowPlaceholder = (0, _context.createPlaceholder)();
exports.TopRowPlaceholder = TopRowPlaceholder;
var BottomRowPlaceholder = (0, _context.createPlaceholder)();
exports.BottomRowPlaceholder = BottomRowPlaceholder;
var RowClick = (0, _context.createValue)();
exports.RowClick = RowClick;
var RowsViewScroll = (0, _context.createValue)();
exports.RowsViewScroll = RowsViewScroll;
var RowsViewHeight = (0, _context.createValue)();
exports.RowsViewHeight = RowsViewHeight;
var SetRowsViewScrollPositionAction = (0, _context.createValue)();
exports.SetRowsViewScrollPositionAction = SetRowsViewScrollPositionAction;
var RowsViewHeightValue = (0, _context.createValue)();
exports.RowsViewHeightValue = RowsViewHeightValue;
var SetRowsViewContentRenderAction = (0, _context.createValue)();
exports.SetRowsViewContentRenderAction = SetRowsViewContentRenderAction;
var SetRowsViewOffsetAction = (0, _context.createValue)();
exports.SetRowsViewOffsetAction = SetRowsViewOffsetAction;
var viewFunction = function(viewModel) {
    return (0, _inferno.createVNode)(1, "div", viewModel.classes, [(0, _inferno.createComponentVNode)(2, _scrollable.Scrollable, {
        onScroll: viewModel.onScrollContent,
        children: (0, _inferno.createVNode)(1, "div", "".concat(_classes.default.content), (0, _inferno.createComponentVNode)(2, _table.Table, {
            children: (0, _inferno.createFragment)([(0, _inferno.createComponentVNode)(2, _placeholder.Placeholder, {
                type: TopRowPlaceholder
            }), viewModel.rows.map((function(item) {
                return (0, _inferno.createComponentVNode)(2, _data_row.DataRow, {
                    row: item,
                    rowIndex: item.index,
                    columns: viewModel.props.columns
                }, item.reactKey)
            })), (0, _inferno.createComponentVNode)(2, _placeholder.Placeholder, {
                type: BottomRowPlaceholder
            })], 0)
        }), 2, null, null, viewModel.divRef)
    }, null, viewModel.scrollableRef), viewModel.isEmpty && (0, _inferno.createComponentVNode)(2, _no_data_text.NoDataText, {
        template: viewModel.props.noDataTemplate
    }), (0, _inferno.createComponentVNode)(2, _value_setter.ValueSetter, {
        type: SetRowsViewOffsetAction,
        value: viewModel.scrollTo
    })], 0, {
        role: "presentation"
    }, null, viewModel.rowsViewRef)
};
exports.viewFunction = viewFunction;
var TableContentProps = {
    visibleRows: Object.freeze([]),
    columns: Object.freeze([])
};
exports.TableContentProps = TableContentProps;
var getTemplate = function(TemplateProp) {
    return TemplateProp && (TemplateProp.defaultProps ? function(props) {
        return (0, _inferno.normalizeProps)((0, _inferno.createComponentVNode)(2, TemplateProp, _extends({}, props)))
    } : TemplateProp)
};
var TableContent = function(_InfernoComponent) {
    _inheritsLoose(TableContent, _InfernoComponent);

    function TableContent(props) {
        var _this;
        _this = _InfernoComponent.call(this, props) || this;
        _this.state = {};
        _this.rowsViewRef = (0, _inferno.createRef)();
        _this.divRef = (0, _inferno.createRef)();
        _this.scrollableRef = (0, _inferno.createRef)();
        _this.__getterCache = {};
        _this.subscribeToRowClick = _this.subscribeToRowClick.bind(_assertThisInitialized(_this));
        _this.calculateRowsViewHeight = _this.calculateRowsViewHeight.bind(_assertThisInitialized(_this));
        _this.rowsViewContentReady = _this.rowsViewContentReady.bind(_assertThisInitialized(_this));
        _this.onRowClick = _this.onRowClick.bind(_assertThisInitialized(_this));
        _this.onScrollContent = _this.onScrollContent.bind(_assertThisInitialized(_this));
        _this.scrollTo = _this.scrollTo.bind(_assertThisInitialized(_this));
        _this.onResize = _this.onResize.bind(_assertThisInitialized(_this));
        return _this
    }
    var _proto = TableContent.prototype;
    _proto.createEffects = function() {
        return [new _inferno2.InfernoEffect(this.subscribeToRowClick, [this.plugins, this.props.visibleRows]), new _inferno2.InfernoEffect(this.calculateRowsViewHeight, [this.plugins]), new _inferno2.InfernoEffect(this.rowsViewContentReady, [this.props, this.plugins, this.props.visibleRows, this.props.columns, this.props.noDataTemplate])]
    };
    _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, this.props.visibleRows]);
        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.props, this.plugins, this.props.visibleRows, this.props.columns, this.props.noDataTemplate])
    };
    _proto.subscribeToRowClick = function() {
        var _this2 = this;
        var onRowClick = this.onRowClick.bind(this);
        _events_engine.default.on(this.divRef.current, _click.name, ".".concat(_classes.default.row), onRowClick);
        return function() {
            return _events_engine.default.off(_this2.divRef.current, _click.name, onRowClick)
        }
    };
    _proto.calculateRowsViewHeight = function() {
        var _this3 = this;
        this.onResize(this.rowsViewRef.current);
        _resize_observer.default.observe(this.rowsViewRef.current, (function(_ref) {
            var target = _ref.target;
            _this3.onResize(target)
        }));
        return function() {
            _resize_observer.default.unobserve(_this3.rowsViewRef.current)
        }
    };
    _proto.rowsViewContentReady = function() {
        var element = this.divRef.current;
        element && this.plugins.callAction(SetRowsViewContentRenderAction, element)
    };
    _proto.onRowClick = function(e) {
        var allRows = this.divRef.current.getElementsByClassName(_classes.default.row);
        var index = Array.from(allRows).indexOf(e.currentTarget);
        if (index >= 0) {
            this.plugins.callAction(RowClick, this.props.visibleRows[index], e)
        }
    };
    _proto.onScrollContent = function(e) {
        this.plugins.callAction(SetRowsViewScrollPositionAction, e.scrollOffset)
    };
    _proto.scrollTo = function(e) {
        var _this$scrollableRef$c;
        null === (_this$scrollableRef$c = this.scrollableRef.current) || void 0 === _this$scrollableRef$c ? void 0 : _this$scrollableRef$c.scrollTo(e)
    };
    _proto.onResize = function(target) {
        this.plugins.set(RowsViewHeightValue, (0, _utils.getElementHeight)(target))
    };
    _proto.componentWillUpdate = function(nextProps, nextState, context) {
        _InfernoComponent.prototype.componentWillUpdate.call(this);
        if (this.props.visibleRows !== nextProps.visibleRows) {
            this.__getterCache.rows = void 0
        }
    };
    _proto.render = function() {
        var props = this.props;
        return viewFunction({
            props: _extends({}, props, {
                noDataTemplate: getTemplate(props.noDataTemplate)
            }),
            rowsViewRef: this.rowsViewRef,
            divRef: this.divRef,
            scrollableRef: this.scrollableRef,
            plugins: this.plugins,
            onRowClick: this.onRowClick,
            classes: this.classes,
            rows: this.rows,
            isEmpty: this.isEmpty,
            onScrollContent: this.onScrollContent,
            scrollTo: this.scrollTo,
            onResize: this.onResize,
            restAttributes: this.restAttributes
        })
    };
    _createClass(TableContent, [{
        key: "plugins",
        get: function() {
            if ("PluginsContext" in this.context) {
                return this.context.PluginsContext
            }
            return _context.PluginsContext
        }
    }, {
        key: "classes",
        get: function() {
            var _combineClasses;
            return (0, _combine_classes.combineClasses)((_combineClasses = {}, _defineProperty(_combineClasses, _classes.default.rowsView, true), _defineProperty(_combineClasses, _classes.default.noWrap, true), _defineProperty(_combineClasses, _classes.default.afterHeaders, true), _defineProperty(_combineClasses, _classes.default.empty, this.isEmpty), _combineClasses))
        }
    }, {
        key: "rows",
        get: function() {
            var _this4 = this;
            if (void 0 !== this.__getterCache.rows) {
                return this.__getterCache.rows
            }
            return this.__getterCache.rows = _this4.props.visibleRows.map((function(row, index) {
                return _extends({}, row, {
                    index: index,
                    reactKey: (0, _utils.getReactRowKey)(row, index)
                })
            }))
        }
    }, {
        key: "isEmpty",
        get: function() {
            return 0 === this.plugins.getValue(_plugins.TotalCount)
        }
    }, {
        key: "restAttributes",
        get: function() {
            var _this$props = this.props,
                restProps = (_this$props.columns, _this$props.noDataTemplate, _this$props.visibleRows, _objectWithoutProperties(_this$props, _excluded));
            return restProps
        }
    }]);
    return TableContent
}(_inferno2.InfernoComponent);
exports.TableContent = TableContent;
TableContent.defaultProps = TableContentProps;
