// (C) 2026 GoodData Corporation
import { useMemo } from "react";
import { useIntl } from "react-intl";
import { areObjRefsEqual, insightMeasures, isInsightWidget, isLocalIdRef, measureItem, } from "@gooddata/sdk-model";
export const isMetricHeaderListItem = (item) => {
    return typeof item === "object" && item !== null && "type" in item && item.type === "header";
};
export const isMetricSeparatorListItem = (item) => {
    return typeof item === "object" && item !== null && "type" in item && item.type === "separator";
};
export function useMetricDropdownItems({ measures, searchQuery, enableMeasureValueFilterKD, insightWidgets, insightsMap, }) {
    const intl = useIntl();
    const metricMeasures = useMemo(() => {
        return enableMeasureValueFilterKD ? measures : [];
    }, [enableMeasureValueFilterKD, measures]);
    const filteredMeasures = useMemo(() => {
        return searchQuery
            ? metricMeasures.filter((m) => m.measure.title.toLowerCase().includes(searchQuery.toLowerCase()))
            : metricMeasures;
    }, [metricMeasures, searchQuery]);
    const dashboardMeasureRefs = useMemo(() => {
        if (!enableMeasureValueFilterKD) {
            return [];
        }
        return insightWidgets.flatMap((widget) => {
            if (!isInsightWidget(widget)) {
                return [];
            }
            const insight = insightsMap.get(widget.insight);
            if (!insight) {
                return [];
            }
            return insightMeasures(insight)
                .map(measureItem)
                .filter((ref) => !!ref && !isLocalIdRef(ref));
        });
    }, [enableMeasureValueFilterKD, insightWidgets, insightsMap]);
    const groupedFilteredMeasures = useMemo(() => {
        const fromDashboard = [];
        const allOther = [];
        filteredMeasures.forEach((measure) => {
            const isFromDashboard = dashboardMeasureRefs.some((ref) => areObjRefsEqual(ref, measure.measure.ref));
            if (isFromDashboard) {
                fromDashboard.push(measure);
                return;
            }
            allOther.push(measure);
        });
        return { fromDashboard, allOther };
    }, [dashboardMeasureRefs, filteredMeasures]);
    const metricDropdownItems = useMemo(() => {
        const { fromDashboard, allOther } = groupedFilteredMeasures;
        const hasFromDashboardMeasures = fromDashboard.length > 0;
        const hasAllOtherMeasures = allOther.length > 0;
        const items = [];
        if (hasFromDashboardMeasures) {
            items.push({
                type: "header",
                title: intl.formatMessage({
                    id: "dashboardMeasureValueFilter.section.from_dashboard",
                }),
            });
            items.push(...fromDashboard);
        }
        if (hasAllOtherMeasures) {
            if (hasFromDashboardMeasures) {
                items.push({ type: "separator" });
            }
            items.push({
                type: "header",
                title: intl.formatMessage({
                    id: "dashboardMeasureValueFilter.section.all_other",
                }),
            });
            items.push(...allOther);
        }
        return items;
    }, [groupedFilteredMeasures, intl]);
    return {
        metricMeasures,
        metricDropdownItems,
    };
}
//# sourceMappingURL=useMetricDropdownItems.js.map