"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __values = (this && this.__values) || function (o) {
    var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
    if (m) return m.call(o);
    return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spread = (this && this.__spread) || function () {
    for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var _ = require("lodash");
var antd_1 = require("antd");
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1(props) {
        var _this = _super.call(this, props) || this;
        _this.nameField = 'name';
        _this.valueField = 'value';
        _this.iconField = ''; //如果没有ICON，就不要传
        _this.mustSelect = false;
        _this.Searched = new Set();
        _this.state = {
            selectedKeys: [],
            expandedKeys: [],
        };
        if (_.has(_this.props, 'nameField')) {
            _this.nameField = _this.props.nameField;
        }
        if (_.has(_this.props, 'valueField')) {
            _this.valueField = _this.props.valueField;
        }
        if (_.has(_this.props, 'iconField')) {
            _this.iconField = _this.props.iconField;
        }
        if (_.has(_this.props, 'mustSelect')) {
            _this.mustSelect = _this.props.mustSelect;
        }
        return _this;
    }
    default_1.prototype.componentDidMount = function () {
        var _this = this;
        var state = {
            expandedKeys: this.props.treeData.map(function (item) { return item[_this.valueField] + ''; }),
            selectedKeys: _.concat([], this.state.selectedKeys),
        };
        if (state.selectedKeys.length == 0) {
            if (this.props.defaultSelect) {
                state.selectedKeys = [this.props.defaultSelect];
            }
            else if (this.props.mustSelect) {
                state.selectedKeys = [this.props.treeData[0][this.valueField] + ''];
            }
        }
        this.setState(state);
    };
    default_1.prototype.findChildren = function (key, children) {
        var e_1, _a;
        var parents = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            parents[_i - 2] = arguments[_i];
        }
        try {
            for (var children_1 = __values(children), children_1_1 = children_1.next(); !children_1_1.done; children_1_1 = children_1.next()) {
                var sub = children_1_1.value;
                this.findSub.apply(this, __spread([key, sub], parents));
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (children_1_1 && !children_1_1.done && (_a = children_1.return)) _a.call(children_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    };
    default_1.prototype.findSub = function (key, sub) {
        var e_2, _a;
        var parents = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            parents[_i - 2] = arguments[_i];
        }
        if (_.isArray(sub.children) && sub.children.length > 0) {
            this.findChildren.apply(this, __spread([key, sub.children], __spread([sub], parents)));
        }
        if (sub[this.nameField].toUpperCase().indexOf(key.toUpperCase()) >= 0) {
            // console.log(sub);
            this.Searched.add(sub[this.valueField] + '');
            try {
                for (var parents_1 = __values(parents), parents_1_1 = parents_1.next(); !parents_1_1.done; parents_1_1 = parents_1.next()) {
                    var root = parents_1_1.value;
                    this.Searched.add(root[this.valueField] + '');
                }
            }
            catch (e_2_1) { e_2 = { error: e_2_1 }; }
            finally {
                try {
                    if (parents_1_1 && !parents_1_1.done && (_a = parents_1.return)) _a.call(parents_1);
                }
                finally { if (e_2) throw e_2.error; }
            }
        }
    };
    default_1.prototype.buildNode = function (param, item) {
        if (this.iconField != '' && item[this.iconField] != '') {
            if (item[this.iconField] && item[this.iconField].indexOf('/') >= 0) {
                param.icon = React.createElement("span", { style: { lineHeight: '24px' } },
                    React.createElement("img", { style: { marginTop: -4 }, src: item[this.iconField] }));
            }
            else {
                param.icon = React.createElement(antd_1.Icon, { type: item[this.iconField] });
            }
        }
        if (_.isArray(item.children) && item.children.length > 0) {
            return React.createElement(antd_1.Tree.TreeNode, __assign({}, param, { dataRef: item }), this.renderTree(item.children));
        }
        return React.createElement(antd_1.Tree.TreeNode, __assign({}, param, { dataRef: item }));
    };
    default_1.prototype.renderNode = function (item) {
        var key = this.props.searchValue;
        var param = {
            title: item[this.nameField],
            key: item[this.valueField] + '',
        };
        if (_.isEmpty(key)) {
            return this.buildNode(param, item);
        }
        var index = param.title.toUpperCase().indexOf(key.toUpperCase());
        var beforeStr = param.title.substr(0, index);
        var middleStr = param.title.substr(index, key.length);
        var afterStr = param.title.substr(index + key.length);
        param.title =
            index > -1 ? React.createElement("span", null,
                beforeStr,
                React.createElement("span", { className: "high" }, middleStr),
                afterStr)
                : param.title;
        return this.buildNode(param, item);
    };
    default_1.prototype.renderTree = function (data) {
        var _this = this;
        return data.map(function (item) {
            if (_this.props.searchValue) {
                if (_this.Searched.has(item[_this.valueField] + '')) {
                    return _this.renderNode(item);
                }
                return React.createElement(React.Fragment, null);
            }
            else {
                return _this.renderNode(item);
            }
        });
    };
    default_1.prototype.selectNode = function (selectedKeys, evt) {
        if (evt === void 0) { evt = null; }
        if (this.props.mustSelect && selectedKeys.length == 0) {
            return;
        }
        this.setState({ selectedKeys: selectedKeys });
        if (selectedKeys.length > 0) {
            this.props.onSelect(selectedKeys[0], evt.node.props.dataRef);
        }
        else {
            this.props.onSelect(null, null);
        }
    };
    default_1.prototype.render = function () {
        var _this = this;
        if (this.props.searchValue) {
            this.Searched.clear();
            this.findChildren(this.props.searchValue, this.props.treeData);
        }
        var param = {};
        if (this.props.iconField) {
            param.showIcon = true;
        }
        if (this.props.searchValue) {
            param.expandedKeys = Array.from(this.Searched);
        }
        else {
            param.expandedKeys = this.state.expandedKeys;
            if (param.expandedKeys.length == 0 && this.props.treeData.length == 1) {
                param.expandedKeys.push(this.props.treeData[0][this.valueField]);
            }
        }
        return React.createElement(antd_1.Tree
        // showLine
        , __assign({ 
            // showLine
            onSelect: function (selectedKeys, evt) { return _this.selectNode(selectedKeys, evt); }, selectedKeys: this.state.selectedKeys, onExpand: function (expandedKeys) {
                if (!_this.props.searchValue) {
                    _this.setState({ expandedKeys: (expandedKeys.length == 0) ? [undefined] : expandedKeys });
                }
            } }, param), this.renderTree(this.props.treeData));
    };
    return default_1;
}(React.PureComponent));
exports.default = default_1;
