import { jsx as _jsx } from "react/jsx-runtime";
import { SquarePlus } from "lucide-react";
import { Spinner } from "./ui/Spinner";
export async function showItemContextMenu({ editContext, event, itemIds, language, version = 0, openInNewSlotItem, commandCallback, }) {
    if (!editContext || itemIds.length === 0)
        return;
    event.preventDefault();
    event.stopPropagation();
    const { clientX, clientY, shiftKey, altKey, ctrlKey } = event;
    const syntheticEvent = new MouseEvent("contextmenu", {
        bubbles: true,
        cancelable: true,
        view: window,
        clientX,
        clientY,
        shiftKey,
        altKey,
        ctrlKey,
    });
    let loadingShown = false;
    const loadingTimer = setTimeout(() => {
        loadingShown = true;
        editContext.showContextMenu(syntheticEvent, [
            {
                id: "loading",
                label: "Loading…",
                disabled: true,
                icon: _jsx(Spinner, { size: "sm", className: "mr-2" }),
            },
        ]);
    }, 100);
    const items = await editContext.itemsRepository.getItems(itemIds.map((id) => ({ id, language, version })));
    clearTimeout(loadingTimer);
    const openInNewSlotMenuItems = openInNewSlotItem && !editContext.isMobile
        ? [
            {
                id: "open-in-new-slot",
                label: "Open in new slot",
                icon: _jsx(SquarePlus, { className: "h-4 w-4" }),
                command: async () => {
                    await editContext.loadItem(openInNewSlotItem, {
                        openInNewSlot: true,
                    });
                },
            },
            { id: "sep-open-in-new-slot", separator: true },
        ]
        : [];
    if (!items || items.length === 0) {
        const menuItems = [
            ...openInNewSlotMenuItems,
            { id: "empty", label: "No actions available", disabled: true },
        ];
        if (loadingShown)
            editContext.updateContextMenu(menuItems);
        else
            editContext.showContextMenu(syntheticEvent, menuItems);
        return;
    }
    const menuItems = await editContext.configuration.editor.contentTree.contextMenu.factory({
        items,
        editContext,
        commandCallback,
    });
    if (!menuItems || menuItems.length === 0) {
        const emptyMenuItems = [
            ...openInNewSlotMenuItems,
            { id: "empty", label: "No actions available", disabled: true },
        ];
        if (loadingShown)
            editContext.updateContextMenu(emptyMenuItems);
        return;
    }
    const finalMenuItems = [...openInNewSlotMenuItems, ...menuItems];
    if (loadingShown)
        editContext.updateContextMenu(finalMenuItems);
    else
        editContext.showContextMenu(syntheticEvent, finalMenuItems);
}
//# sourceMappingURL=itemContextMenu.js.map