const MAX_CATEGORY_ITEMS_FIRST_RENDER = 30;
/* objects in file metadata*/
export const KB_OBJECT = "object";
export const KB_CATEGORY = "category";
export const STATUS_REGEXP_KEY = "status";
// - - - - - - - - - - - - - - - - -
// Objects In File Tree Model
// - - - - - - - - - - - - - - - - -
export const convertImportCategoryItemToTreeItem = (importCatIcon, importItems) => {
    const importTreeItems = [];
    importItems.forEach(importItem => {
        importTreeItems.push({
            id: importItem.id,
            caption: importItem.name,
            expanded: true,
            leaf: true,
            startImgSrc: importCatIcon,
            metadata: KB_OBJECT
        });
    });
    return importTreeItems;
};
export const convertImportCategoryDataToTreeView = (importCategoryData) => {
    const treeData = [];
    if (!importCategoryData) {
        return treeData;
    }
    importCategoryData.forEach(importCategory => {
        const importCategoryTreeItem = {
            id: importCategory.id,
            caption: `${importCategory.name} (${importCategory.items.length})`,
            expanded: importCategory.items.length <= MAX_CATEGORY_ITEMS_FIRST_RENDER,
            lazy: importCategory.items.length > MAX_CATEGORY_ITEMS_FIRST_RENDER,
            leaf: false,
            startImgSrc: importCategory.icon,
            metadata: KB_CATEGORY,
            items: importCategory.items.length <= MAX_CATEGORY_ITEMS_FIRST_RENDER ?
                convertImportCategoryItemToTreeItem(importCategory.icon, importCategory.items) : []
        };
        treeData.push(importCategoryTreeItem);
    });
    return treeData;
};
// - - - - - - - - - - - - - - - - -
// Import Status Tree Model
// - - - - - - - - - - - - - - - - -
const createImportResultDataMessages = (importItemId, messagesArray, status) => {
    const messages = [];
    messagesArray.forEach((message, i) => {
        messages.push({
            id: `${importItemId}-message-${i}`,
            caption: message,
            startImgSrc: "gemini-tools/list-view/on-surface",
            leaf: true,
            metadata: JSON.stringify({
                [STATUS_REGEXP_KEY]: status
            })
        });
    });
    return messages;
};
const createImportResultItem = (categoryData, importItemResultData) => {
    const resultItemId = `${importItemResultData.id}-${importItemResultData.name}`;
    const importResultItem = {
        id: resultItemId,
        startImgSrc: categoryData.icon,
        caption: importItemResultData.name,
        parts: `tree-view-item tree-view-item--status tree-view-item--${importItemResultData.status}`,
        expanded: true,
        leaf: importItemResultData.messages.length === 0,
        metadata: JSON.stringify({
            [STATUS_REGEXP_KEY]: importItemResultData.status
        }),
        items: createImportResultDataMessages(resultItemId, importItemResultData.messages, importItemResultData.status)
    };
    return importResultItem;
};
// This functions updates the "Import Status" model, by adding a new result item.
export const addImportItemResultDataItem = (importItemResultData, categoryItemMap, objectCategoryItemMap, importTreeActualState) => {
    const updatedImportTreeModel = [...importTreeActualState];
    // Use the resultItem.id to find the corresponding category in the categoryMap
    const objectCategoryId = objectCategoryItemMap.get(importItemResultData.id);
    const categoryData = categoryItemMap.get(objectCategoryId);
    const categoryIndex = updatedImportTreeModel.findIndex(treeItem => treeItem.id === objectCategoryId);
    if (categoryIndex === -1) {
        // category does not exists yet.
        const category = {
            id: objectCategoryId,
            caption: `${categoryData.name} (1)`,
            startImgSrc: `${categoryData.icon}`,
            expanded: true,
            items: [createImportResultItem(categoryData, importItemResultData)]
        };
        updatedImportTreeModel.push(category);
        return updatedImportTreeModel;
    }
    // else category already exists. Just insert the result item in the category.
    const resultItem = createImportResultItem(categoryData, importItemResultData);
    updatedImportTreeModel[categoryIndex].items = [
        ...updatedImportTreeModel[categoryIndex].items,
        resultItem
    ];
    // update category caption length
    const newItemsLength = updatedImportTreeModel[categoryIndex].items.length;
    updatedImportTreeModel[categoryIndex].caption = `${categoryData.name} (${newItemsLength})`;
    return updatedImportTreeModel;
};
//# sourceMappingURL=helpers.js.map
