// (C) 2024-2026 GoodData Corporation
import { batchActions } from "redux-batched-actions";
import { put, select } from "redux-saga/effects";
import { getSizeInfo } from "../../../_staging/layout/sizing.js";
import { visualizationSwitcherWidgetVisualizationAdded, } from "../../events/visualizationSwitcher.js";
import { selectSettings } from "../../store/config/configSelectors.js";
import { insightsActions } from "../../store/insights/index.js";
import { tabsActions } from "../../store/tabs/index.js";
import { selectWidgetsMap } from "../../store/tabs/layout/layoutSelectors.js";
import { validateExistingVisualizationSwitcherWidget } from "./validation/widgetValidations.js";
export function* addVisualizationToSwticherWidgetContentHandler(ctx, cmd) {
    const { payload: { visualization, insight }, correlationId, } = cmd;
    const widgets = yield select(selectWidgetsMap);
    const visualizationSwitcherWidget = validateExistingVisualizationSwitcherWidget(widgets, cmd, ctx);
    const settings = yield select(selectSettings);
    const newSize = getSizeInfo(settings, "insight", insight);
    yield put(batchActions([
        insightsActions.upsertInsight(insight),
        tabsActions.addVisualizationSwitcherWidgetVisualization({
            ref: visualizationSwitcherWidget.ref,
            visualization,
            newSize,
            undo: {
                cmd,
            },
        }),
    ]));
    return visualizationSwitcherWidgetVisualizationAdded(ctx, visualizationSwitcherWidget.ref, visualization, correlationId);
}
