// (C) 2025-2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { switchDashboardTab } from "../../commands/tabs.js";
import { invalidArgumentsProvided } from "../../events/general.js";
import { dashboardTabDeleted, } from "../../events/tabs.js";
import { dispatchDashboardEvent } from "../../store/_infra/eventDispatcher.js";
import { tabsActions } from "../../store/tabs/index.js";
import { selectAllAnalyticalWidgets } from "../../store/tabs/layout/layoutSelectors.js";
import { selectActiveTabLocalIdentifier, selectTabs } from "../../store/tabs/tabsSelectors.js";
import { validateDrills } from "../common/validateDrills.js";
import { switchDashboardTabHandler } from "./switchDashboardTabHandler.js";
/**
 * @internal
 */
export function* deleteDashboardTabHandler(ctx, cmd) {
    const { tabId } = cmd.payload;
    const tabs = yield select(selectTabs);
    const activeTabLocalIdentifier = yield select(selectActiveTabLocalIdentifier);
    if (!tabs || tabs.length === 0) {
        throw invalidArgumentsProvided(ctx, cmd, "Attempting to delete tab when there are no tabs.");
    }
    const index = tabs.findIndex((t) => t.localIdentifier === tabId);
    if (index < 0) {
        throw invalidArgumentsProvided(ctx, cmd, `Attempting to delete non-existent tab with id ${tabId}. There are ${tabs.length} tabs.`);
    }
    const isActive = activeTabLocalIdentifier === tabId;
    let nextActiveTabLocalIdentifier = undefined;
    if (isActive) {
        // Prefer tab at the same index after deletion, then previous index
        const hasNextAtSameIndex = index + 1 < tabs.length; // there is an item after current index
        if (hasNextAtSameIndex) {
            nextActiveTabLocalIdentifier = tabs[index + 1].localIdentifier;
        }
        else if (index - 1 >= 0) {
            nextActiveTabLocalIdentifier = tabs[index - 1].localIdentifier;
        }
        else {
            nextActiveTabLocalIdentifier = undefined;
        }
        if (nextActiveTabLocalIdentifier) {
            // Switch to the next tab first so the current active tab state is saved back
            const switchedEvent = yield call(switchDashboardTabHandler, ctx, switchDashboardTab(nextActiveTabLocalIdentifier, cmd.correlationId));
            // Ensure app-level sagas receive the switched event for URL sync etc.
            yield dispatchDashboardEvent(switchedEvent);
        }
        else {
            // No next active -> clear active tab id explicitly
            yield put(tabsActions.setActiveTabLocalIdentifier(undefined));
        }
    }
    // Remove the tab
    yield put(tabsActions.removeTabById(tabId));
    // Validate drills after tab deletion to check if any widgets drill to the deleted tab
    const widgets = yield select(selectAllAnalyticalWidgets);
    yield call(validateDrills, ctx, cmd, widgets);
    // Emit deleted event
    return dashboardTabDeleted(ctx, tabId, index, nextActiveTabLocalIdentifier, cmd.correlationId);
}
//# sourceMappingURL=deleteDashboardTabHandler.js.map