"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "traverseTree", {
    enumerable: true,
    get: function() {
        return traverseTree;
    }
});
var _dedupe = /*#__PURE__*/ _interop_require_default(require("classnames/dedupe"));
var _utils = require(".");
var _Treetokens = require("../Tree.tokens");
function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
// Поиск в глубину для обхода узлов дерева.
var dfs = function dfs1(node, selectedKeys, rootParentsSelected, parents) {
    var _node_children, _parents_filter_;
    if (selectedKeys.has(node.key)) {
        parents.forEach(function(parent) {
            parent.className = (0, _dedupe.default)(parent.className, _Treetokens.classes.treeItemHasSelectedChildren);
        });
    }
    (_node_children = node.children) === null || _node_children === void 0 ? void 0 : _node_children.forEach(function(childNode) {
        return dfs(childNode, selectedKeys, rootParentsSelected, _to_consumable_array(parents).concat([
            node
        ]));
    });
    if (rootParentsSelected.has((_parents_filter_ = parents.filter(function(parent) {
        return parent.key !== 'root';
    })[0]) === null || _parents_filter_ === void 0 ? void 0 : _parents_filter_.key)) {
        node.className = (0, _dedupe.default)(node.className, _Treetokens.classes.treeItemPrimaryBackground);
    }
    if (!node.children) {
        node.className = (0, _dedupe.default)(node.className, _Treetokens.classes.treeLeafNode);
    }
};
var getRootParentsSelectedChildren = function getRootParentsSelectedChildren(node, selectedKeysSet) {
    var rootParentsSelected = new Set();
    var dfs = function dfs1(node, currentRootParentKey) {
        if (rootParentsSelected.has(currentRootParentKey)) {
            return;
        }
        if (selectedKeysSet.has(node.key)) {
            rootParentsSelected.add(currentRootParentKey);
        }
        if (node.children) {
            node.children.forEach(function(childNode) {
                dfs(childNode, currentRootParentKey);
            });
        }
    };
    if (node.children) {
        node.children.forEach(function(rootNode) {
            dfs(rootNode, rootNode.key);
        });
    }
    return rootParentsSelected;
};
var traverseTree = function traverseTree(items, selectedKeys) {
    var selectedKeysSet = new Set(selectedKeys);
    var copyItems = (0, _utils.deepCopy)(items);
    var rootParentsSelected = getRootParentsSelectedChildren({
        key: 'root',
        children: copyItems
    }, selectedKeysSet);
    copyItems.forEach(function(item) {
        dfs(item, selectedKeysSet, rootParentsSelected, [
            item
        ]);
    });
    return copyItems;
};
