"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseResultTreeList = void 0;
var tslib_1 = require("tslib");
/**
 * 结果树(暂时不支持结果排序)
 */
var react_1 = (0, tslib_1.__importDefault)(require("react"));
var lodash_1 = require("lodash");
var Select_1 = require("./Select");
var theme_1 = require("../theme");
var helper_1 = require("../utils/helper");
var locale_1 = require("../locale");
var Tree_1 = (0, tslib_1.__importDefault)(require("./Tree"));
var TransferSearch_1 = (0, tslib_1.__importDefault)(require("./TransferSearch"));
// 递归找到对应选中节点(dfs)
function getDeep(node, cb, pathNodes, valueField) {
    if (node[valueField] && cb(node)) {
        node.isChecked = true;
        for (var i = pathNodes.length - 2; i >= 0; i--) {
            if (!pathNodes[i].isChecked) {
                pathNodes[i].isChecked = true;
                continue;
            }
            break;
        }
    }
    else if (node.children && Array.isArray(node.children)) {
        node.children.forEach(function (n) {
            pathNodes.push(n);
            getDeep(n, cb, pathNodes, valueField);
            pathNodes.pop();
        });
    }
}
// 递归删除树多余的节点
function deepCheckedTreeNode(nodes) {
    var arr = [];
    for (var i = 0; i < nodes.length; i++) {
        var node = nodes[i];
        if (node.isChecked) {
            if (node.children && Array.isArray(node.children)) {
                node.children = deepCheckedTreeNode(node.children);
            }
            arr.push(node);
        }
    }
    return arr;
}
// 根据选项获取到结果
function getResultOptions(value, options, valueField) {
    if (value === void 0) { value = []; }
    var newOptions = (0, lodash_1.cloneDeep)(options);
    var callBack = function (node) {
        return !!(value || []).find(function (target) { return target[valueField] === node[valueField]; });
    };
    newOptions &&
        newOptions.forEach(function (op) {
            getDeep(op, callBack, [op], valueField);
        });
    return deepCheckedTreeNode(newOptions);
}
// 在包含回调函数情况下，遍历树
function deepTree(nodes, cb) {
    for (var i = 0; i < nodes.length; i++) {
        var node = nodes[i];
        cb(node);
        if (node.children && Array.isArray(node.children)) {
            deepTree(node.children, cb);
        }
    }
}
// 树的节点删除
function deepDeleteTree(nodes, option, valueField) {
    var arr = [];
    for (var i = 0; i < nodes.length; i++) {
        var node = nodes[i];
        if ((0, lodash_1.isEqual)(node, option)) {
            continue;
        }
        if (node.children && Array.isArray(node.children)) {
            node.children = deepDeleteTree(node.children, option, valueField);
        }
        if ((node.children && node.children.length > 0) ||
            node[valueField] !== undefined) {
            arr.push(node);
        }
    }
    return arr;
}
var BaseResultTreeList = /** @class */ (function (_super) {
    (0, tslib_1.__extends)(BaseResultTreeList, _super);
    function BaseResultTreeList() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            searching: false,
            treeOptions: [],
            searchTreeOptions: []
        };
        return _this;
    }
    BaseResultTreeList.itemRender = function (option) {
        return react_1.default.createElement("span", null, "".concat(option.scopeLabel || '').concat(option.label));
    };
    BaseResultTreeList.getDerivedStateFromProps = function (props) {
        var newOptions = getResultOptions(props.value, props.options, props.valueField);
        return {
            treeOptions: (0, lodash_1.cloneDeep)(newOptions)
        };
    };
    // 删除非选中节点
    BaseResultTreeList.prototype.deleteTreeChecked = function (option) {
        var _a = this.props, _b = _a.value, value = _b === void 0 ? [] : _b, onChange = _a.onChange, valueField = _a.valueField;
        var _c = this.state, searching = _c.searching, treeOptions = _c.treeOptions;
        var temNode = [];
        var cb = function (node) {
            if ((0, lodash_1.isEqual)(node, option)) {
                temNode = [node];
            }
        };
        deepTree(treeOptions || [], cb);
        var arr = [];
        var cb2 = function (node) {
            if (node.isChecked && node[valueField]) {
                arr.push(node);
            }
        };
        deepTree(temNode, cb2);
        onChange &&
            onChange(value.filter(function (item) {
                return !arr.find(function (arrItem) {
                    return (0, lodash_1.isEqual)((0, lodash_1.omit)(arrItem, ['isChecked', 'childrens']), item);
                });
            }));
        // 搜索时，重新生成树
        searching && this.deleteResultTreeNode(option);
    };
    // 搜索树点击删除时，删除对应节点
    BaseResultTreeList.prototype.deleteResultTreeNode = function (option) {
        var arr = deepDeleteTree((0, lodash_1.cloneDeep)(this.state.searchTreeOptions) || [], option, this.props.valueField);
        this.setState({ searchTreeOptions: arr });
    };
    BaseResultTreeList.prototype.search = function (inputValue) {
        // 结果为空，直接清空
        if (!inputValue) {
            this.clearSearch();
            return;
        }
        var _a = this.props, valueField = _a.valueField, onSearch = _a.onSearch;
        var temOptions = this.state.treeOptions || [];
        var cb = function (node) {
            node.isChecked = false;
            return true;
        };
        deepTree(temOptions, cb);
        var callBack = function (node) { return onSearch === null || onSearch === void 0 ? void 0 : onSearch(inputValue, node); };
        temOptions &&
            temOptions.forEach(function (op) {
                getDeep(op, callBack, [op], valueField);
            });
        this.setState({
            searching: true,
            searchTreeOptions: deepCheckedTreeNode(temOptions)
        });
    };
    BaseResultTreeList.prototype.clearSearch = function () {
        this.setState({
            searching: false,
            searchTreeOptions: []
        });
    };
    BaseResultTreeList.prototype.renderTree = function () {
        var _this = this;
        var _a = this.props, className = _a.className, cx = _a.classnames, value = _a.value, placeholder = _a.placeholder, valueField = _a.valueField, itemRender = _a.itemRender, __ = _a.translate;
        var _b = this.state, treeOptions = _b.treeOptions, searching = _b.searching, searchTreeOptions = _b.searchTreeOptions;
        return (react_1.default.createElement("div", { className: cx('ResultTreeList', className) }, Array.isArray(value) && value.length ?
            (react_1.default.createElement(Tree_1.default, { className: cx('Transfer-tree'), options: !searching ? treeOptions : searchTreeOptions, valueField: valueField, value: [], onChange: helper_1.noop, showIcon: false, itemRender: itemRender, removable: true, onDelete: function (option) { return _this.deleteTreeChecked(option); } })) :
            (react_1.default.createElement("div", { className: cx('Selections-placeholder') }, __(placeholder)))));
    };
    BaseResultTreeList.prototype.render = function () {
        var _a = this.props, cx = _a.classnames, className = _a.className, title = _a.title, searchable = _a.searchable, __ = _a.translate, _b = _a.placeholder, placeholder = _b === void 0 ? __('Transfer.searchKeyword') : _b;
        return (react_1.default.createElement("div", { className: cx('Selections', className) },
            title ? react_1.default.createElement("div", { className: cx('Selections-title') }, title) : null,
            searchable ? (react_1.default.createElement(TransferSearch_1.default, { placeholder: placeholder, onSearch: this.search, onCancelSearch: this.clearSearch })) : null,
            this.renderTree()));
    };
    var _a;
    BaseResultTreeList.defaultProps = {
        placeholder: 'placeholder.selectData',
        itemRender: BaseResultTreeList.itemRender
    };
    (0, tslib_1.__decorate)([
        helper_1.autobind,
        (0, tslib_1.__metadata)("design:type", Function),
        (0, tslib_1.__metadata)("design:paramtypes", [typeof (_a = typeof Select_1.Option !== "undefined" && Select_1.Option) === "function" ? _a : Object]),
        (0, tslib_1.__metadata)("design:returntype", void 0)
    ], BaseResultTreeList.prototype, "deleteTreeChecked", null);
    (0, tslib_1.__decorate)([
        helper_1.autobind,
        (0, tslib_1.__metadata)("design:type", Function),
        (0, tslib_1.__metadata)("design:paramtypes", [String]),
        (0, tslib_1.__metadata)("design:returntype", void 0)
    ], BaseResultTreeList.prototype, "search", null);
    (0, tslib_1.__decorate)([
        helper_1.autobind,
        (0, tslib_1.__metadata)("design:type", Function),
        (0, tslib_1.__metadata)("design:paramtypes", []),
        (0, tslib_1.__metadata)("design:returntype", void 0)
    ], BaseResultTreeList.prototype, "clearSearch", null);
    return BaseResultTreeList;
}(react_1.default.Component));
exports.BaseResultTreeList = BaseResultTreeList;
exports.default = (0, theme_1.themeable)((0, locale_1.localeable)(BaseResultTreeList));
//# sourceMappingURL=./components/ResultTreeList.js.map
