import { getIconPath } from "@genexus/mercury";
export const ACTIVE_ITEM_DEFAULT_ICON = getIconPath({
    category: "system",
    name: "check-circle",
    colorType: "primary"
});
export const convertItemsNodeListToFlattenedTreeViewModel = (itemsList, checkboxType) => {
    const itemsListFlattenedTreeViewModel = [];
    const processNode = (item) => {
        var _a;
        const startImgSrc = item.active && !item.iconStart
            ? ACTIVE_ITEM_DEFAULT_ICON
            : item.iconStart;
        const leaf = !((_a = item.nodes) === null || _a === void 0 ? void 0 : _a.length);
        const checkbox = checkboxType === "all" ||
            (checkboxType === "custom" && item.checkbox) ||
            (checkboxType === "leaf" && leaf);
        const metaData = JSON.stringify({
            versionType: item.type
        });
        itemsListFlattenedTreeViewModel.push({
            caption: item.name,
            checkbox: checkbox,
            checked: item.checked,
            startImgSrc: startImgSrc,
            endImgSrc: item.iconEnd,
            expanded: item.expanded,
            id: item.id.toString(),
            items: null,
            leaf: leaf,
            metadata: metaData,
            parts: item.parts,
            selected: item.selected,
            order: item.order
        });
        if (item.nodes) {
            item.nodes.forEach(childNode => processNode(childNode));
        }
    };
    itemsList.forEach(item => processNode(item));
    return itemsListFlattenedTreeViewModel;
};
export const convertItemsListToTreeViewModel = (itemsList, checkboxType) => {
    const itemsListTreeViewModel = [];
    itemsList.forEach(item => {
        var _a;
        const startImgSrc = item.active && !item.iconStart
            ? ACTIVE_ITEM_DEFAULT_ICON
            : item.iconStart;
        const leaf = !((_a = item.nodes) === null || _a === void 0 ? void 0 : _a.length);
        const checkbox = checkboxType === "all" ||
            (checkboxType === "custom" && item.checkbox) ||
            (checkboxType === "leaf" && leaf);
        const expanded = item.expanded;
        const iconStart = startImgSrc;
        const iconEnd = item.iconEnd;
        const metaData = JSON.stringify({
            versionType: item.type
        });
        const order = parseInt(item.id);
        itemsListTreeViewModel.push({
            caption: item.name,
            checkbox: checkbox,
            checked: item.checked,
            startImgSrc: iconStart,
            endImgSrc: iconEnd,
            expanded: expanded,
            id: item.id.toString(),
            items: !leaf
                ? convertItemsListToTreeViewModel(item.nodes, checkboxType)
                : null,
            leaf: leaf,
            metadata: metaData,
            parts: item.parts,
            order: order,
            selected: item.selected
        });
    });
    return itemsListTreeViewModel;
};
const findItemNode = (id, item) => {
    if (item.id === id) {
        return item;
    }
    if (item.nodes) {
        for (const itemNode of item.nodes) {
            const result = findItemNode(id, itemNode);
            if (result) {
                return result;
            }
        }
    }
    return null;
};
function findItemInArray(itemsNodes, id) {
    for (const item of itemsNodes) {
        const result = findItemNode(id, item);
        if (result) {
            return result;
        }
    }
    return null;
}
const setAllLeafsCheckedFalse = (itemsList) => {
    const uncheck = (itemNode) => {
        var _a;
        itemNode.checked = false;
        if ((_a = itemNode.nodes) === null || _a === void 0 ? void 0 : _a.length) {
            itemNode.nodes.forEach(childNode => uncheck(childNode));
        }
    };
    itemsList.forEach(item => uncheck(item));
    return itemsList;
};
export const unselectAllNodes = (itemsList) => {
    const unselect = (itemNode) => {
        var _a;
        itemNode.selected = false;
        if ((_a = itemNode.nodes) === null || _a === void 0 ? void 0 : _a.length) {
            itemNode.nodes.forEach(childNode => unselect(childNode));
        }
    };
    itemsList.forEach(item => unselect(item));
};
export const checkItems = (itemsList, idsChecked) => {
    setAllLeafsCheckedFalse(itemsList);
    idsChecked.forEach(id => {
        const itemFind = findItemInArray(itemsList, id);
        if (itemFind) {
            itemFind.checked = true;
        }
    });
    return itemsList;
};
export const expandItems = (itemsList, idsChecked) => {
    idsChecked.forEach(id => {
        const itemFind = findItemInArray(itemsList, id);
        if (itemFind) {
            itemFind.expanded = true;
        }
    });
    return itemsList;
};
export const selectItems = (itemsList, idsSelected) => {
    idsSelected.forEach(id => {
        const itemFind = findItemInArray(itemsList, id);
        if (itemFind) {
            itemFind.selected = true;
        }
    });
    return itemsList;
};
function searchInNode(item, id) {
    var _a;
    // If the current node is the searched node, we return an empty list (it has no parents in this branch)
    if (item.id === id) {
        return [];
    }
    // We traverse each child looking for the target node
    if ((_a = item.nodes) === null || _a === void 0 ? void 0 : _a.length) {
        for (const node of item.nodes) {
            const parents = searchInNode(node, id);
            if (parents !== null) {
                // If we find it, we include the current node at the beginning of the path and return
                return [item.id, ...parents];
            }
        }
    }
    return null;
}
export const searchParentsItems = (items, ids) => {
    const parents = [];
    for (const id of ids) {
        for (const rootItem of items) {
            const results = searchInNode(rootItem, id);
            if (results !== null) {
                results.forEach(result => {
                    parents.push(result);
                });
            }
        }
    }
    return parents;
};
/**
 * Recursively searches for the first node with the active=true property
 * @param nodes Array of nodes to search through
 * @returns An object with caption, iconStart, and path to the first active node found, or null if none is found
 */
export const findFirstActiveNode = (nodes) => {
    // Helper function that keeps track of the path during recursion
    const findActiveWithPath = (nodeList, currentPath = []) => {
        // Base case: if nodes is undefined or empty
        if (!nodeList || nodeList.length === 0) {
            return null;
        }
        // Iterate through each node in the array
        for (const node of nodeList) {
            // Create a new path that includes the current node
            const nodePath = [...currentPath, node.id];
            // Check if the current node is active
            if (node.active === true) {
                return {
                    caption: node.name,
                    iconStart: node.iconStart,
                    path: nodePath
                };
            }
            // If the node has children, search through them with the updated path
            if (node.nodes && node.nodes.length > 0) {
                const result = findActiveWithPath(node.nodes, nodePath);
                // If we find an active node in this subtree, return it immediately
                if (result !== null) {
                    return result;
                }
            }
        }
        // No active node found in any of the nodes
        return null;
    };
    // Start the search with an empty path
    return findActiveWithPath(nodes);
};
//# sourceMappingURL=helpers.js.map
