/**
 * DevExtreme (esm/renovation/ui/grids/data_grid_next/master_detail/master_detail.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 = ["defaultExpandedRowKeys", "enabled", "expandedRowKeys", "expandedRowKeysChange", "template"];
import {
    createFragment,
    createComponentVNode,
    normalizeProps
} from "inferno";
import {
    Fragment
} from "inferno";
import {
    InfernoEffect,
    InfernoComponent
} from "@devextreme/runtime/inferno";
import {
    PluginsContext,
    createSelector
} from "../../../../utils/plugin/context";
import {
    VisibleRows,
    VisibleColumns
} from "../plugins";
import {
    GetterExtender
} from "../../../../utils/plugin/getter_extender";
import {
    ExpandColumn
} from "./expand_column";
import {
    SetExpanded,
    IsExpanded,
    MasterDetailTemplate
} from "./plugins";
import {
    MasterDetailRow
} from "./master_detail_row";
import CLASSES from "../classes";
export var AddMasterDetailRows = createSelector([VisibleRows, IsExpanded], (visibleRows, isExpanded) => {
    var result = visibleRows.slice();
    for (var i = 0; i < result.length; i += 1) {
        var item = result[i];
        if (isExpanded(item.key)) {
            result.splice(i + 1, 0, _extends({}, item, {
                rowType: "detail",
                template: MasterDetailRow
            }));
            i += 1
        } else if ("detail" === item.rowType) {
            result.splice(i, 1)
        }
    }
    return result
});
export var viewFunction = () => createFragment([createComponentVNode(2, GetterExtender, {
    type: VisibleRows,
    order: 2,
    value: AddMasterDetailRows
})], 4);
export var DataGridNextMasterDetailProps = {
    enabled: true,
    defaultExpandedRowKeys: Object.freeze([]),
    expandedRowKeysChange: () => {}
};
var getTemplate = TemplateProp => TemplateProp && (TemplateProp.defaultProps ? props => normalizeProps(createComponentVNode(2, TemplateProp, _extends({}, props))) : TemplateProp);
export class DataGridNextMasterDetail extends InfernoComponent {
    constructor(props) {
        super(props);
        this.state = {
            expandedRowKeys: void 0 !== this.props.expandedRowKeys ? this.props.expandedRowKeys : this.props.defaultExpandedRowKeys
        };
        this.setMasterDetailTemplate = this.setMasterDetailTemplate.bind(this);
        this.addVisibleColumnsHandler = this.addVisibleColumnsHandler.bind(this);
        this.addPluginMethods = this.addPluginMethods.bind(this);
        this.isExpanded = this.isExpanded.bind(this);
        this.setExpanded = this.setExpanded.bind(this)
    }
    get plugins() {
        if ("PluginsContext" in this.context) {
            return this.context.PluginsContext
        }
        return PluginsContext
    }
    createEffects() {
        return [new InfernoEffect(this.setMasterDetailTemplate, [this.plugins, this.props.template]), new InfernoEffect(this.addVisibleColumnsHandler, [this.props.enabled, this.plugins]), new InfernoEffect(this.addPluginMethods, [this.plugins, this.props.expandedRowKeysChange, this.state.expandedRowKeys, this.props.expandedRowKeys])]
    }
    updateEffects() {
        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.template]);
        null === (_this$_effects$2 = this._effects[1]) || void 0 === _this$_effects$2 ? void 0 : _this$_effects$2.update([this.props.enabled, this.plugins]);
        null === (_this$_effects$3 = this._effects[2]) || void 0 === _this$_effects$3 ? void 0 : _this$_effects$3.update([this.plugins, this.props.expandedRowKeysChange, this.state.expandedRowKeys, this.props.expandedRowKeys])
    }
    setMasterDetailTemplate() {
        this.plugins.set(MasterDetailTemplate, this.props.template)
    }
    addVisibleColumnsHandler() {
        if (this.props.enabled) {
            return this.plugins.extend(VisibleColumns, 1, columns => {
                var expandColumn = {
                    headerCssClass: "".concat(CLASSES.commandExpand, " ").concat(CLASSES.groupSpace),
                    cellContainerTemplate: ExpandColumn
                };
                return [expandColumn, ...columns]
            })
        }
        return
    }
    addPluginMethods() {
        this.plugins.set(SetExpanded, this.setExpanded);
        this.plugins.set(IsExpanded, this.isExpanded)
    }
    isExpanded(key) {
        return (void 0 !== this.props.expandedRowKeys ? this.props.expandedRowKeys : this.state.expandedRowKeys).includes(key)
    }
    setExpanded(key, value) {
        if (value) {
            var __newValue;
            this.setState(__state_argument => {
                __newValue = [...void 0 !== this.props.expandedRowKeys ? this.props.expandedRowKeys : __state_argument.expandedRowKeys, key];
                return {
                    expandedRowKeys: __newValue
                }
            });
            this.props.expandedRowKeysChange(__newValue)
        } else {
            var _newValue;
            this.setState(__state_argument => {
                _newValue = (void 0 !== this.props.expandedRowKeys ? this.props.expandedRowKeys : __state_argument.expandedRowKeys).filter(i => i !== key);
                return {
                    expandedRowKeys: _newValue
                }
            });
            this.props.expandedRowKeysChange(_newValue)
        }
    }
    get restAttributes() {
        var _this$props$expandedR = _extends({}, this.props, {
                expandedRowKeys: void 0 !== this.props.expandedRowKeys ? this.props.expandedRowKeys : this.state.expandedRowKeys
            }),
            restProps = _objectWithoutPropertiesLoose(_this$props$expandedR, _excluded);
        return restProps
    }
    render() {
        this.props;
        return viewFunction()
    }
}
DataGridNextMasterDetail.defaultProps = DataGridNextMasterDetailProps;
