import { __assign, __extends, __rest } from "tslib";
import { Transfer } from '@alicloud/console-components';
import Tree from '../tree';
import React from 'react';
var LpTransfer = /** @class */ (function (_super) {
    __extends(LpTransfer, _super);
    function LpTransfer(props) {
        var _this = _super.call(this, props) || this;
        //递归遍历
        _this.getallNode = function (treeData, callBack) {
            var flatten = function (list, level, path) {
                if (list === void 0) { list = []; }
                if (path === void 0) { path = []; }
                list.forEach(function (item) {
                    path.splice(level);
                    path[level] = item.label;
                    if (item.children === undefined || item.children.length === 0) {
                        callBack(item, path.slice(0));
                    }
                    else {
                        flatten(item.children, level + 1, path);
                    }
                });
            };
            flatten(treeData, 0, []);
        };
        //移动全部
        _this.handleChange = function () {
            var rest = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                rest[_i] = arguments[_i];
            }
            var extra = rest[2];
            if (extra.direction === "right") {
                var selected_1 = [];
                var checkedKeys_1 = [];
                if (_this.props.treeSource) {
                    var orignData_ = _this.props.treeSource;
                    _this.getallNode(orignData_, function (item, path) {
                        if ((!item.children || item.children.length === 0)) {
                            if (_this.props.checkable) {
                                selected_1.push(__assign(__assign({}, item), { labelCell: React.createElement("div", { className: "lp-tree-title" },
                                        React.createElement("p", null, item.label),
                                        React.createElement("p", { className: "lp-tree-path", title: path.join(" / ") }, path.join(" / "))) }));
                                checkedKeys_1.push(item.id);
                            }
                            else {
                                item.disabled = true;
                                selected_1.push(__assign(__assign({}, item), { disabled: false, labelCell: React.createElement("div", { className: "lp-tree-title" },
                                        React.createElement("p", null, item.label),
                                        React.createElement("p", { className: "lp-tree-path", title: path.join(" / ") }, path.join(" / "))) }));
                            }
                        }
                    });
                    _this.setState({
                        orignData: orignData_,
                        selectData: selected_1,
                        checkedKeys: checkedKeys_1
                    });
                }
            }
            else {
                var selected_ = _this.state.selectData.slice(0).map(function (i) { return i.id; });
                _this.onCancel(selected_);
            }
        };
        _this.onSelect = function (selectedKeys, extra) {
            if (extra.selectedNodes[0].props.children === undefined || extra.selectedNodes[0].props.children.length === 0) {
                var selected_2 = _this.state.selectData.slice(0);
                var orignData_ = _this.state.orignData.slice(0);
                _this.getallNode(orignData_, function (item, path) {
                    if (selectedKeys.indexOf(item.id) !== -1) {
                        item.disabled = true;
                        selected_2.push(__assign(__assign({}, item), { disabled: false, labelCell: React.createElement("div", { className: "lp-tree-title" },
                                React.createElement("p", null, item.label),
                                React.createElement("p", { className: "lp-tree-path", title: path.join(" / ") }, path.join(" / "))) }));
                    }
                });
                _this.setState({
                    orignData: orignData_,
                    selectData: selected_2
                });
            }
        };
        _this.onCheck = function (checkedKeys) {
            var orignData_ = _this.state.orignData.slice(0);
            var selected_ = [];
            var checkedKeys_ = [];
            _this.getallNode(orignData_, function (item, path) {
                if (checkedKeys.indexOf(item.id) !== -1 && (!item.children || item.children.length === 0)) {
                    selected_.push(__assign(__assign({}, item), { labelCell: React.createElement("div", { className: "lp-tree-title" },
                            React.createElement("p", null, item.label),
                            React.createElement("p", { className: "lp-tree-path", title: path.join(" / ") }, path.join(" / "))) }));
                    checkedKeys_.push(item.id);
                }
            });
            _this.setState({
                orignData: orignData_,
                selectData: selected_,
                checkedKeys: checkedKeys_
            });
        };
        _this.onCancel = function (selectedKeys) {
            var orignData_ = _this.state.orignData.slice(0);
            var selected_ = _this.state.selectData.slice(0);
            var checkedKeys_ = _this.state.checkedKeys.slice(0);
            _this.getallNode(orignData_, function (item) {
                var id = item.id;
                if (selectedKeys.indexOf(id) !== -1) {
                    // item.visiabled = true;
                    item.disabled = false;
                    selected_ = selected_.filter(function (i) { return !(i.id === id); });
                    checkedKeys_ = checkedKeys_.filter(function (i) { return !(i === id); });
                }
            });
            _this.setState({
                orignData: orignData_,
                selectData: selected_,
                checkedKeys: checkedKeys_
            });
        };
        _this.state = {
            orignData: props.treeSource ? props.treeSource : [],
            selectData: [],
            checkedKeys: []
        };
        return _this;
    }
    LpTransfer.prototype.render = function () {
        var _this = this;
        var _a = this.props, treeSource = _a.treeSource, checkable = _a.checkable, width = _a.width, height = _a.height, rest = __rest(_a, ["treeSource", "checkable", "width", "height"]);
        if (!treeSource) {
            return React.createElement(Transfer, __assign({}, rest));
        }
        else {
            var _b = this.state, orignData_1 = _b.orignData, selectData_1 = _b.selectData, checkedKeys_2 = _b.checkedKeys;
            return (React.createElement(Transfer, __assign({ mode: "simple", onChange: this.handleChange, titles: ['全部', '选中'], className: "lp-transfer-box", listStyle: {
                    width: width,
                    height: height
                } }, rest), function (_a) {
                var position = _a.position;
                if (position === 'left') {
                    return (React.createElement(Tree, { isNodeBlock: true, style: { padding: '0 16px' }, showSearch: true, checkedKeys: checkable ? checkedKeys_2 : undefined, checkable: checkable, treeSource: orignData_1, 
                        //点击元素
                        onSelect: checkable ? undefined : _this.onSelect, 
                        //点击复选框
                        onCheck: checkable ? _this.onCheck : undefined }));
                }
                else {
                    return (React.createElement(Tree, { style: { padding: '0 12px' }, showSearch: true, treeSource: selectData_1, 
                        //点击元素
                        onSelect: _this.onCancel, className: "lp-tree-right" }));
                }
            }));
        }
    };
    return LpTransfer;
}(React.Component));
export default LpTransfer;
LpTransfer.defaultProps = {
    width: 276,
    height: 316
};
