// (C) 2022-2026 GoodData Corporation
import { useCallback } from "react";
import { insightVisualizationType, widgetRef } from "@gooddata/sdk-model";
import { changeInsightWidgetVisProperties } from "../../../../../model/commands/insight.js";
import { useDashboardDispatch, useDashboardSelector, } from "../../../../../model/react/DashboardStoreProvider.js";
import { selectSettings } from "../../../../../model/store/config/configSelectors.js";
import { selectIsInEditMode } from "../../../../../model/store/renderMode/renderModeSelectors.js";
function isSupportedWidgetProperties(properties, isInEditMode, enableNewPivotTable) {
    // currently we only support the columnWidths and textWrapping for pivot tables
    // this should be ideally driven by the PlugVis API, not hardcoded here
    const controls = properties?.controls;
    const hasColumnWidths = !!controls?.columnWidths;
    const hasTextWrapping = !!controls?.textWrapping;
    if (enableNewPivotTable) {
        return hasColumnWidths || hasTextWrapping;
    }
    if (isInEditMode) {
        return hasColumnWidths;
    }
    return false;
}
export function useHandlePropertiesPushData(widget, insight) {
    const ref = widgetRef(widget);
    const dispatch = useDashboardDispatch();
    const isInEditMode = useDashboardSelector(selectIsInEditMode);
    const settings = useDashboardSelector(selectSettings);
    const enableNewPivotTable = settings?.enableNewPivotTable ?? true;
    const visType = insightVisualizationType(insight);
    return useCallback((data) => {
        // propagate properties from push data only for pivot tables (this is how gdc-dashboard does it)
        if (isSupportedWidgetProperties(data.properties, isInEditMode, enableNewPivotTable) &&
            visType === "table") {
            dispatch(changeInsightWidgetVisProperties(ref, data.properties));
        }
    }, [dispatch, ref, visType, isInEditMode, enableNewPivotTable]);
}
//# sourceMappingURL=useHandlePropertiesPushData.js.map