import { __assign, __rest } from "tslib";
var TreeInstance = (function () {
    function TreeInstance(data, defaultCheckedKeys, defaultExpandedKeys, defaultExpandAll) {
        this.treeNodes = this.handleTreeNode(data, defaultCheckedKeys, defaultExpandedKeys, defaultExpandAll, '');
    }
    TreeInstance.prototype.handleTreeNode = function (data, defaultCheckedKeys, defaultExpandedKeys, defaultExpandAll, parentKey) {
        var _this = this;
        return data.map(function (item, idx) {
            var key = item.key, title = item.title, children = item.children, disableCheckbox = item.disableCheckbox, disabled = item.disabled, otherProps = __rest(item, ["key", "title", "children", "disableCheckbox", "disabled"]);
            var uniqueKey = parentKey ? parentKey + ("-" + idx) : "" + idx;
            var node = __assign(__assign({}, otherProps), { key: key,
                uniqueKey: uniqueKey, title: title || '---', disabled: disabled || false, disableCheckbox: disableCheckbox || false, checked: key ? defaultCheckedKeys.includes(key) : false, indeterminate: false, expanded: defaultExpandAll || (key ? defaultExpandedKeys.includes(key) : false), parentKey: parentKey });
            if (children) {
                node.children = _this.handleTreeNode(children, defaultCheckedKeys, defaultExpandedKeys, defaultExpandAll, uniqueKey);
                var indeterminate = _this.isIndeterminate(node);
                return __assign(__assign({}, node), { indeterminate: indeterminate });
            }
            return node;
        });
    };
    Object.defineProperty(TreeInstance.prototype, "nodes", {
        get: function () {
            return this.treeNodes;
        },
        enumerable: false,
        configurable: true
    });
    TreeInstance.prototype.getNodeByUniqueKey = function (uniqueKey) {
        var keys = uniqueKey.split('-').map(function (item) { return parseInt(item); });
        var curr = this.treeNodes;
        var target = undefined;
        for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
            var key = keys_1[_i];
            if (curr) {
                target = curr[key];
                curr = target === null || target === void 0 ? void 0 : target.children;
            }
        }
        return target;
    };
    TreeInstance.prototype.isIndeterminate = function (node) {
        if (node.children) {
            if (node.children.some(function (n) { return n.indeterminate; })) {
                return true;
            }
            var numOfChecked = node.children.filter(function (n) { return n.checked; }).length;
            return numOfChecked > 0 && numOfChecked < node.children.length;
        }
        return false;
    };
    TreeInstance.prototype.checkChildren = function (children, isChecked) {
        var _this = this;
        children.forEach(function (child) {
            if (child.disabled) {
                return;
            }
            child.checked = isChecked;
            child.indeterminate = _this.isIndeterminate(child);
            if (child.children) {
                _this.checkChildren(child.children, isChecked);
            }
        });
    };
    TreeInstance.prototype.checkParent = function (node) {
        if (node.disabled) {
            return;
        }
        var children = node.children;
        var numOfChecked = children.filter(function (n) { return n.checked; }).length;
        node.checked = numOfChecked === children.length;
        node.indeterminate = this.isIndeterminate(node);
        var parentNode = this.getNodeByUniqueKey(node.parentKey);
        if (parentNode) {
            this.checkParent(parentNode);
        }
    };
    TreeInstance.prototype.setNodeChecked = function (uniqueKey, isChecked) {
        var node = this.getNodeByUniqueKey(uniqueKey);
        if (node) {
            node.checked = isChecked;
            if (node.children) {
                this.checkChildren(node.children, isChecked);
            }
            node.indeterminate = this.isIndeterminate(node);
            var parentNode = this.getNodeByUniqueKey(node.parentKey);
            if (parentNode) {
                this.checkParent(parentNode);
            }
        }
    };
    TreeInstance.prototype.setNodeExpanded = function (uniqueKey, isExpanded) {
        var node = this.getNodeByUniqueKey(uniqueKey);
        if (node) {
            node.expanded = isExpanded;
        }
    };
    TreeInstance.prototype.isPropValueTrue = function (node, prop, list) {
        var _this = this;
        var key = node.key, uniqueKey = node.uniqueKey, children = node.children;
        if (node[prop]) {
            list.push(key ? key : uniqueKey);
        }
        if (children) {
            children.forEach(function (child) {
                _this.isPropValueTrue(child, prop, list);
            });
        }
    };
    TreeInstance.prototype.getCheckedKeys = function () {
        var _this = this;
        var keys = [];
        this.nodes.forEach(function (node) { return _this.isPropValueTrue(node, 'checked', keys); });
        return keys;
    };
    TreeInstance.prototype.getExpandedKeys = function () {
        var _this = this;
        var keys = [];
        this.nodes.forEach(function (node) { return _this.isPropValueTrue(node, 'expanded', keys); });
        return keys;
    };
    return TreeInstance;
}());
export { TreeInstance };
//# sourceMappingURL=tree-instance.js.map