import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
// (C) 2007-2026 GoodData Corporation
import { useMemo } from "react";
import { serializeLayoutItemPath } from "../../../_staging/layout/coordinates.js";
import { useScreenSize } from "../../dashboard/components/DashboardScreenSizeContext.js";
import { useIsDraggingWidget } from "../../dragAndDrop/draggableWidget/useIsDraggingWidget.js";
import { HeightResizerHotspot } from "../dragAndDrop/Resize/HeightResizerHotspot.js";
import { DashboardLayoutItem } from "./DashboardLayoutItem.js";
const defaultItemKeyGetter = ({ item }) => serializeLayoutItemPath(item.index());
export function DashboardLayoutGridRowEdit({ section, itemKeyGetter = defaultItemKeyGetter, gridRowRenderer, itemRenderer, widgetRenderer, getLayoutDimensions, items, renderMode, itemsInRowsByIndex, }) {
    const screen = useScreenSize();
    const isDraggingWidget = useIsDraggingWidget();
    const rowItems = useMemo(() => itemsInRowsByIndex.flatMap(([, itemsInRow], rowIndex) => itemsInRow.map((item) => (_jsx(DashboardLayoutItem, { item: item, renderMode: renderMode, itemRenderer: itemRenderer, widgetRenderer: widgetRenderer, rowIndex: rowIndex }, itemKeyGetter({ item, screen }))))), [itemKeyGetter, itemRenderer, itemsInRowsByIndex, renderMode, screen, widgetRenderer]);
    const extendedRows = useMemo(() => isDraggingWidget
        ? rowItems
        : [...itemsInRowsByIndex].reverse().reduce((acc, [index, itemsInRow]) => {
            return splice(acc, index + 1, 0, _jsx(HeightResizerHotspot, { getLayoutDimensions: getLayoutDimensions, 
                // TWidget to ExtendedDashboardWidget (originally unknown)
                section: section, items: itemsInRow }, `HeightResizerHotspot-${index}`));
        }, rowItems), [isDraggingWidget, rowItems, itemsInRowsByIndex, getLayoutDimensions, section]);
    return (_jsx(_Fragment, { children: gridRowRenderer
            ? gridRowRenderer({
                children: extendedRows,
                section,
                items,
                renderMode,
            })
            : extendedRows }));
}
function splice(arr, start, deleteCount, ...addItem) {
    const result = [];
    if (start > 0) {
        result.push(...arr.slice(0, start));
    }
    result.push(...addItem);
    const len = result.length - addItem.length;
    const count = deleteCount <= 0 ? len : len + deleteCount;
    if (arr[count]) {
        result.push(...arr.slice(count));
    }
    return result;
}
