export const KB_PROPERTY = "property";
export const KB_OBJECT = "object";
const getPropertyIcon = (typeId, kbPropertiesTypes) => 
// The object icon has to be retrieved from the ObjectTypes array, by
// matching the ObjectData typeId with the ObjectType id
kbPropertiesTypes.find(propertyType => propertyType.id === typeId).icon;
const convertObjectTypeToTreeItem = (objectType) => ({
    caption: objectType.name,
    id: objectType.id,
    startImgSrc: objectType.icon,
    leaf: false,
    parts: "object-type tree-view-item",
    expanded: true,
    items: []
});
const convertObjectToTreeItem = (object, objectTypes) => ({
    caption: object.name,
    id: object.id,
    startImgSrc: objectTypes.get(object.typeId).icon,
    parts: "object tree-view-item",
    metadata: KB_OBJECT,
    leaf: true
});
const convertPropertyToTreeItem = (property, kbPropertiesTypes) => ({
    caption: property.name,
    id: property.id,
    startImgSrc: getPropertyIcon(property.typeId, kbPropertiesTypes),
    parts: "property tree-view-item",
    metadata: KB_PROPERTY,
    leaf: true
});
/**
 * Updates the tree model with the provided objects.
 * Objects may already exist and are placed under their parent node.
 * If the parent node doesn't exist (identified by typeId on ObjectTypes),
 * it is created and added at the bottom.
 *
 * @param {ObjectData[] | undefined} objects - The array of objects to update.
 * @param {Map<string, ObjectType>} objectTypesMap - A map of object types by ID.
 * @param {Set<string>} objectsSet - A set of existing object IDs.
 * @param {TreeViewModel} actualTreeState - The current tree state to be updated.
 * @returns {TreeViewModel} - The updated tree view model.
 */
export const updateObjectsTreeModel = (objects, objectTypesMap, objectsSet, actualTreeState) => {
    if (!objects) {
        return [...actualTreeState];
    }
    const updatedTreeData = [...actualTreeState];
    objects.forEach(object => {
        // objects that already exists in the actual actualTreeState should not be added.
        if (objectsSet.has(object.id)) {
            // object already exists
            return;
        }
        const objectTreeItem = convertObjectToTreeItem(object, objectTypesMap);
        // TODO: Improve the algorithm efficiency. This check visits potentially all nodes
        // from the TreeViewModel on each iteration. Using a Map to pre-store the TreeView
        // nodes would improve efficiency.
        const objectTypeTreeItemIndex = updatedTreeData.findIndex(treeItem => treeItem.id === object.typeId);
        if (objectTypeTreeItemIndex === -1) {
            // Create and add new object type tree item
            const objectType = objectTypesMap.get(object.typeId);
            if (objectType) {
                const objectTypeTreeItem = convertObjectTypeToTreeItem(objectType);
                objectTypeTreeItem.items.push(objectTreeItem);
                updatedTreeData.push(objectTypeTreeItem);
            }
        }
        else {
            // objectType exists already. insert object as children of the object type tree item.
            updatedTreeData[objectTypeTreeItemIndex].items.push(objectTreeItem);
        }
        objectsSet.add(object.id);
    });
    return updatedTreeData;
};
/**
 * Converts an array of KB properties data into tree item data for a tree view.
 *
 * @param {KBPropertyData[]} properties - The array of KB property data.
 * @param {KBPropertyType[]} kbPropertiesTypes - The array of KB property types.
 * @param {TreeViewModel} actualTreeState - The current state of the tree.
 * @returns {TreeViewModel} - The updated tree view item models.
 */
export const convertKbPropertiesDataToTreeItemData = (properties, kbPropertiesTypes, actualTreeState) => {
    if (!properties) {
        return [...actualTreeState];
    }
    const updatedTreeData = [...actualTreeState];
    properties.forEach(property => {
        // properties have no parent node, and they should be added at the beginning of the tree
        const propertyAlreadyExists = updatedTreeData.find(node => node.id === property.id) !== undefined;
        if (propertyAlreadyExists) {
            return;
        }
        const propertyTreeItem = convertPropertyToTreeItem(property, kbPropertiesTypes);
        updatedTreeData.unshift(propertyTreeItem);
    });
    return updatedTreeData;
};
/**
 * Removes properties from the tree model.
 *
 * @param {TreeViewModel} actualTreeState - The current state of the tree,
 * containing various tree view items.
 * @returns {TreeViewModel} - A new array of tree view items that excludes
 * any items with metadata of type `KB_PROPERTY`.
 */
export const clearKbPropertiesFromTree = (actualTreeState) => actualTreeState.filter(item => item.metadata !== KB_PROPERTY);
//# sourceMappingURL=helpers.js.map
