// (C) 2025-2026 GoodData Corporation
import { useMemo } from "react";
import { objRefToString } from "@gooddata/sdk-model";
import { useKdaState } from "../providers/KdaState.js";
import { useAttribute } from "./useAttribute.js";
export function useSummaryDrivers() {
    const { state } = useKdaState();
    const attributeFinder = useAttribute();
    return useMemo(() => {
        //Groups
        const groups = state.items.reduce((prev, { data }) => {
            const ref = objRefToString(data.attribute);
            if (!prev[ref]) {
                prev[ref] = createKdaGroup(ref, data.attribute, data.displayForm, data.title, data.description);
            }
            if (data.isSignificant) {
                prev[ref].significantDrivers.push(data);
            }
            prev[ref].allDrivers.push(data);
            return prev;
        }, {});
        //Selected fill empty groups
        state.selectedAttributes.reduce((prev, current) => {
            const ref = objRefToString(current);
            if (!prev[ref]) {
                const attribute = attributeFinder(current);
                if (attribute) {
                    prev[ref] = createKdaGroup(ref, current, attribute.defaultDisplayForm, attribute.attribute.title, attribute.attribute.description);
                }
            }
            return prev;
        }, groups);
        return Object.values(groups).sort((a, b) => b.significantDrivers.length - a.significantDrivers.length);
    }, [state.items, attributeFinder, state.selectedAttributes]);
}
function createKdaGroup(id, attribute, displayForm, title, description) {
    return {
        id,
        significantDrivers: [],
        allDrivers: [],
        attribute,
        displayForm,
        title,
        description,
    };
}
//# sourceMappingURL=useSummaryDrivers.js.map