"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 __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 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.nameField = 'name';
        _this.valueField = 'value';
        _this.state = {
            data: [],
            selectedKeys: []
        };
        _this.delNode = function (key) {
            console.log('dynamic tree not support');
        };
        _this.updateNode = function (item) {
            console.log('dynamic tree not support');
        };
        _this.addNode = function (item) {
            console.log('dynamic tree not support');
        };
        _this.getCurrent = function () {
            return {};
        };
        _this.filter = function (key) {
            console.log('filter not support in dynamic tree!');
        };
        return _this;
    }
    default_1.prototype.renderNode = function (item) {
        if (item.children) {
            //@ts-ignore
            return React.createElement(antd_1.Tree.TreeNode, { title: item[this.nameField], key: item[this.valueField], dataRef: item }, this.renderParent(item.children));
        }
        //@ts-ignore
        return React.createElement(antd_1.Tree.TreeNode, { title: item[this.nameField], key: item[this.valueField], dataRef: item });
    };
    default_1.prototype.renderParent = function (items) {
        var _this = this;
        return items.map(function (item) { return _this.renderNode(item); });
    };
    default_1.prototype.componentDidMount = function () {
        this.nameField = this.props.nameField || 'name';
        this.valueField = this.props.valueField || 'value';
        this.setState({ data: this.props.data });
    };
    default_1.prototype.shouldComponentUpdate = function () {
        return !this.loaded;
    };
    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.onLoadData = function (treeNode) {
        var _this = this;
        if (_.isArray(treeNode.props.children) && treeNode.props.children.length > 0) {
            return new Promise(function (resolve) { return resolve(); });
        }
        return this.props.loadData(treeNode.props.eventKey).then(function (resp) {
            var list = resp.data || resp.list || [];
            var children = [];
            list.map(function (item) {
                var _a;
                children.push((_a = {},
                    _a[_this.nameField] = item[_this.nameField],
                    _a[_this.valueField] = item[_this.valueField] + '',
                    _a));
            });
            treeNode.props.dataRef.children = children;
            _this.loaded = false;
            _this.setState({
                data: __spread(_this.state.data),
            });
        });
    };
    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] + '']); }, 50);
            }
        }
        return React.createElement(antd_1.Tree, { showLine: true, onSelect: function (selectedKeys) { return _this.selectNode(selectedKeys); }, selectedKeys: this.state.selectedKeys, loadData: function (treeNode) { return _this.onLoadData(treeNode); } }, this.renderParent(this.state.data));
    };
    return default_1;
}(_TreeContents_1.default));
exports.default = default_1;
