import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
// (C) 2021-2026 GoodData Corporation
import { useState } from "react";
import { Separator } from "@gooddata/sdk-ui-kit";
import { useDashboardSelector } from "../../../../../model/react/DashboardStoreProvider.js";
import { selectRenderMode } from "../../../../../model/store/renderMode/renderModeSelectors.js";
import { DashboardInsightEditMenuBubble } from "../../../insightMenu/DefaultDashboardInsightMenu/DashboardInsightMenu/DashboardInsightEditMenuBubble.js";
import { DashboardInsightMenuBubble } from "../../../insightMenu/DefaultDashboardInsightMenu/DashboardInsightMenu/DashboardInsightMenuBubble.js";
import { DashboardInsightMenuItemButton } from "../../../insightMenu/DefaultDashboardInsightMenu/DashboardInsightMenu/DashboardInsightMenuItemButton.js";
import { DashboardInsightSubmenuContainer } from "../../../insightMenu/DefaultDashboardInsightMenu/DashboardInsightMenu/DashboardInsightSubmenuContainer.js";
import { DashboardRichTextMenuContainer } from "./DashboardRichTextMenuContainer.js";
export function DashboardRichTextMenuBody({ items, widget, submenu, setSubmenu, onClose, renderMode, }) {
    const renderSubmenuComponent = submenu ? (_jsx(submenu.SubmenuComponent, { widget: widget, onClose: onClose, onGoBack: () => setSubmenu(null) })) : null;
    return submenu ? (submenu.renderSubmenuComponentOnly ? (renderSubmenuComponent) : (_jsx(DashboardInsightSubmenuContainer, { onClose: onClose, title: submenu.itemName, onBack: () => setSubmenu(null), children: renderSubmenuComponent }))) : (_jsx(DashboardRichTextMenuContainer, { onClose: onClose, widget: widget, renderMode: renderMode, children: _jsx(DashboardRichTextMenuRoot, { items: items, setSubmenu: setSubmenu }) }));
}
export function DashboardRichTextMenu(props) {
    const { widget, onClose } = props;
    const renderMode = useDashboardSelector(selectRenderMode);
    const [submenu, setSubmenu] = useState(null);
    return renderMode === "edit" ? (_jsx(DashboardInsightEditMenuBubble, { onClose: onClose, isSubmenu: !!submenu, children: _jsx(DashboardRichTextMenuBody, { ...props, submenu: submenu, setSubmenu: setSubmenu, renderMode: renderMode }) })) : (_jsx(DashboardInsightMenuBubble, { onClose: onClose, widget: widget, isSubmenu: !!submenu, children: _jsx(DashboardRichTextMenuBody, { ...props, submenu: submenu, setSubmenu: setSubmenu, renderMode: renderMode }) }));
}
function DashboardRichTextMenuRoot({ items, setSubmenu }) {
    return (_jsx(_Fragment, { children: items.map((item) => {
            if (item.type === "separator") {
                return _jsx(Separator, {}, item.itemId);
            }
            if (item.type === "submenu") {
                return (_jsx(DashboardInsightMenuItemButton, { ...item, onClick: (event) => {
                        if (item.onClick) {
                            item.onClick(event);
                        }
                        setSubmenu(item);
                    }, submenu: true }, item.itemId));
            }
            return _jsx(DashboardInsightMenuItemButton, { ...item }, item.itemId);
        }) }));
}
