// (C) 2021-2026 GoodData Corporation
import { useCallback, useMemo } from "react";
import { areObjRefsEqual } from "@gooddata/sdk-model";
import { setShowWidgetAsTable } from "../../../model/commands/showWidgetAsTable.js";
import { useDashboardDispatch, useDashboardSelector } from "../../../model/react/DashboardStoreProvider.js";
import { selectShowWidgetAsTable } from "../../../model/store/showWidgetAsTable/showWidgetAsTableSelectors.js";
/**
 * Hook for toggling and checking if a widget should be shown as table.
 *
 * @param widget - The widget to check and toggle.
 * @returns isWidgetAsTable, toggleWidgetAsTable
 *
 * @beta
 */
export function useShowAsTable(widget) {
    const dispatch = useDashboardDispatch();
    const widgetsAsTable = useDashboardSelector(selectShowWidgetAsTable);
    const isWidgetAsTable = useMemo(() => widgetsAsTable.some((ref) => areObjRefsEqual(ref, widget.ref)), [widgetsAsTable, widget.ref]);
    const toggleWidgetAsTable = useCallback(() => {
        dispatch(setShowWidgetAsTable(widget.ref, !isWidgetAsTable));
    }, [dispatch, widget.ref, isWidgetAsTable]);
    const setWidgetAsTable = useCallback((asTable) => {
        dispatch(setShowWidgetAsTable(widget.ref, asTable));
    }, [dispatch, widget.ref]);
    return { isWidgetAsTable, toggleWidgetAsTable, setWidgetAsTable };
}
