import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2026 GoodData Corporation
import { bem } from "../@ui/@utils/bem.js";
import { LoadingSpinner } from "../LoadingSpinner/LoadingSpinner.js";
const { e } = bem("gd-ui-kit-catalog-item-picker");
export function buildSectionHeader(id, title, variant) {
    return {
        type: "static",
        id,
        data: _jsx("div", { className: e("section-header", { variant }), children: title }),
    };
}
export function buildDivider(variant) {
    return {
        type: "static",
        id: "divider-visualization-catalog",
        data: _jsx("div", { className: e("divider", { variant }) }),
    };
}
export function buildLoadingRow(variant) {
    return {
        type: "static",
        id: "catalog-loading-row",
        data: (_jsx("div", { className: e("loading-row", { variant }), children: _jsx(LoadingSpinner, { className: "small" }) })),
    };
}
export function buildMetricListboxItems(params) {
    const items = [];
    const { metricInsightItems, groupedMetricCatalogItems, fromVisualizationTitle, variant, makeInteractiveItem, } = params;
    const hasVisualizationItems = metricInsightItems.length > 0;
    const hasCatalogItems = groupedMetricCatalogItems.some((group) => group.items.length > 0);
    if (hasVisualizationItems) {
        items.push(buildSectionHeader("header-visualization", fromVisualizationTitle ?? "", variant));
    }
    metricInsightItems.forEach((item) => items.push(makeInteractiveItem(item)));
    if (hasVisualizationItems && hasCatalogItems) {
        items.push(buildDivider(variant));
    }
    groupedMetricCatalogItems.forEach((group, index) => {
        items.push(buildSectionHeader(`header-group-${index}`, group.title, variant));
        group.items.forEach((item) => items.push(makeInteractiveItem(item)));
    });
    return items;
}
export function buildDateListboxItems(params) {
    const items = [];
    const { filteredInsightItems, filteredCatalogItems, shouldShowDateDatasetSelector, shouldShowCatalogLoadingRow, hasCatalogItems, fromVisualizationTitle, variant, dateDatasetSelectorItem, makeInteractiveItem, } = params;
    const hasInsightItems = filteredInsightItems.length > 0;
    const shouldRenderDividerBetweenSections = hasInsightItems && (hasCatalogItems || shouldShowDateDatasetSelector || shouldShowCatalogLoadingRow);
    if (hasInsightItems) {
        items.push(buildSectionHeader("header-visualization", fromVisualizationTitle ?? "", variant));
    }
    filteredInsightItems.forEach((item) => items.push(makeInteractiveItem(item)));
    if (shouldRenderDividerBetweenSections) {
        items.push(buildDivider(variant));
    }
    if (dateDatasetSelectorItem) {
        items.push(dateDatasetSelectorItem);
    }
    filteredCatalogItems.forEach((item) => items.push(makeInteractiveItem(item)));
    if (shouldShowCatalogLoadingRow && filteredCatalogItems.length === 0) {
        items.push(buildLoadingRow(variant));
    }
    return items;
}
export function buildAttributeOnlyListboxItems(params) {
    const items = [];
    const { filteredInsightItems, catalogItemsByDataset, ungroupedCatalogItems, shouldShowCatalogLoadingRow, fromVisualizationTitle, ungroupedTitle, variant, makeInteractiveItem, } = params;
    const hasInsightItems = filteredInsightItems.length > 0;
    const hasCatalogItems = catalogItemsByDataset.size > 0;
    const shouldRenderDividerBetweenSections = hasInsightItems && (hasCatalogItems || shouldShowCatalogLoadingRow);
    if (hasInsightItems) {
        items.push(buildSectionHeader("header-visualization", fromVisualizationTitle ?? "", variant));
    }
    filteredInsightItems.forEach((item) => items.push(makeInteractiveItem(item)));
    if (shouldRenderDividerBetweenSections) {
        items.push(buildDivider(variant));
    }
    if (shouldShowCatalogLoadingRow && catalogItemsByDataset.size === 0) {
        items.push(buildLoadingRow(variant));
        return items;
    }
    if (ungroupedCatalogItems.length > 0) {
        items.push(buildSectionHeader("header-ungrouped", ungroupedTitle, variant));
        ungroupedCatalogItems.forEach((item) => items.push(makeInteractiveItem(item)));
    }
    catalogItemsByDataset.forEach((group, datasetKey) => {
        items.push(buildSectionHeader(`header-${datasetKey}`, group.title.toUpperCase(), variant));
        group.items.forEach((item) => items.push(makeInteractiveItem(item)));
    });
    return items;
}
export function buildAttributeListboxItems(params) {
    if (params.effectiveType === "date") {
        return buildDateListboxItems(params);
    }
    return buildAttributeOnlyListboxItems(params);
}
