/**
 * DevExtreme (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/
 */
"use strict";
exports.viewFunction = exports.DataGridNextMasterDetailProps = exports.DataGridNextMasterDetail = exports.AddMasterDetailRows = void 0;
var _inferno = require("inferno");
var _inferno2 = require("@devextreme/runtime/inferno");
var _context = require("../../../../utils/plugin/context");
var _plugins = require("../plugins");
var _getter_extender = require("../../../../utils/plugin/getter_extender");
var _expand_column = require("./expand_column");
var _plugins2 = require("./plugins");
var _master_detail_row = require("./master_detail_row");
var _classes = _interopRequireDefault(require("../classes"));
var _excluded = ["defaultExpandedRowKeys", "enabled", "expandedRowKeys", "expandedRowKeysChange", "template"];

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 _toConsumableArray(arr) {
    return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread()
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}

function _unsupportedIterableToArray(o, minLen) {
    if (!o) {
        return
    }
    if ("string" === typeof o) {
        return _arrayLikeToArray(o, minLen)
    }
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if ("Object" === n && o.constructor) {
        n = o.constructor.name
    }
    if ("Map" === n || "Set" === n) {
        return Array.from(o)
    }
    if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) {
        return _arrayLikeToArray(o, minLen)
    }
}

function _iterableToArray(iter) {
    if ("undefined" !== typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) {
        return Array.from(iter)
    }
}

function _arrayWithoutHoles(arr) {
    if (Array.isArray(arr)) {
        return _arrayLikeToArray(arr)
    }
}

function _arrayLikeToArray(arr, len) {
    if (null == len || len > arr.length) {
        len = arr.length
    }
    for (var i = 0, arr2 = new Array(len); i < len; i++) {
        arr2[i] = arr[i]
    }
    return arr2
}

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 AddMasterDetailRows = (0, _context.createSelector)([_plugins.VisibleRows, _plugins2.IsExpanded], (function(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: _master_detail_row.MasterDetailRow
            }));
            i += 1
        } else if ("detail" === item.rowType) {
            result.splice(i, 1)
        }
    }
    return result
}));
exports.AddMasterDetailRows = AddMasterDetailRows;
var viewFunction = function() {
    return (0, _inferno.createFragment)([(0, _inferno.createComponentVNode)(2, _getter_extender.GetterExtender, {
        type: _plugins.VisibleRows,
        order: 2,
        value: AddMasterDetailRows
    })], 4)
};
exports.viewFunction = viewFunction;
var DataGridNextMasterDetailProps = {
    enabled: true,
    defaultExpandedRowKeys: Object.freeze([]),
    expandedRowKeysChange: function() {}
};
exports.DataGridNextMasterDetailProps = DataGridNextMasterDetailProps;
var getTemplate = function(TemplateProp) {
    return TemplateProp && (TemplateProp.defaultProps ? function(props) {
        return (0, _inferno.normalizeProps)((0, _inferno.createComponentVNode)(2, TemplateProp, _extends({}, props)))
    } : TemplateProp)
};
var DataGridNextMasterDetail = function(_InfernoComponent) {
    _inheritsLoose(DataGridNextMasterDetail, _InfernoComponent);

    function DataGridNextMasterDetail(props) {
        var _this;
        _this = _InfernoComponent.call(this, props) || this;
        _this.state = {
            expandedRowKeys: void 0 !== _this.props.expandedRowKeys ? _this.props.expandedRowKeys : _this.props.defaultExpandedRowKeys
        };
        _this.setMasterDetailTemplate = _this.setMasterDetailTemplate.bind(_assertThisInitialized(_this));
        _this.addVisibleColumnsHandler = _this.addVisibleColumnsHandler.bind(_assertThisInitialized(_this));
        _this.addPluginMethods = _this.addPluginMethods.bind(_assertThisInitialized(_this));
        _this.isExpanded = _this.isExpanded.bind(_assertThisInitialized(_this));
        _this.setExpanded = _this.setExpanded.bind(_assertThisInitialized(_this));
        return _this
    }
    var _proto = DataGridNextMasterDetail.prototype;
    _proto.createEffects = function() {
        return [new _inferno2.InfernoEffect(this.setMasterDetailTemplate, [this.plugins, this.props.template]), new _inferno2.InfernoEffect(this.addVisibleColumnsHandler, [this.props.enabled, this.plugins]), new _inferno2.InfernoEffect(this.addPluginMethods, [this.plugins, this.props.expandedRowKeysChange, this.state.expandedRowKeys, this.props.expandedRowKeys])]
    };
    _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.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])
    };
    _proto.setMasterDetailTemplate = function() {
        this.plugins.set(_plugins2.MasterDetailTemplate, this.props.template)
    };
    _proto.addVisibleColumnsHandler = function() {
        if (this.props.enabled) {
            return this.plugins.extend(_plugins.VisibleColumns, 1, (function(columns) {
                var expandColumn = {
                    headerCssClass: "".concat(_classes.default.commandExpand, " ").concat(_classes.default.groupSpace),
                    cellContainerTemplate: _expand_column.ExpandColumn
                };
                return [expandColumn].concat(_toConsumableArray(columns))
            }))
        }
        return
    };
    _proto.addPluginMethods = function() {
        this.plugins.set(_plugins2.SetExpanded, this.setExpanded);
        this.plugins.set(_plugins2.IsExpanded, this.isExpanded)
    };
    _proto.isExpanded = function(key) {
        return (void 0 !== this.props.expandedRowKeys ? this.props.expandedRowKeys : this.state.expandedRowKeys).includes(key)
    };
    _proto.setExpanded = function(key, value) {
        var _this2 = this;
        if (value) {
            var __newValue;
            this.setState((function(__state_argument) {
                __newValue = [].concat(_toConsumableArray(void 0 !== _this2.props.expandedRowKeys ? _this2.props.expandedRowKeys : __state_argument.expandedRowKeys), [key]);
                return {
                    expandedRowKeys: __newValue
                }
            }));
            this.props.expandedRowKeysChange(__newValue)
        } else {
            var _newValue;
            this.setState((function(__state_argument) {
                _newValue = (void 0 !== _this2.props.expandedRowKeys ? _this2.props.expandedRowKeys : __state_argument.expandedRowKeys).filter((function(i) {
                    return i !== key
                }));
                return {
                    expandedRowKeys: _newValue
                }
            }));
            this.props.expandedRowKeysChange(_newValue)
        }
    };
    _proto.render = function() {
        this.props;
        return viewFunction()
    };
    _createClass(DataGridNextMasterDetail, [{
        key: "plugins",
        get: function() {
            if ("PluginsContext" in this.context) {
                return this.context.PluginsContext
            }
            return _context.PluginsContext
        }
    }, {
        key: "restAttributes",
        get: function() {
            var _this$props$expandedR = _extends({}, this.props, {
                    expandedRowKeys: void 0 !== this.props.expandedRowKeys ? this.props.expandedRowKeys : this.state.expandedRowKeys
                }),
                restProps = (_this$props$expandedR.defaultExpandedRowKeys, _this$props$expandedR.enabled, _this$props$expandedR.expandedRowKeys, _this$props$expandedR.expandedRowKeysChange, _this$props$expandedR.template, _objectWithoutProperties(_this$props$expandedR, _excluded));
            return restProps
        }
    }]);
    return DataGridNextMasterDetail
}(_inferno2.InfernoComponent);
exports.DataGridNextMasterDetail = DataGridNextMasterDetail;
DataGridNextMasterDetail.defaultProps = DataGridNextMasterDetailProps;
