// (C) 2025-2026 GoodData Corporation
import { batchActions } from "redux-batched-actions";
import { put, select } from "redux-saga/effects";
import { dashboardTabSwitched } from "../../events/tabs.js";
import { tabsActions } from "../../store/tabs/index.js";
import { selectActiveTabLocalIdentifier, selectTabs } from "../../store/tabs/tabsSelectors.js";
/**
 * @internal
 */
export function* switchDashboardTabHandler(ctx, cmd) {
    const { tabId } = cmd.payload;
    const currentActiveTabId = yield select(selectActiveTabLocalIdentifier);
    const tabs = yield select(selectTabs);
    const newTab = tabs?.find((t) => t.localIdentifier === tabId);
    if (!newTab) {
        throw new Error(`Tab with id "${tabId}" not found`);
    }
    const actions = [];
    actions.push(tabsActions.setActiveTabLocalIdentifier(newTab.localIdentifier));
    actions.push(tabsActions.clearLayoutHistory());
    yield put(batchActions(actions));
    return dashboardTabSwitched(ctx, currentActiveTabId, tabId, cmd.correlationId);
}
//# sourceMappingURL=switchDashboardTabHandler.js.map