/**
 * DevExtreme (esm/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/
 */
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import _extends from "@babel/runtime/helpers/esm/extends";
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"];
import {
    createVNode,
    createComponentVNode,
    normalizeProps
} from "inferno";
import {
    InfernoEffect,
    InfernoComponent
} from "@devextreme/runtime/inferno";
import {
    Plugins
} from "../../../utils/plugin/context";
import {
    ValueSetter
} from "../../../utils/plugin/value_setter";
import {
    GetterExtender
} from "../../../utils/plugin/getter_extender";
import {
    Widget
} from "../../common/widget";
import {
    BaseWidgetProps
} from "../../common/base_props";
import {
    TableContent
} from "./views/table_content";
import {
    TableHeader
} from "./views/table_header";
import {
    Footer
} from "./views/footer";
import CLASSES from "./classes";
import {
    LocalData,
    Columns,
    KeyExprPlugin,
    RemoteOperations,
    DataStateValue,
    VisibleColumns,
    LocalVisibleItems,
    VisibleRows,
    LocalDataState,
    VisibleDataRows,
    CalculateLocalDataState,
    LoadOptionsValue
} from "./plugins";

function isStore(dataSource) {
    return void 0 !== dataSource && !Array.isArray(dataSource)
}
var defaultDataState = {
    data: [],
    totalCount: 0
};
export var viewFunction = viewModel => normalizeProps(createComponentVNode(2, 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: [createComponentVNode(2, ValueSetter, {
        type: LocalData,
        value: viewModel.localData
    }), createComponentVNode(2, ValueSetter, {
        type: Columns,
        value: viewModel.columns
    }), createComponentVNode(2, ValueSetter, {
        type: KeyExprPlugin,
        value: viewModel.keyExpr
    }), createComponentVNode(2, ValueSetter, {
        type: RemoteOperations,
        value: viewModel.props.remoteOperations
    }), createComponentVNode(2, ValueSetter, {
        type: DataStateValue,
        value: viewModel.dataState
    }), createComponentVNode(2, GetterExtender, {
        type: VisibleColumns,
        order: -1,
        value: Columns
    }), createComponentVNode(2, GetterExtender, {
        type: LocalVisibleItems,
        order: -1,
        value: LocalData
    }), createComponentVNode(2, GetterExtender, {
        type: VisibleRows,
        order: -1,
        value: VisibleDataRows
    }), createComponentVNode(2, GetterExtender, {
        type: LocalDataState,
        order: -1,
        value: CalculateLocalDataState
    }), createVNode(1, "div", "".concat(CLASSES.dataGrid, " ").concat(CLASSES.gridBaseContainer), [createComponentVNode(2, TableHeader, {
        columns: viewModel.visibleColumns
    }), createComponentVNode(2, TableContent, {
        columns: viewModel.visibleColumns,
        visibleRows: viewModel.visibleRows,
        noDataTemplate: viewModel.props.noDataTemplate
    }), createComponentVNode(2, Footer), viewModel.props.children], 0, {
        role: "grid",
        "aria-label": "Data grid"
    })]
})));
export var DataGridNextProps = Object.create(Object.prototype, _extends(Object.getOwnPropertyDescriptors(BaseWidgetProps), Object.getOwnPropertyDescriptors({
    remoteOperations: false,
    cacheEnabled: true,
    columns: Object.freeze([]),
    defaultDataState: void 0,
    dataStateChange: () => {}
})));
var aria = {
    role: "presentation"
};
var getTemplate = TemplateProp => TemplateProp && (TemplateProp.defaultProps ? props => normalizeProps(createComponentVNode(2, TemplateProp, _extends({}, props))) : TemplateProp);
export class DataGridNext extends InfernoComponent {
    constructor(props) {
        super(props);
        this.plugins = new 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(this);
        this.updateVisibleColumns = this.updateVisibleColumns.bind(this);
        this.updateDataStateFromLocal = this.updateDataStateFromLocal.bind(this);
        this.loadDataSource = this.loadDataSource.bind(this);
        this.refresh = this.refresh.bind(this);
        this.loadDataSourceIfNeed = this.loadDataSourceIfNeed.bind(this);
        this.loadStore = this.loadStore.bind(this)
    }
    createEffects() {
        return [new InfernoEffect(this.updateVisibleRows, [this.plugins]), new InfernoEffect(this.updateVisibleColumns, [this.plugins]), new InfernoEffect(this.updateDataStateFromLocal, [this.plugins, this.props.dataStateChange]), new InfernoEffect(this.loadDataSource, [this.props.cacheEnabled, this.props.dataSource, this.plugins, this.props.remoteOperations, this.props.dataStateChange, this.props.onDataErrorOccurred])]
    }
    updateEffects() {
        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])
    }
    getChildContext() {
        return _extends({}, this.context, {
            PluginsContext: this.plugins
        })
    }
    updateVisibleRows() {
        return this.plugins.watch(VisibleRows, visibleRows => {
            this.setState(__state_argument => ({
                visibleRows: visibleRows
            }))
        })
    }
    updateVisibleColumns() {
        return this.plugins.watch(VisibleColumns, columns => {
            this.setState(__state_argument => ({
                visibleColumns: columns
            }))
        })
    }
    updateDataStateFromLocal() {
        return this.plugins.watch(LocalDataState, dataState => {
            if (void 0 !== dataState) {
                var __newValue;
                this.setState(__state_argument => {
                    __newValue = dataState;
                    return {
                        dataState: __newValue
                    }
                });
                this.props.dataStateChange(__newValue)
            }
        })
    }
    loadDataSource() {
        var {
            cacheEnabled: cacheEnabled,
            dataSource: dataSource
        } = this.props;
        var prevLoadOptions = void 0;
        return this.plugins.watch(LoadOptionsValue, loadOptions => {
            if (!cacheEnabled || JSON.stringify(loadOptions) !== JSON.stringify(prevLoadOptions)) {
                prevLoadOptions = loadOptions;
                this.loadDataSourceIfNeed(dataSource, loadOptions)
            }
        })
    }
    get aria() {
        return aria
    }
    get dataState() {
        if (void 0 !== this.__getterCache.dataState) {
            return this.__getterCache.dataState
        }
        return this.__getterCache.dataState = (() => {
            var _ref;
            return null !== (_ref = void 0 !== this.props.dataState ? this.props.dataState : this.state.dataState) && void 0 !== _ref ? _ref : defaultDataState
        })()
    }
    get keyExpr() {
        var _ref2, _this$props$keyExpr;
        var {
            dataSource: dataSource
        } = this.props;
        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
    }
    get columns() {
        if (void 0 !== this.__getterCache.columns) {
            return this.__getterCache.columns
        }
        return this.__getterCache.columns = (() => {
            var userColumns = this.props.columns;
            return userColumns.map(userColumn => ({
                dataField: userColumn
            }))
        })()
    }
    get localData() {
        if (void 0 !== this.__getterCache.localData) {
            return this.__getterCache.localData
        }
        return this.__getterCache.localData = (() => {
            var {
                dataSource: dataSource
            } = this.props;
            return Array.isArray(dataSource) ? dataSource : this.state.loadedData
        })()
    }
    loadDataSourceIfNeed(dataSource, loadOptions) {
        if (isStore(dataSource)) {
            this.loadStore(dataSource, loadOptions)
        }
    }
    loadStore(store, loadOptions) {
        store.load(loadOptions).then((data, extra) => {
            if (this.props.remoteOperations) {
                if (Array.isArray(data)) {
                    var __newValue;
                    this.setState(__state_argument => {
                        __newValue = _extends({
                            dataOffset: loadOptions.skip,
                            data: data
                        }, extra);
                        return {
                            dataState: __newValue
                        }
                    });
                    this.props.dataStateChange(__newValue)
                } else {
                    var _newValue;
                    this.setState(__state_argument => {
                        _newValue = _extends({}, data, {
                            dataOffset: loadOptions.skip
                        });
                        return {
                            dataState: _newValue
                        }
                    });
                    this.props.dataStateChange(_newValue)
                }
            } else {
                this.setState(__state_argument => ({
                    loadedData: data
                }))
            }
        }, error => {
            var _this$props$onDataErr, _this$props;
            null === (_this$props$onDataErr = (_this$props = this.props).onDataErrorOccurred) || void 0 === _this$props$onDataErr ? void 0 : _this$props$onDataErr.call(_this$props, {
                error: error
            })
        })
    }
    get restAttributes() {
        var _this$props$dataState = _extends({}, this.props, {
                dataState: void 0 !== this.props.dataState ? this.props.dataState : this.state.dataState
            }),
            restProps = _objectWithoutPropertiesLoose(_this$props$dataState, _excluded);
        return restProps
    }
    refresh() {
        var _this$plugins$getValu;
        var loadOptions = null !== (_this$plugins$getValu = this.plugins.getValue(LoadOptionsValue)) && void 0 !== _this$plugins$getValu ? _this$plugins$getValu : {};
        this.loadDataSourceIfNeed(this.props.dataSource, loadOptions)
    }
    componentWillUpdate(nextProps, nextState, context) {
        super.componentWillUpdate();
        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
        }
    }
    render() {
        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
        })
    }
}
DataGridNext.defaultProps = DataGridNextProps;
