"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    convertListToOptions: function() {
        return convertListToOptions;
    },
    default: function() {
        return _default;
    },
    eachTree: function() {
        return eachTree;
    },
    formatTree: function() {
        return formatTree;
    }
});
var _class_call_check = require("@swc/helpers/_/_class_call_check");
var _create_class = require("@swc/helpers/_/_create_class");
var _define_property = require("@swc/helpers/_/_define_property");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _to_property_key = require("@swc/helpers/_/_to_property_key");
var formatTree = function formatTree1(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 = (0, _object_without_properties._)(node, [
            valueKey,
            textKey,
            childrenKey
        ].map(_to_property_key._));
        var newNode = (0, _object_spread_props._)((0, _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;
    });
};
var eachTree = function eachTree1(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: ''
};
var convertListToOptions = function convertListToOptions(list, options) {
    var mergedOptions = (0, _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 = (0, _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;
        (0, _class_call_check._)(this, Tree);
        (0, _define_property._)(this, "nodes", void 0);
        (0, _define_property._)(this, "config", void 0);
        // eslint-disable-next-line class-methods-use-this
        (0, _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;
        });
        (0, _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 = (0, _object_spread._)({
            value: 'value',
            text: 'text',
            children: 'children'
        }, config || {});
        this.nodes = formatTree(nodes, null, this.config);
    }
    (0, _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;
}();
var _default = Tree;
