// (C) 2022-2026 GoodData Corporation
import { useMemo } from "react";
import stringify from "json-stable-stringify";
import { insightSetFilters } from "@gooddata/sdk-model";
import { useBackendStrict, useExecutionDataView, useWorkspaceStrict, } from "@gooddata/sdk-ui";
import { useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { useWidgetFilters } from "../../../model/react/useWidgetFilters.js";
import { selectInsightByRef } from "../../../model/store/insights/insightsSelectors.js";
/**
 * This hook provides an easy way to read a data view from insight widget.
 *
 * @param config - configuration of the hook
 *
 * @public
 */
export function useInsightWidgetDataView({ insightWidget, onCancel, onError, onLoading, onPending, onSuccess, }) {
    const backend = useBackendStrict();
    const workspace = useWorkspaceStrict();
    const insight = useDashboardSelector(selectInsightByRef(insightWidget?.insight));
    const widgetFiltersPromise = useWidgetFilters(insightWidget);
    const insightWithAddedFilters = useMemo(() => insightSetFilters(insight, widgetFiltersPromise.result), 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [
        insight,
        /**
         * We use stringified value to avoid setting equal filters. This prevents cascading cache invalidation
         * and expensive re-renders down the line. The stringification is worth it as the filters are usually
         * pretty small thus saving more time than it is taking.
         */
        // eslint-disable-next-line react-hooks/exhaustive-deps
        stringify(widgetFiltersPromise.result),
    ]);
    const insightExecution = useMemo(() => {
        return insightWithAddedFilters && insightWidget
            ? backend.workspace(workspace).execution().forInsight(insightWithAddedFilters)
            : undefined;
    }, [backend, workspace, insightWithAddedFilters, insightWidget]);
    return useExecutionDataView({
        execution: insightExecution,
        onCancel,
        onError,
        onLoading,
        onPending,
        onSuccess,
    });
}
