import cls from "classnames/dedupe";
import { deepCopy } from ".";
import { classes } from "../Tree.tokens";
// Поиск в глубину для обхода узлов дерева.
const dfs = (node, selectedKeys, rootParentsSelected, parents)=>{
    if (selectedKeys.has(node.key)) {
        parents.forEach((parent)=>{
            parent.className = cls(parent.className, classes.treeItemHasSelectedChildren);
        });
    }
    node.children?.forEach((childNode)=>dfs(childNode, selectedKeys, rootParentsSelected, [
            ...parents,
            node
        ]));
    if (rootParentsSelected.has(parents.filter((parent)=>parent.key !== 'root')[0]?.key)) {
        node.className = cls(node.className, classes.treeItemPrimaryBackground);
    }
    if (!node.children) {
        node.className = cls(node.className, classes.treeLeafNode);
    }
};
const getRootParentsSelectedChildren = (node, selectedKeysSet)=>{
    const rootParentsSelected = new Set();
    const dfs = (node, currentRootParentKey)=>{
        if (rootParentsSelected.has(currentRootParentKey)) {
            return;
        }
        if (selectedKeysSet.has(node.key)) {
            rootParentsSelected.add(currentRootParentKey);
        }
        if (node.children) {
            node.children.forEach((childNode)=>{
                dfs(childNode, currentRootParentKey);
            });
        }
    };
    if (node.children) {
        node.children.forEach((rootNode)=>{
            dfs(rootNode, rootNode.key);
        });
    }
    return rootParentsSelected;
};
// Перебираем и клонируем элементы в дереве, а также добавляем к ним класснеймы.
export const traverseTree = (items, selectedKeys)=>{
    const selectedKeysSet = new Set(selectedKeys);
    const copyItems = deepCopy(items);
    const rootParentsSelected = getRootParentsSelectedChildren({
        key: 'root',
        children: copyItems
    }, selectedKeysSet);
    copyItems.forEach((item)=>{
        dfs(item, selectedKeysSet, rootParentsSelected, [
            item
        ]);
    });
    return copyItems;
};
