// (C) 2025-2026 GoodData Corporation
import { put, select } from "redux-saga/effects";
import { invalidArgumentsProvided } from "../../events/general.js";
import { dashboardTabRepositioned } from "../../events/tabs.js";
import { tabsActions } from "../../store/tabs/index.js";
import { selectActiveTabLocalIdentifier, selectTabs } from "../../store/tabs/tabsSelectors.js";
/**
 * @internal
 */
export function* repositionDashboardTabHandler(ctx, cmd) {
    const { oldIndex, newIndex } = cmd.payload;
    const tabs = yield select(selectTabs);
    const activeTabLocalIdentifier = yield select(selectActiveTabLocalIdentifier);
    if (!tabs || tabs.length === 0) {
        throw invalidArgumentsProvided(ctx, cmd, "Attempting to reposition tab when there are no tabs.");
    }
    if (!isValidIndex(oldIndex, tabs)) {
        throw invalidArgumentsProvided(ctx, cmd, `Attempting to reposition non-existent tab from index ${oldIndex}. There are ${tabs.length} tabs.`);
    }
    if (!isValidIndex(newIndex, tabs)) {
        throw invalidArgumentsProvided(ctx, cmd, `Attempting to reposition tab to invalid index ${newIndex}. There are ${tabs.length} tabs.`);
    }
    const updatedTabs = tabs.slice();
    const [moved] = updatedTabs.splice(oldIndex, 1);
    // Insert at newIndex directly so the final position matches the requested index
    updatedTabs.splice(newIndex, 0, moved);
    // Persist reordered tabs and keep currently active tab as-is
    yield put(tabsActions.setTabs({
        tabs: updatedTabs,
        activeTabLocalIdentifier,
    }));
    return dashboardTabRepositioned(ctx, oldIndex, newIndex, cmd.correlationId);
}
function isValidIndex(index, array) {
    return index >= 0 && index < array.length && Number.isInteger(index);
}
//# sourceMappingURL=repositionDashboardTabHandler.js.map