import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { useMemo } from "react";
import { DateDatasetPickerItem } from "./DateDatasetPickerItem.js";
import { buildAttributeListboxItems, buildLoadingRow, buildMetricListboxItems, } from "./listboxItemBuilders.js";
export function useCatalogItemPickerListboxItems(params) {
    const { effectiveType, metricInsightItems, groupedMetricCatalogItems, filteredInsightItems, filteredCatalogItems, catalogItemsByDataset, ungroupedCatalogItems, selectedIds, isLoading, shouldShowDateDatasetSelector, dateDatasetOptions, effectiveSelectedDateDatasetKey, isDateDatasetDropdownOpen, listboxId, labels, variant, setSelectedDateDatasetKey, setIsDateDatasetDropdownOpen, } = params;
    return useMemo(() => {
        const makeInteractiveItem = (item) => ({
            type: "interactive",
            id: item.id,
            stringTitle: item.title,
            data: {
                item,
                isSelected: selectedIds.has(item.id),
            },
        });
        if (effectiveType === "metric") {
            const metricItems = buildMetricListboxItems({
                metricInsightItems,
                groupedMetricCatalogItems,
                fromVisualizationTitle: labels.fromVisualization,
                variant,
                makeInteractiveItem,
            });
            if (isLoading && metricItems.length === 0) {
                return [buildLoadingRow(variant)];
            }
            return metricItems;
        }
        const shouldShowCatalogLoadingRow = isLoading && filteredCatalogItems.length === 0;
        const hasCatalogItems = effectiveType === "date" ? filteredCatalogItems.length > 0 : catalogItemsByDataset.size > 0;
        const dateDatasetSelectorItem = shouldShowDateDatasetSelector && effectiveType === "date"
            ? {
                type: "static",
                id: "date-dataset-selector",
                data: (_jsx(DateDatasetPickerItem, { dateDatasetOptions: dateDatasetOptions, selectedDateDatasetKey: effectiveSelectedDateDatasetKey, isDropdownOpen: isDateDatasetDropdownOpen, onSelect: (key) => {
                        setSelectedDateDatasetKey(key);
                        setIsDateDatasetDropdownOpen(false);
                    }, onToggleDropdown: () => setIsDateDatasetDropdownOpen((open) => !open), listboxId: listboxId, dateAsLabel: labels.dateAsLabel ?? "" })),
            }
            : undefined;
        return buildAttributeListboxItems({
            filteredInsightItems,
            filteredCatalogItems,
            catalogItemsByDataset,
            ungroupedCatalogItems,
            shouldShowDateDatasetSelector,
            shouldShowCatalogLoadingRow,
            hasCatalogItems,
            effectiveType: effectiveType === "date" ? "date" : "attribute",
            fromVisualizationTitle: labels.fromVisualization,
            ungroupedTitle: labels.ungroupedTitle,
            variant,
            dateDatasetSelectorItem,
            makeInteractiveItem,
        });
    }, [
        catalogItemsByDataset,
        dateDatasetOptions,
        effectiveSelectedDateDatasetKey,
        effectiveType,
        filteredCatalogItems,
        filteredInsightItems,
        groupedMetricCatalogItems,
        isDateDatasetDropdownOpen,
        isLoading,
        listboxId,
        metricInsightItems,
        selectedIds,
        setIsDateDatasetDropdownOpen,
        setSelectedDateDatasetKey,
        shouldShowDateDatasetSelector,
        labels,
        ungroupedCatalogItems,
        variant,
    ]);
}
