// (C) 2022-2026 GoodData Corporation
import { useCallback } from "react";
import { areItemsInSameSection, getItemIndex, updateItemIndex, } from "../../../../_staging/layout/coordinates.js";
import { moveNestedLayoutSectionItemAndRemoveOriginalSectionIfEmpty } from "../../../../model/commands/layout.js";
import { useDashboardDispatch } from "../../../../model/react/DashboardStoreProvider.js";
export function useMoveWidgetDropHandler(layoutPath) {
    const dispatch = useDashboardDispatch();
    return useCallback((item) => {
        let targetIndex = getItemIndex(layoutPath);
        if (areItemsInSameSection(item.layoutPath, layoutPath)) {
            const currentItemIndex = getItemIndex(item.layoutPath);
            // if the item is moved within the same section to bigger index, indexes are shifted by 1
            if (currentItemIndex < targetIndex) {
                targetIndex = targetIndex - 1;
            }
            if (currentItemIndex === targetIndex) {
                // same positions we do not need to do anything
                return;
            }
        }
        dispatch(moveNestedLayoutSectionItemAndRemoveOriginalSectionIfEmpty(item.layoutPath, updateItemIndex(layoutPath, targetIndex)));
    }, [dispatch, layoutPath]);
}
