// (C) 2023-2026 GoodData Corporation
import { compact } from "lodash-es";
import { serializeObjRef, } from "@gooddata/sdk-model";
import { CatalogAttributeDataType } from "./types.js";
export const findCatalogAttributeByRef = (catalogAttributesMap, ref) => {
    return catalogAttributesMap.get(getCatalogKey(ref));
};
export const convertToCatalogAttributeData = (attributes, dates) => {
    const attrs = (attributes ?? []).map((it) => ({
        type: CatalogAttributeDataType.ATTRIBUTE,
        ref: it.attribute.ref,
        title: it.attribute.title,
        icon: "gd-icon-attribute",
    }));
    const dateAttributes = (dates ?? [])
        .flatMap((it) => it.dateAttributes)
        .flatMap((it) => ({
        type: CatalogAttributeDataType.DATE_ATTRIBUTE,
        ref: it.attribute.ref,
        title: it.attribute.title,
        icon: "gd-icon-date",
    }));
    return [...attrs, ...dateAttributes].reduce((map, attribute) => {
        map.set(getCatalogKey(attribute.ref), attribute);
        return map;
    }, new Map());
};
export const convertToCatalogAttributeDataByRefs = (catalogAttributesMap, refs) => {
    return compact(refs.map((ref) => findCatalogAttributeByRef(catalogAttributesMap, ref)));
};
export const appendEmptyAttribute = (attributes, baseRowIndex) => {
    const uncompletedItemIndex = attributes.findIndex((attribute) => !attribute.completed);
    const newAttributes = [...attributes].filter((attribute) => attribute.completed);
    // if uncompleted item is above baseRowIndex, we need to insert new empty item at baseRowIndex
    const rowIndex = uncompletedItemIndex > -1 && uncompletedItemIndex < baseRowIndex ? baseRowIndex : baseRowIndex + 1;
    newAttributes.splice(rowIndex, 0, {});
    return newAttributes;
};
export const searchAttributes = (attributes, selectedTab, searchString) => {
    return attributes
        .filter((item) => item.type === selectedTab)
        .filter((item) => item.title?.toLowerCase().includes(searchString.toLowerCase()));
};
const getCatalogKey = (ref) => {
    return serializeObjRef(ref);
};
//# sourceMappingURL=utils.js.map