// (C) 2022-2026 GoodData Corporation
import { useEffect, useMemo } from "react";
import { queryAttributeByDisplayForm, } from "../../model/queries/attributes.js";
import { useDashboardSelector } from "../../model/react/DashboardStoreProvider.js";
import { useDashboardQueryProcessing } from "../../model/react/useDashboardQueryProcessing.js";
import { selectIsNewDashboard } from "../../model/store/meta/metaSelectors.js";
import { selectPreloadedAttributesWithReferences } from "../../model/store/tabs/filterContext/filterContextSelectors.js";
/**
 * @internal
 */
export function useAttributes(displayForms) {
    const { run: getAttributes, result: attributes, status: attributesLoadingStatus, error: attributesLoadingError, } = useDashboardQueryProcessing({
        queryCreator: queryAttributeByDisplayForm,
    });
    // First wait for preloaded filter attributes, otherwise we might be spawning lot of unnecessary requests
    const attributesWithReferences = useDashboardSelector(selectPreloadedAttributesWithReferences);
    const isNewDashboard = useDashboardSelector(selectIsNewDashboard);
    useEffect(() => {
        const shouldLoad = isNewDashboard || attributesWithReferences;
        if (shouldLoad) {
            getAttributes(displayForms);
        }
    }, [displayForms, getAttributes, isNewDashboard, attributesWithReferences]);
    const attributesLoading = useMemo(() => {
        return attributesLoadingStatus === "pending" || attributesLoadingStatus === "running";
    }, [attributesLoadingStatus]);
    return {
        attributes,
        attributesLoading,
        attributesLoadingError,
    };
}
//# sourceMappingURL=useAttributes.js.map