import { __assign, __extends, __rest, __spreadArrays } from "tslib";
import isEqual from 'lodash/isEqual';
import union from 'lodash/union';
import React, { Component } from 'react';
import { Input, Icon, Tree } from '@alicloud/console-components';
import classnames from 'classnames';
import TreeNode from './TreeNode';
;
var IotTree = /** @class */ (function (_super) {
    __extends(IotTree, _super);
    function IotTree(props) {
        var _this = _super.call(this, props) || this;
        _this.UNSAFE_componentWillReceiveProps = function (nextProps) {
            if (isEqual(nextProps, _this.props)) {
                return;
            }
            _this.updateState(nextProps);
        };
        _this.onSelect = function (key, e) {
            if (_this.props.disabled)
                return;
            var _a = _this.state, expandedKeys = _a.expandedKeys, selectedKeys = _a.selectedKeys;
            var expandedKeys_ = expandedKeys.slice(0);
            var selectedKeys_ = selectedKeys.slice(0);
            if (e.selected) {
                expandedKeys_ = union(key, _this.state.expandedKeys);
                selectedKeys_ = key;
            }
            else {
                for (var i = 0; i < key.length; i++) {
                    var index = expandedKeys_.indexOf(key[i]);
                    var index_ = selectedKeys_.indexOf(key[i]);
                    if (index !== -1) {
                        expandedKeys_.splice(index, 1);
                    }
                    if (index_ !== -1) {
                        selectedKeys_.splice(index_, 1);
                    }
                }
            }
            _this.setState({
                expandedKeys: expandedKeys_,
                selectedKeys: selectedKeys_
            });
            _this.props.onSelect && _this.props.onSelect(key, e);
        };
        _this.onSearch = function (value) {
            if (_this.props.disabled)
                return;
            var matchedKeys = [];
            var expandedKeys = [];
            var loop = function (data) {
                data.forEach(function (item) {
                    if (item.label.indexOf(value.trim()) > -1) {
                        matchedKeys.push(item.id);
                    }
                    if (item.children && item.children.length) {
                        loop(item.children);
                    }
                });
            };
            if (value && value.trim() !== "") {
                loop(_this.state.dataSource);
                expandedKeys = matchedKeys;
            }
            else {
                if (_this.props.selectedNodeId) {
                    if (Array.isArray(_this.props.selectedNodeId)) {
                        expandedKeys = _this.props.selectedNodeId;
                    }
                    else if (_this.props.selectedNodeId) {
                        expandedKeys = [_this.props.selectedNodeId];
                    }
                    expandedKeys = [_this.props.selectedNodeId];
                }
                else {
                    var rootId = _this.getRootId(_this.state.dataSource);
                    //如果有异步数据根结点不展开，除非指定selectedNodeId
                    if (!_this.props.loadData) {
                        expandedKeys = [rootId];
                    }
                }
                matchedKeys = [];
            }
            _this.setState({
                matchedKeys: matchedKeys,
                searchValue: value,
                expandedKeys: expandedKeys,
            });
        };
        _this.deleteChildNode = function (nodes, ids) {
            nodes.props.children.forEach(function (node) {
                var index = ids.indexOf(node.props.eventKey);
                if (index !== -1) {
                    ids.splice(index, 1);
                }
                if (node.props.children && node.props.children.length > 0) {
                    _this.deleteChildNode(node, ids);
                }
            });
        };
        _this.onExpand = function (keys, e) {
            var expandedKeys_ = _this.state.expandedKeys.slice(0);
            if (e.expanded) {
                expandedKeys_ = union(expandedKeys_, keys);
            }
            else { //如果自己手动选择展开收起，那么以前保存的selectedKeys中子节点下的expandedKeys_要剔除
                var index = expandedKeys_.indexOf(e.node.props.eventKey);
                expandedKeys_.splice(index, 1);
                if (e.node.props.children && e.node.props.children.length > 0) {
                    _this.deleteChildNode(e.node, expandedKeys_);
                }
            }
            _this.setState({
                expandedKeys: expandedKeys_,
            });
            _this.props.onExpand && _this.props.onExpand(keys, e);
        };
        _this.state = {
            selectedKeys: [],
            dataSource: [],
            searchValue: '',
            expandedKeys: [],
            matchedKeys: []
        };
        return _this;
    }
    IotTree.prototype.componentDidMount = function () {
        this.updateState(this.props);
    };
    //做expandedKeys和treeSource处理
    IotTree.prototype.updateState = function (nextProps) {
        var treeSource = nextProps.treeSource, dataSource = nextProps.dataSource, extraExpandedKeys = nextProps.extraExpandedKeys, selectedNodeId = nextProps.selectedNodeId, loadData = nextProps.loadData;
        var expandedKeys = this.state.expandedKeys;
        var data = treeSource || dataSource;
        var treeSource_data = [];
        if (Array.isArray(data)) {
            treeSource_data = data.slice();
        }
        else if (data) {
            treeSource_data = [data];
        }
        var rootId = this.getRootId(treeSource_data);
        var toExpandedKeys = [];
        //如果有异步数据根结点不展开，除非指定selectedNodeId
        if (!loadData) {
            toExpandedKeys = [rootId];
        }
        if (expandedKeys.length) {
            toExpandedKeys = expandedKeys;
        }
        if (extraExpandedKeys) {
            if (Array.isArray(extraExpandedKeys)) {
                toExpandedKeys = union(toExpandedKeys, __spreadArrays(extraExpandedKeys));
            }
            else if (extraExpandedKeys) {
                toExpandedKeys = union(toExpandedKeys, [extraExpandedKeys]);
            }
        }
        if (extraExpandedKeys) {
            if (Array.isArray(extraExpandedKeys)) {
                toExpandedKeys = union(toExpandedKeys, __spreadArrays(extraExpandedKeys));
            }
            else {
                toExpandedKeys = union(toExpandedKeys, [extraExpandedKeys]);
            }
        }
        var selectedKeys_ = this.state.selectedKeys;
        if (selectedNodeId) {
            if (Array.isArray(selectedNodeId)) {
                selectedKeys_ = selectedNodeId;
            }
            else {
                selectedKeys_ = [selectedNodeId];
            }
            toExpandedKeys = union(toExpandedKeys, selectedKeys_);
        }
        var disabledKeys = this.getDisabledKeys(treeSource_data);
        var expandedKeys_ = union(toExpandedKeys, disabledKeys, this.state.selectedKeys);
        this.setState({
            dataSource: treeSource_data,
            expandedKeys: expandedKeys_,
            selectedKeys: selectedKeys_
        });
    };
    IotTree.prototype.getDisabledKeys = function (nodes) {
        var disabledKeys = [];
        for (var i = 0; i < nodes.length; i++) {
            var node = nodes[i];
            this.iterNode(node, function (node) {
                if (node.disabled) {
                    disabledKeys.push(node.id);
                }
            });
            return disabledKeys;
        }
        return disabledKeys;
    };
    IotTree.prototype.getRootId = function (nodes) {
        if (Array.isArray(nodes) && nodes.length > 0) {
            return nodes[0].id || nodes[0].key;
        }
        else if (nodes) {
            return nodes.id || nodes.key;
        }
        else {
            return undefined;
        }
    };
    IotTree.prototype.iterNode = function (node, callback) {
        var _this = this;
        if (node.children && node.children.length > 0) {
            node.children.forEach(function (child) {
                _this.iterNode(child, callback);
            });
        }
        callback(node);
    };
    IotTree.prototype.renderSearch = function () {
        if (!this.props.showSearch) {
            return null;
        }
        return (React.createElement("div", { className: "lp-tree-search" },
            React.createElement(Input, __assign({ hasClear: true, placeholder: "\u8BF7\u8F93\u5165\u5173\u952E\u5B57", className: "lp-tree-input", value: this.state.searchValue, onChange: this.onSearch, disabled: this.props.disabled }, this.props.searchProps)),
            !this.state.searchValue && (React.createElement(Icon, { className: "lp-tree-icon", type: "search", size: "xs" }))));
    };
    IotTree.prototype.renderTree = function () {
        var _this = this;
        var renderNodes = function (data, level, disabled) {
            var treeNodeDate = data.map(function (item) {
                var children = item.children, label = item.label, visiabled = item.visiabled, key = item.key, id = item.id, labelCell = item.labelCell, rest = __rest(item, ["children", "label", "visiabled", "key", "id", "labelCell"]);
                var disabled_ = disabled ? disabled : item.disabled;
                return visiabled === false ? null : (React.createElement(Tree.Node, __assign({ label: _this.props.editable ? item.label : _this.renderNode(item, level, disabled_) }, rest, { key: key || id || Date.now(), disabled: disabled_ }), item.children &&
                    renderNodes(item.children, level + 1, disabled)));
            });
            return treeNodeDate;
        };
        var _a = this.state, searchValue = _a.searchValue, expandedKeys = _a.expandedKeys, dataSource = _a.dataSource, matchedKeys = _a.matchedKeys;
        //有些属性提取出来没用，是因为不行在...rest中透传到dom节点上导致warn警告
        var _b = this.props, className = _b.className, searchEmpty = _b.searchEmpty, disabled = _b.disabled, onExpand = _b.onExpand, onSelect = _b.onSelect, selectedNodeId = _b.selectedNodeId, showSearch = _b.showSearch, extraExpandedKeys = _b.extraExpandedKeys, treeSource = _b.treeSource, menuSource = _b.menuSource, onClickMenuItem = _b.onClickMenuItem, children = _b.children, rest = __rest(_b, ["className", "searchEmpty", "disabled", "onExpand", "onSelect", "selectedNodeId", "showSearch", "extraExpandedKeys", "treeSource", "menuSource", "onClickMenuItem", "children"]);
        var filterTreeNode = function (node) {
            return searchValue !== undefined && searchValue.trim() !== "" && matchedKeys.indexOf(node.props.eventKey) > -1;
        };
        if (!searchValue || matchedKeys.length > 0) {
            return (React.createElement(Tree, __assign({ className: classnames("lp-tree", className), expandedKeys: expandedKeys, autoExpandParent: true, filterTreeNode: filterTreeNode, onExpand: this.onExpand, onSelect: this.onSelect, selectedKeys: this.state.selectedKeys }, rest), children || renderNodes(dataSource || [], 0, disabled)));
        }
        return React.createElement("p", { className: "lp-tree-empty-list" }, searchEmpty);
    };
    IotTree.prototype.renderNode = function (node, level, disabled) {
        if (node == null || !node.label) {
            return null;
        }
        var _a = this.props, menuSource = _a.menuSource, onClickMenuItem = _a.onClickMenuItem;
        return (React.createElement(TreeNode, { node: node, level: level, menu: menuSource, onClickMenuItem: onClickMenuItem, disabled: disabled ? disabled : node.disabled, checkable: this.props.checkable }));
    };
    IotTree.prototype.render = function () {
        return (React.createElement("div", { className: "lp-tree" },
            this.renderSearch(),
            this.renderTree()));
    };
    return IotTree;
}(Component));
export default IotTree;
IotTree.defaultProps = {
    showSearch: false,
    checkable: false,
    showLine: false,
    searchEmpty: "未搜索到相关结果",
};
IotTree.Node = Tree.Node;
