// (C) 2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { measureValueFilterMoved } from "../../../events/filters.js";
import { invalidArgumentsProvided } from "../../../events/general.js";
import { dispatchDashboardEvent } from "../../../store/_infra/eventDispatcher.js";
import { selectFilterContextDraggableFilterIndexByRef, selectFilterContextFilters, selectFilterContextMeasureValueFilterByLocalId, } from "../../../store/tabs/filterContext/filterContextSelectors.js";
import { tabsActions } from "../../../store/tabs/index.js";
import { dispatchFilterContextChanged } from "../common.js";
export function* moveMeasureValueFilterHandler(ctx, cmd) {
    const { localIdentifier, index } = cmd.payload;
    const affectedFilter = yield select(selectFilterContextMeasureValueFilterByLocalId(localIdentifier));
    if (!affectedFilter) {
        throw invalidArgumentsProvided(ctx, cmd, `Filter with localIdentifier ${localIdentifier} not found.`);
    }
    const allFilters = yield select(selectFilterContextFilters);
    const maximalTargetIndex = allFilters.length - 1;
    if (index > maximalTargetIndex || index < -1) {
        throw invalidArgumentsProvided(ctx, cmd, `Invalid index (${index}) provided, it must be between -1 and ${maximalTargetIndex}`);
    }
    const originalIndex = yield select(selectFilterContextDraggableFilterIndexByRef(localIdentifier));
    yield put(tabsActions.moveMeasureValueFilter({
        localIdentifier,
        index,
    }));
    const finalIndex = yield select(selectFilterContextDraggableFilterIndexByRef(localIdentifier));
    yield dispatchDashboardEvent(measureValueFilterMoved(ctx, affectedFilter, originalIndex, finalIndex, cmd.correlationId));
    yield call(dispatchFilterContextChanged, ctx, cmd);
}
