/**
 * DevExtreme (renovation/ui/grids/data_grid_next/data_grid_next.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.DataGridNextProps = exports.DataGridNext = 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 _widget = require("../../common/widget");
var _base_props = require("../../common/base_props");
var _table_content = require("./views/table_content");
var _table_header = require("./views/table_header");
var _footer = require("./views/footer");
var _classes = _interopRequireDefault(require("./classes"));
var _plugins = require("./plugins");
var _excluded = ["accessKey", "activeStateEnabled", "cacheEnabled", "children", "className", "columns", "dataSource", "dataState", "dataStateChange", "defaultDataState", "disabled", "focusStateEnabled", "height", "hint", "hoverStateEnabled", "keyExpr", "noDataTemplate", "onClick", "onDataErrorOccurred", "onKeyDown", "remoteOperations", "rtlEnabled", "tabIndex", "visible", "width"];

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

function isStore(dataSource) {
    return void 0 !== dataSource && !Array.isArray(dataSource)
}
var defaultDataState = {
    data: [],
    totalCount: 0
};
var viewFunction = function(viewModel) {
    return (0, _inferno.normalizeProps)((0, _inferno.createComponentVNode)(2, _widget.Widget, _extends({
        accessKey: viewModel.props.accessKey,
        activeStateEnabled: viewModel.props.activeStateEnabled,
        aria: viewModel.aria,
        className: viewModel.props.className,
        disabled: viewModel.props.disabled,
        focusStateEnabled: viewModel.props.focusStateEnabled,
        height: viewModel.props.height,
        hint: viewModel.props.hint,
        hoverStateEnabled: viewModel.props.hoverStateEnabled,
        rtlEnabled: viewModel.props.rtlEnabled,
        tabIndex: viewModel.props.tabIndex,
        visible: viewModel.props.visible,
        width: viewModel.props.width
    }, viewModel.restAttributes, {
        children: [(0, _inferno.createComponentVNode)(2, _value_setter.ValueSetter, {
            type: _plugins.LocalData,
            value: viewModel.localData
        }), (0, _inferno.createComponentVNode)(2, _value_setter.ValueSetter, {
            type: _plugins.Columns,
            value: viewModel.columns
        }), (0, _inferno.createComponentVNode)(2, _value_setter.ValueSetter, {
            type: _plugins.KeyExprPlugin,
            value: viewModel.keyExpr
        }), (0, _inferno.createComponentVNode)(2, _value_setter.ValueSetter, {
            type: _plugins.RemoteOperations,
            value: viewModel.props.remoteOperations
        }), (0, _inferno.createComponentVNode)(2, _value_setter.ValueSetter, {
            type: _plugins.DataStateValue,
            value: viewModel.dataState
        }), (0, _inferno.createComponentVNode)(2, _getter_extender.GetterExtender, {
            type: _plugins.VisibleColumns,
            order: -1,
            value: _plugins.Columns
        }), (0, _inferno.createComponentVNode)(2, _getter_extender.GetterExtender, {
            type: _plugins.LocalVisibleItems,
            order: -1,
            value: _plugins.LocalData
        }), (0, _inferno.createComponentVNode)(2, _getter_extender.GetterExtender, {
            type: _plugins.VisibleRows,
            order: -1,
            value: _plugins.VisibleDataRows
        }), (0, _inferno.createComponentVNode)(2, _getter_extender.GetterExtender, {
            type: _plugins.LocalDataState,
            order: -1,
            value: _plugins.CalculateLocalDataState
        }), (0, _inferno.createVNode)(1, "div", "".concat(_classes.default.dataGrid, " ").concat(_classes.default.gridBaseContainer), [(0, _inferno.createComponentVNode)(2, _table_header.TableHeader, {
            columns: viewModel.visibleColumns
        }), (0, _inferno.createComponentVNode)(2, _table_content.TableContent, {
            columns: viewModel.visibleColumns,
            visibleRows: viewModel.visibleRows,
            noDataTemplate: viewModel.props.noDataTemplate
        }), (0, _inferno.createComponentVNode)(2, _footer.Footer), viewModel.props.children], 0, {
            role: "grid",
            "aria-label": "Data grid"
        })]
    })))
};
exports.viewFunction = viewFunction;
var DataGridNextProps = Object.create(Object.prototype, _extends(Object.getOwnPropertyDescriptors(_base_props.BaseWidgetProps), Object.getOwnPropertyDescriptors({
    remoteOperations: false,
    cacheEnabled: true,
    columns: Object.freeze([]),
    defaultDataState: void 0,
    dataStateChange: function() {}
})));
exports.DataGridNextProps = DataGridNextProps;
var aria = {
    role: "presentation"
};
var getTemplate = function(TemplateProp) {
    return TemplateProp && (TemplateProp.defaultProps ? function(props) {
        return (0, _inferno.normalizeProps)((0, _inferno.createComponentVNode)(2, TemplateProp, _extends({}, props)))
    } : TemplateProp)
};
var DataGridNext = function(_InfernoComponent) {
    _inheritsLoose(DataGridNext, _InfernoComponent);

    function DataGridNext(props) {
        var _this;
        _this = _InfernoComponent.call(this, props) || this;
        _this.plugins = new _context.Plugins;
        _this.__getterCache = {};
        _this.state = {
            visibleRows: [],
            visibleColumns: [],
            loadedData: void 0,
            dataState: void 0 !== _this.props.dataState ? _this.props.dataState : _this.props.defaultDataState
        };
        _this.updateVisibleRows = _this.updateVisibleRows.bind(_assertThisInitialized(_this));
        _this.updateVisibleColumns = _this.updateVisibleColumns.bind(_assertThisInitialized(_this));
        _this.updateDataStateFromLocal = _this.updateDataStateFromLocal.bind(_assertThisInitialized(_this));
        _this.loadDataSource = _this.loadDataSource.bind(_assertThisInitialized(_this));
        _this.refresh = _this.refresh.bind(_assertThisInitialized(_this));
        _this.loadDataSourceIfNeed = _this.loadDataSourceIfNeed.bind(_assertThisInitialized(_this));
        _this.loadStore = _this.loadStore.bind(_assertThisInitialized(_this));
        return _this
    }
    var _proto = DataGridNext.prototype;
    _proto.createEffects = function() {
        return [new _inferno2.InfernoEffect(this.updateVisibleRows, [this.plugins]), new _inferno2.InfernoEffect(this.updateVisibleColumns, [this.plugins]), new _inferno2.InfernoEffect(this.updateDataStateFromLocal, [this.plugins, this.props.dataStateChange]), new _inferno2.InfernoEffect(this.loadDataSource, [this.props.cacheEnabled, this.props.dataSource, this.plugins, this.props.remoteOperations, this.props.dataStateChange, this.props.onDataErrorOccurred])]
    };
    _proto.updateEffects = function() {
        var _this$_effects$, _this$_effects$2, _this$_effects$3, _this$_effects$4;
        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.props.dataStateChange]);
        null === (_this$_effects$4 = this._effects[3]) || void 0 === _this$_effects$4 ? void 0 : _this$_effects$4.update([this.props.cacheEnabled, this.props.dataSource, this.plugins, this.props.remoteOperations, this.props.dataStateChange, this.props.onDataErrorOccurred])
    };
    _proto.getChildContext = function() {
        return _extends({}, this.context, {
            PluginsContext: this.plugins
        })
    };
    _proto.updateVisibleRows = function() {
        var _this2 = this;
        return this.plugins.watch(_plugins.VisibleRows, (function(visibleRows) {
            _this2.setState((function(__state_argument) {
                return {
                    visibleRows: visibleRows
                }
            }))
        }))
    };
    _proto.updateVisibleColumns = function() {
        var _this3 = this;
        return this.plugins.watch(_plugins.VisibleColumns, (function(columns) {
            _this3.setState((function(__state_argument) {
                return {
                    visibleColumns: columns
                }
            }))
        }))
    };
    _proto.updateDataStateFromLocal = function() {
        var _this4 = this;
        return this.plugins.watch(_plugins.LocalDataState, (function(dataState) {
            if (void 0 !== dataState) {
                var __newValue;
                _this4.setState((function(__state_argument) {
                    __newValue = dataState;
                    return {
                        dataState: __newValue
                    }
                }));
                _this4.props.dataStateChange(__newValue)
            }
        }))
    };
    _proto.loadDataSource = function() {
        var _this5 = this;
        var _this$props = this.props,
            cacheEnabled = _this$props.cacheEnabled,
            dataSource = _this$props.dataSource;
        var prevLoadOptions = void 0;
        return this.plugins.watch(_plugins.LoadOptionsValue, (function(loadOptions) {
            if (!cacheEnabled || JSON.stringify(loadOptions) !== JSON.stringify(prevLoadOptions)) {
                prevLoadOptions = loadOptions;
                _this5.loadDataSourceIfNeed(dataSource, loadOptions)
            }
        }))
    };
    _proto.loadDataSourceIfNeed = function(dataSource, loadOptions) {
        if (isStore(dataSource)) {
            this.loadStore(dataSource, loadOptions)
        }
    };
    _proto.loadStore = function(store, loadOptions) {
        var _this6 = this;
        store.load(loadOptions).then((function(data, extra) {
            if (_this6.props.remoteOperations) {
                if (Array.isArray(data)) {
                    var __newValue;
                    _this6.setState((function(__state_argument) {
                        __newValue = _extends({
                            dataOffset: loadOptions.skip,
                            data: data
                        }, extra);
                        return {
                            dataState: __newValue
                        }
                    }));
                    _this6.props.dataStateChange(__newValue)
                } else {
                    var _newValue;
                    _this6.setState((function(__state_argument) {
                        _newValue = _extends({}, data, {
                            dataOffset: loadOptions.skip
                        });
                        return {
                            dataState: _newValue
                        }
                    }));
                    _this6.props.dataStateChange(_newValue)
                }
            } else {
                _this6.setState((function(__state_argument) {
                    return {
                        loadedData: data
                    }
                }))
            }
        }), (function(error) {
            var _this6$props$onDataEr, _this6$props;
            null === (_this6$props$onDataEr = (_this6$props = _this6.props).onDataErrorOccurred) || void 0 === _this6$props$onDataEr ? void 0 : _this6$props$onDataEr.call(_this6$props, {
                error: error
            })
        }))
    };
    _proto.refresh = function() {
        var _this$plugins$getValu;
        var loadOptions = null !== (_this$plugins$getValu = this.plugins.getValue(_plugins.LoadOptionsValue)) && void 0 !== _this$plugins$getValu ? _this$plugins$getValu : {};
        this.loadDataSourceIfNeed(this.props.dataSource, loadOptions)
    };
    _proto.componentWillUpdate = function(nextProps, nextState, context) {
        _InfernoComponent.prototype.componentWillUpdate.call(this);
        if (this.state.dataState !== nextState.dataState || this.props.dataState !== nextProps.dataState) {
            this.__getterCache.dataState = void 0
        }
        if (this.props.columns !== nextProps.columns) {
            this.__getterCache.columns = void 0
        }
        if (this.props.dataSource !== nextProps.dataSource || this.state.loadedData !== nextState.loadedData) {
            this.__getterCache.localData = void 0
        }
    };
    _proto.render = function() {
        var props = this.props;
        return viewFunction({
            props: _extends({}, props, {
                dataState: void 0 !== this.props.dataState ? this.props.dataState : this.state.dataState,
                noDataTemplate: getTemplate(props.noDataTemplate)
            }),
            visibleRows: this.state.visibleRows,
            visibleColumns: this.state.visibleColumns,
            loadedData: this.state.loadedData,
            plugins: this.plugins,
            aria: this.aria,
            dataState: this.dataState,
            keyExpr: this.keyExpr,
            columns: this.columns,
            localData: this.localData,
            loadDataSourceIfNeed: this.loadDataSourceIfNeed,
            loadStore: this.loadStore,
            restAttributes: this.restAttributes
        })
    };
    _createClass(DataGridNext, [{
        key: "aria",
        get: function() {
            return aria
        }
    }, {
        key: "dataState",
        get: function() {
            var _this7 = this;
            if (void 0 !== this.__getterCache.dataState) {
                return this.__getterCache.dataState
            }
            return this.__getterCache.dataState = null !== (_ref = void 0 !== _this7.props.dataState ? _this7.props.dataState : _this7.state.dataState) && void 0 !== _ref ? _ref : defaultDataState;
            var _ref
        }
    }, {
        key: "keyExpr",
        get: function() {
            var _ref2, _this$props$keyExpr;
            var dataSource = this.props.dataSource;
            var storeKey = isStore(dataSource) ? dataSource.key() : null;
            return null !== (_ref2 = null !== (_this$props$keyExpr = this.props.keyExpr) && void 0 !== _this$props$keyExpr ? _this$props$keyExpr : storeKey) && void 0 !== _ref2 ? _ref2 : null
        }
    }, {
        key: "columns",
        get: function() {
            var _this8 = this;
            if (void 0 !== this.__getterCache.columns) {
                return this.__getterCache.columns
            }
            return this.__getterCache.columns = (userColumns = _this8.props.columns, userColumns.map((function(userColumn) {
                return {
                    dataField: userColumn
                }
            })));
            var userColumns
        }
    }, {
        key: "localData",
        get: function() {
            var _this9 = this;
            if (void 0 !== this.__getterCache.localData) {
                return this.__getterCache.localData
            }
            return this.__getterCache.localData = (dataSource = _this9.props.dataSource, Array.isArray(dataSource) ? dataSource : _this9.state.loadedData);
            var dataSource
        }
    }, {
        key: "restAttributes",
        get: function() {
            var _this$props$dataState = _extends({}, this.props, {
                    dataState: void 0 !== this.props.dataState ? this.props.dataState : this.state.dataState
                }),
                restProps = (_this$props$dataState.accessKey, _this$props$dataState.activeStateEnabled, _this$props$dataState.cacheEnabled, _this$props$dataState.children, _this$props$dataState.className, _this$props$dataState.columns, _this$props$dataState.dataSource, _this$props$dataState.dataState, _this$props$dataState.dataStateChange, _this$props$dataState.defaultDataState, _this$props$dataState.disabled, _this$props$dataState.focusStateEnabled, _this$props$dataState.height, _this$props$dataState.hint, _this$props$dataState.hoverStateEnabled, _this$props$dataState.keyExpr, _this$props$dataState.noDataTemplate, _this$props$dataState.onClick, _this$props$dataState.onDataErrorOccurred, _this$props$dataState.onKeyDown, _this$props$dataState.remoteOperations, _this$props$dataState.rtlEnabled, _this$props$dataState.tabIndex, _this$props$dataState.visible, _this$props$dataState.width, _objectWithoutProperties(_this$props$dataState, _excluded));
            return restProps
        }
    }]);
    return DataGridNext
}(_inferno2.InfernoComponent);
exports.DataGridNext = DataGridNext;
DataGridNext.defaultProps = DataGridNextProps;
