"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * 左树右表中的左树
*/
var React = require("react");
var antd_1 = require("antd");
var _TreeContents_1 = require("./_TreeContents");
var _ = require("lodash");
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.loaded = false;
        _this.filterKey = '';
        _this.nameField = 'name';
        _this.valueField = 'value';
        _this.iconField = ''; //如果没有ICON，就不要传
        _this.allParents = [];
        _this.state = {
            selectedKeys: [],
            expandedKeys: [],
            filtered: false,
        };
        _this.getCurrent = function () {
            return {};
        };
        _this.delNode = function (key) {
            console.log('static tree not support');
        };
        _this.updateNode = function (item) {
            console.log('static tree not support');
        };
        _this.addNode = function (item) {
            console.log('static tree not support');
        };
        _this.filter = function (key) {
            _this.filterKey = key;
            _this.loaded = false;
            if (key) {
                _this.setState({ filtered: true });
            }
            else {
                _this.setState({ filtered: false });
            }
        };
        return _this;
    }
    default_1.prototype.checkChild = function (item, key) {
        if (_.isArray(item.children) && item.children.length > 0) {
            for (var i = 0; i < item.children.length; i++) {
                if (item.children[i][this.nameField].toLowerCase().indexOf(key.toLowerCase()) >= 0) {
                    return true;
                }
                else {
                    if (this.checkChild(item.children[i], key)) {
                        return true;
                    }
                }
            }
        }
        return false;
    };
    default_1.prototype.renderNode = function (item) {
        var param = {
            title: item[this.nameField],
            key: item[this.valueField],
        };
        if (this.filterKey) {
            var title = [];
            var ptns = item[this.nameField].split(new RegExp(this.filterKey, 'i'));
            var size = ptns.length;
            var start = 0;
            if (size >= 2) {
                for (var i = 0; i < size; i++) {
                    start += ptns[i].length;
                    title.push(ptns[i]);
                    if (i < (size - 1)) {
                        title.push(React.createElement("span", { style: { color: 'red' } }, item[this.nameField].substr(start, this.filterKey.length)));
                    }
                }
            }
            else {
                if (this.checkChild(item, this.filterKey)) {
                    title.push(item[this.nameField]);
                }
                else {
                    return [];
                }
            }
            param.title = title;
        }
        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), this.renderParent(item.children));
        }
        return React.createElement(antd_1.Tree.TreeNode, __assign({}, param));
    };
    default_1.prototype.renderParent = function (items) {
        var _this = this;
        return items.map(function (item) { return _this.renderNode(item); });
    };
    default_1.prototype.buildAllFolders = function (node) {
        var _this = this;
        node.map(function (item) {
            if (_.isArray(item.children) && item.children.length > 0) {
                _this.allParents.push(item[_this.valueField]);
                _this.buildAllFolders(item.children);
            }
        });
    };
    default_1.prototype.componentWillMount = function () {
        this.nameField = this.props.nameField || 'name';
        this.valueField = this.props.valueField || 'value';
        this.iconField = this.props.iconField || '';
        this.buildAllFolders(this.props.data);
    };
    default_1.prototype.shouldComponentUpdate = function (old) {
        return !this.loaded || old.data != this.props.data;
    };
    default_1.prototype.selectNode = function (selectedKeys) {
        if (this.props.mustSelect && selectedKeys.length == 0) {
            return;
        }
        this.loaded = false;
        this.setState({ selectedKeys: selectedKeys });
        this.props.onSelect(selectedKeys[0]);
    };
    default_1.prototype.render = function () {
        var _this = this;
        this.loaded = true;
        var selectedKeys = _.concat([], this.state.selectedKeys);
        if (selectedKeys.length == 0) {
            if (this.props.defaultSelect) {
                this.selectNode([this.props.defaultSelect + '']);
            }
            else if (this.props.mustSelect) {
                window.setTimeout(function () { return _this.selectNode([_this.props.data[0][_this.valueField] + '']); }, 300);
            }
        }
        var param = {};
        if (this.props.iconField) {
            param.showIcon = true;
        }
        if (this.props.defaultExpandAll) {
            param.defaultExpandAll = true;
        }
        if (this.state.filtered) {
            param.expandedKeys = this.allParents;
        }
        else {
            param.expandedKeys = this.state.expandedKeys;
            if (param.expandedKeys.length == 0 && this.props.data.length == 1) {
                param.expandedKeys.push(this.props.data[0][this.valueField]);
            }
        }
        return React.createElement(antd_1.Tree, __assign({ showLine: true, onSelect: function (selectedKeys) { return _this.selectNode(selectedKeys); }, selectedKeys: this.state.selectedKeys, onExpand: function (expandedKeys) {
                if (!_this.state.filtered) {
                    _this.loaded = false;
                    _this.setState({ expandedKeys: (expandedKeys.length == 0) ? [undefined] : expandedKeys });
                }
            } }, param), this.renderParent(this.props.data));
    };
    return default_1;
}(_TreeContents_1.default));
exports.default = default_1;
