import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _to_property_key } from "@swc/helpers/_/_to_property_key";
export var formatTree = function(tree, parent, config) {
    return tree.map(function(node) {
        var tmp = config.value, valueKey = tmp === void 0 ? 'value' : tmp, tmp1 = config.text, textKey = tmp1 === void 0 ? 'text' : tmp1, tmp2 = config.children, childrenKey = tmp2 === void 0 ? 'children' : tmp2;
        var value = node[valueKey], text = node[textKey], children = node[childrenKey], others = _object_without_properties(node, [
            valueKey,
            textKey,
            childrenKey
        ].map(_to_property_key));
        var newNode = _object_spread_props(_object_spread({
            loading: false
        }, others), {
            level: parent ? (parent && parent.level || 0) + 1 : 0,
            value: value,
            text: text,
            children: children,
            _parent: parent
        });
        if (newNode.children && newNode.children.length) {
            newNode.children = formatTree(newNode.children, newNode, config);
        }
        return newNode;
    });
};
export var eachTree = function(tree, cb) {
    var i = 0;
    var node;
    while(node = tree[i++]){
        if (cb(node) === true) {
            break;
        }
        if (node.children && node.children.length) {
            eachTree(node.children, cb);
        }
    }
};
var defaultConvertConfig = {
    topId: null,
    idKey: 'id',
    pidKey: 'pid',
    sortKey: ''
};
export var convertListToOptions = function(list, options) {
    var mergedOptions = _object_spread({}, defaultConvertConfig, options || {});
    var topId = mergedOptions.topId, idKey = mergedOptions.idKey, pidKey = mergedOptions.pidKey, sortKey = mergedOptions.sortKey;
    var result = [];
    var map = {};
    list.forEach(function(node) {
        node = _object_spread({}, node);
        var id = node[idKey], pid = node[pidKey];
        var children = map[pid] = map[pid] || [];
        if (!result.length && pid === topId) {
            result = children;
        }
        children.push(node);
        node.children = map[id] || (map[id] = []);
    });
    if (sortKey) {
        Object.keys(map).forEach(function(i) {
            if (map[i].length > 1) {
                map[i].sort(function(a, b) {
                    return a[sortKey] - b[sortKey];
                });
            }
        });
    }
    map = null;
    return result;
};
var Tree = /*#__PURE__*/ function() {
    "use strict";
    function Tree(nodes, config) {
        var _this = this;
        _class_call_check(this, Tree);
        _define_property(this, "nodes", void 0);
        _define_property(this, "config", void 0);
        // eslint-disable-next-line class-methods-use-this
        _define_property(this, "isLeaf", function(node, lazy) {
            var leaf = node.leaf, children = node.children;
            var hasChildren = Array.isArray(children) && Boolean(children.length);
            return leaf || !hasChildren && !lazy;
        });
        _define_property(this, "hasChildren", function(node, lazy) {
            var isLeaf = _this.isLeaf(node, lazy);
            if (isLeaf) {
                return false;
            }
            var children = node.children;
            return Array.isArray(children) && Boolean(children.length);
        });
        this.config = _object_spread({
            value: 'value',
            text: 'text',
            children: 'children'
        }, config || {});
        this.nodes = formatTree(nodes, null, this.config);
    }
    _create_class(Tree, [
        {
            key: "updateChildren",
            value: function updateChildren(nodes, parent) {
                if (!parent) {
                    this.nodes = formatTree(nodes, null, this.config);
                } else {
                    parent.children = formatTree(nodes, parent, this.config);
                }
            }
        },
        {
            // for test
            key: "getNodeByValue",
            value: function getNodeByValue(value) {
                var foundNode;
                eachTree(this.nodes, function(node) {
                    if (node.value === value) {
                        foundNode = node;
                        return true;
                    }
                    return null;
                });
                return foundNode;
            }
        },
        {
            key: "getPathNodesByValue",
            value: function getPathNodesByValue(value) {
                if (!value.length) {
                    return [];
                }
                var pathNodes = [];
                var currentNodes = this.nodes;
                while(currentNodes && currentNodes.length){
                    var foundNode = currentNodes.find(function(node) {
                        return node.value === value[node.level];
                    });
                    if (!foundNode) {
                        break;
                    }
                    pathNodes.push(foundNode);
                    currentNodes = foundNode.children;
                }
                return pathNodes;
            }
        }
    ]);
    return Tree;
}();
export default Tree;
