// (C) 2026 GoodData Corporation
import { useMemo } from "react";
import { objRefToString } from "@gooddata/sdk-model";
const isDateItem = (item) => item.type === "date";
function filterItemsByType(items, activeType) {
    return items.filter((item) => (activeType === "date" ? isDateItem(item) : !isDateItem(item)));
}
function dedupeCatalogAgainstInsight(insightItems, catalogItems) {
    const insightRefKeys = new Set(insightItems.map((i) => (i.ref ? objRefToString(i.ref) : undefined)).filter((x) => !!x));
    const insightTitlesWithoutRef = new Set(insightItems.filter((i) => !i.ref).map((i) => i.title));
    return catalogItems.filter((item) => {
        const itemKey = item.ref ? objRefToString(item.ref) : undefined;
        if (itemKey && insightRefKeys.has(itemKey)) {
            return false;
        }
        return !insightTitlesWithoutRef.has(item.title);
    });
}
function filterCatalogBySelectedDateDataset(catalogItems, activeType, selectedDateDatasetKey) {
    if (activeType !== "date" || !selectedDateDatasetKey) {
        return catalogItems;
    }
    return catalogItems.filter((item) => {
        if (!item.dataset) {
            return true;
        }
        return objRefToString(item.dataset.identifier) === selectedDateDatasetKey;
    });
}
function filterItemsBySearch(items, searchString) {
    if (!searchString.trim()) {
        return items;
    }
    const lowerSearch = searchString.toLowerCase();
    return items.filter((item) => item.title.toLowerCase().includes(lowerSearch));
}
function groupCatalogItemsByDataset(catalogItems) {
    const grouped = new Map();
    const ungroupedItems = [];
    catalogItems.forEach((item) => {
        if (item.dataset) {
            const datasetKey = objRefToString(item.dataset.identifier);
            if (!grouped.has(datasetKey)) {
                grouped.set(datasetKey, { title: item.dataset.title, items: [] });
            }
            grouped.get(datasetKey).items.push(item);
            return;
        }
        ungroupedItems.push(item);
    });
    return { grouped, ungroupedItems };
}
/**
 * @internal
 */
export function useAttributeItemsData({ availableInsightItems, availableCatalogItems, searchString, activeType, selectedDateDatasetKey, }) {
    return useMemo(() => {
        const insightItemsAfterType = filterItemsByType(availableInsightItems, activeType);
        const catalogItemsAfterType = filterItemsByType(availableCatalogItems, activeType);
        const dedupedCatalog = dedupeCatalogAgainstInsight(insightItemsAfterType, catalogItemsAfterType);
        const dateDatasetFilteredCatalog = filterCatalogBySelectedDateDataset(dedupedCatalog, activeType, selectedDateDatasetKey);
        const filteredInsightItems = filterItemsBySearch(insightItemsAfterType, searchString);
        const filteredCatalogItems = filterItemsBySearch(dateDatasetFilteredCatalog, searchString);
        // Date items are not grouped by dataset (they use the date dataset selector instead)
        if (activeType === "date") {
            return {
                filteredInsightItems,
                filteredCatalogItems,
                catalogItemsByDataset: new Map(),
                ungroupedCatalogItems: [],
            };
        }
        const { grouped, ungroupedItems } = groupCatalogItemsByDataset(filteredCatalogItems);
        return {
            filteredInsightItems,
            catalogItemsByDataset: grouped,
            filteredCatalogItems,
            ungroupedCatalogItems: ungroupedItems,
        };
    }, [availableInsightItems, availableCatalogItems, searchString, activeType, selectedDateDatasetKey]);
}
