import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// (C) 2007-2026 GoodData Corporation
import { useMemo } from "react";
import { getItemIndex, serializeLayoutItemPath } from "../../../_staging/layout/coordinates.js";
import { useScreenSize } from "../../dashboard/components/DashboardScreenSizeContext.js";
import { useSlideSizeStyle } from "../../dashboardContexts/useSlideData.js";
import { useSectionExportData } from "../../export/useExportData.js";
import { DashboardLayoutSectionOverlayController } from "../DashboardItemOverlay/DashboardItemOverlayController.js";
import { DashboardLayoutGridRow } from "./DashboardLayoutGridRow.js";
import { DashboardLayoutGridRowEdit } from "./DashboardLayoutGridRowEdit.js";
import { DashboardLayoutSectionHeaderRenderer } from "./DashboardLayoutSectionHeaderRenderer.js";
import { DashboardLayoutSectionRenderer } from "./DashboardLayoutSectionRenderer.js";
const defaultSectionRenderer = (props) => (_jsx(DashboardLayoutSectionRenderer, { ...props }));
const defaultHeaderRenderer = (props) => (_jsx(DashboardLayoutSectionHeaderRenderer, { ...props }));
const defaultItemKeyGetter = ({ item }) => serializeLayoutItemPath(item.index());
export function DashboardLayoutSection({ section, sectionRenderer = defaultSectionRenderer, sectionHeaderRenderer = defaultHeaderRenderer, itemKeyGetter = defaultItemKeyGetter, gridRowRenderer, itemRenderer, widgetRenderer, getLayoutDimensions, renderMode, parentLayoutItemSize, parentLayoutPath, }) {
    const showBorders = parentLayoutPath === undefined || parentLayoutPath.length === 0;
    const exportData = useSectionExportData(parentLayoutPath?.length ?? 0);
    const exportStyles = useSlideSizeStyle(renderMode, "section", parentLayoutPath);
    const renderProps = {
        section,
        renderMode,
        parentLayoutItemSize,
        parentLayoutPath,
        showBorders,
        exportData: exportData?.section,
    };
    const screen = useScreenSize();
    const items = useMemo(() => {
        if (renderMode === "edit") {
            const itemsInRowsByIndex = section
                .items()
                .asGridRows(screen)
                .map((itemsInRow) => [getItemIndex(itemsInRow.at(-1).index()), itemsInRow]);
            const itemsInRow = section.items().all();
            return (_jsx(DashboardLayoutGridRowEdit, { itemsInRowsByIndex: itemsInRowsByIndex, section: section, items: itemsInRow, gridRowRenderer: gridRowRenderer, itemKeyGetter: itemKeyGetter, itemRenderer: itemRenderer, widgetRenderer: widgetRenderer, renderMode: renderMode, getLayoutDimensions: getLayoutDimensions, 
                // The information is required by the interface but edit row counts the indexes of rows
                // in the inside of the component, unlike view row component.
                rowIndex: -1 }));
        }
        return section
            .items()
            .asGridRows(screen)
            .flatMap((itemsInRow, index) => {
            return (_jsx(DashboardLayoutGridRow, { section: section, items: itemsInRow, gridRowRenderer: gridRowRenderer, itemKeyGetter: itemKeyGetter, itemRenderer: itemRenderer, widgetRenderer: widgetRenderer, renderMode: renderMode, getLayoutDimensions: getLayoutDimensions, rowIndex: index }, index.toString()));
        });
    }, [
        getLayoutDimensions,
        gridRowRenderer,
        itemKeyGetter,
        itemRenderer,
        renderMode,
        section,
        widgetRenderer,
        screen,
    ]);
    return sectionRenderer({
        ...renderProps,
        exportStyles,
        DefaultSectionRenderer: DashboardLayoutSectionRenderer,
        children: (_jsxs(_Fragment, { children: [sectionHeaderRenderer({
                    section,
                    DefaultSectionHeaderRenderer: DashboardLayoutSectionHeaderRenderer,
                    parentLayoutItemSize,
                    parentLayoutPath,
                    exportData,
                }), items, renderMode === "edit" ? _jsx(DashboardLayoutSectionOverlayController, { section: section }) : null] })),
    });
}
