import { Fragment, jsx } from "react/jsx-runtime";
import { useNodesData } from "@xyflow/react";
import { useMemo } from "react";
import { resolveHandles } from "../../utils/manifest-resolver.js";
import { useConnectedHandles } from "../BaseCanvas/ConnectedHandlesContext.js";
import { ButtonHandles } from "./index.js";
const useButtonHandles = ({ handleConfigurations, shouldShowHandles, handleAction, handleMouseEnter, handleMouseLeave, nodeId, selected, hovered, showAddButton, showNotches, shouldShowAddButtonFn, nodeWidth, nodeHeight, portalActions })=>{
    const connectedHandleIds = useConnectedHandles(nodeId);
    const node = useNodesData(nodeId);
    const handleElements = useMemo(()=>{
        if (!handleConfigurations || !Array.isArray(handleConfigurations) || 0 === handleConfigurations.length) return /*#__PURE__*/ jsx(Fragment, {});
        const resolvedHandles = resolveHandles(handleConfigurations, node?.data ?? {});
        const elements = resolvedHandles.map((config, i)=>{
            const groupVisible = shouldShowHandles && (config.visible ?? true);
            const enhancedHandles = config.handles.map((handle)=>({
                    ...handle,
                    showHandle: connectedHandleIds.has(handle.id) || groupVisible,
                    onAction: handle.onAction || handleAction,
                    onMouseEnter: handle.onMouseEnter || handleMouseEnter,
                    onMouseLeave: handle.onMouseLeave || handleMouseLeave
                }));
            return /*#__PURE__*/ jsx(ButtonHandles, {
                nodeId: nodeId,
                handles: enhancedHandles,
                position: config.position,
                selected: selected,
                hovered: hovered,
                showAddButton: showAddButton,
                showNotches: showNotches,
                customPositionAndOffsets: config.customPositionAndOffsets,
                shouldShowAddButtonFn: shouldShowAddButtonFn,
                nodeWidth: nodeWidth,
                nodeHeight: nodeHeight,
                portalActions: portalActions
            }, `${i}:${config.position}:${config.handles.map((h)=>h.id).join(',')}`);
        });
        return elements;
    }, [
        handleConfigurations,
        selected,
        hovered,
        shouldShowHandles,
        connectedHandleIds,
        handleAction,
        handleMouseEnter,
        handleMouseLeave,
        nodeId,
        showAddButton,
        showNotches,
        shouldShowAddButtonFn,
        nodeWidth,
        nodeHeight,
        portalActions,
        node?.data
    ]);
    return handleElements;
};
export { useButtonHandles };
