"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    ButtonHandles: ()=>ButtonHandles,
    ButtonHandle: ()=>ButtonHandle
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_cjs_namespaceObject = require("../../xyflow/react.cjs");
const external_react_namespaceObject = require("react");
const CanvasEventBus_cjs_namespaceObject = require("../../utils/CanvasEventBus.cjs");
const CssUtil_cjs_namespaceObject = require("../../utils/CssUtil.cjs");
const handle_positioning_cjs_namespaceObject = require("../../utils/handle-positioning.cjs");
const external_ButtonHandleLayoutUtils_cjs_namespaceObject = require("./ButtonHandleLayoutUtils.cjs");
const external_ButtonHandleStyleUtils_cjs_namespaceObject = require("./ButtonHandleStyleUtils.cjs");
const external_HandleButton_cjs_namespaceObject = require("./HandleButton.cjs");
const external_HandleLabel_cjs_namespaceObject = require("./HandleLabel.cjs");
const external_HandleNotch_cjs_namespaceObject = require("./HandleNotch.cjs");
const external_useButtonHandleSizeAndPosition_cjs_namespaceObject = require("./useButtonHandleSizeAndPosition.cjs");
const ButtonHandleBase = ({ id, nodeId, type, position, connectionPosition = position, handleType, label, labelIcon, labelBackgroundColor, visible = true, labelVisible, labelVisibility, showButton = true, selected = false, index = 0, total = 1, onAction, onMouseEnter, onMouseLeave, showNotches = true, customPositionAndOffsets, nodeWidth, nodeHeight, portalAction = false })=>{
    const handleRef = (0, external_react_namespaceObject.useRef)(null);
    const [isHovered, setIsHovered] = (0, external_react_namespaceObject.useState)(false);
    const isVertical = position === react_cjs_namespaceObject.Position.Top || position === react_cjs_namespaceObject.Position.Bottom;
    const dispatchMouseEvent = (0, external_react_namespaceObject.useCallback)((eventName, handler)=>{
        const payload = {
            handleId: id,
            nodeId,
            handleType,
            position: connectionPosition
        };
        handler?.(payload);
        CanvasEventBus_cjs_namespaceObject.canvasEventBus.emit(eventName, payload);
    }, [
        id,
        nodeId,
        handleType,
        connectionPosition
    ]);
    const handleButtonMouseEnter = (0, external_react_namespaceObject.useCallback)(()=>dispatchMouseEvent('handle:mouseenter', onMouseEnter), [
        dispatchMouseEvent,
        onMouseEnter
    ]);
    const handleButtonMouseLeave = (0, external_react_namespaceObject.useCallback)(()=>dispatchMouseEvent('handle:mouseleave', onMouseLeave), [
        dispatchMouseEvent,
        onMouseLeave
    ]);
    const positionPercent = (0, external_react_namespaceObject.useMemo)(()=>{
        const relevantSize = isVertical ? nodeWidth : nodeHeight;
        if (relevantSize && relevantSize > 0) {
            const gridPositions = (0, handle_positioning_cjs_namespaceObject.calculateGridAlignedHandlePositions)(relevantSize, total);
            const pixelPosition = gridPositions[index] ?? relevantSize / 2;
            return (0, external_ButtonHandleStyleUtils_cjs_namespaceObject.pixelToPercent)(pixelPosition, relevantSize);
        }
        return (index + 1) / (total + 1) * 100;
    }, [
        index,
        total,
        isVertical,
        nodeWidth,
        nodeHeight
    ]);
    const handleButtonClick = (0, external_react_namespaceObject.useCallback)((event)=>{
        event.stopPropagation();
        const actionEvent = {
            handleId: id,
            nodeId,
            handleType,
            position: connectionPosition,
            originalEvent: event
        };
        onAction?.(actionEvent);
        CanvasEventBus_cjs_namespaceObject.canvasEventBus.emit('handle:action', {
            handleId: id,
            nodeId,
            handleType,
            position: connectionPosition
        });
    }, [
        connectionPosition,
        id,
        nodeId,
        handleType,
        onAction
    ]);
    const markAsHovered = (0, external_react_namespaceObject.useCallback)(()=>setIsHovered(true), []);
    const unmarkAsHovered = (0, external_react_namespaceObject.useCallback)(()=>setIsHovered(false), []);
    const showActionButton = !!onAction && 'source' === type;
    const resolvedLabelVisible = labelVisible ?? visible;
    const keepButtonMounted = 'hover' === labelVisibility;
    const { width: handleWidth, height: handleHeight, top, bottom, left, right, transform } = (0, external_useButtonHandleSizeAndPosition_cjs_namespaceObject.useButtonHandleSizeAndPosition)({
        position,
        positionPercent,
        numHandles: total,
        customPositionAndOffsets
    });
    if (connectionPosition !== position) {
        const layout = (0, external_ButtonHandleLayoutUtils_cjs_namespaceObject.getInwardHandleLayout)(position, handleType);
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
            className: (0, CssUtil_cjs_namespaceObject.cx)('absolute flex overflow-visible', visible ? 'pointer-events-auto opacity-100' : 'pointer-events-none opacity-0'),
            style: {
                top,
                bottom,
                left,
                right,
                transform: layout.rootTransform
            },
            onMouseEnter: markAsHovered,
            onMouseLeave: unmarkAsHovered,
            onMouseDown: unmarkAsHovered,
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(InwardHandleContent, {
                    handleType: handleType,
                    isVertical: isVertical,
                    selected: selected,
                    hovered: isHovered,
                    showNotch: showNotches,
                    label: label,
                    labelIcon: labelIcon,
                    labelBackgroundColor: labelBackgroundColor,
                    labelVisible: resolvedLabelVisible,
                    layout: layout
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(react_cjs_namespaceObject.Handle, {
                    ref: handleRef,
                    type: type,
                    position: connectionPosition,
                    id: id,
                    onMouseEnter: markAsHovered,
                    onMouseLeave: unmarkAsHovered,
                    onMouseDown: unmarkAsHovered,
                    className: (0, CssUtil_cjs_namespaceObject.cx)('absolute! z-20 flex! items-center! justify-center! overflow-visible! border-0! rounded-none! bg-transparent!', visible ? 'cursor-crosshair! pointer-events-auto! opacity-100' : 'cursor-default! pointer-events-none! opacity-0'),
                    style: layout.anchorStyle
                }),
                showActionButton ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_HandleButton_cjs_namespaceObject.HandleButton, {
                    visible: showButton,
                    labelVisible: resolvedLabelVisible,
                    keepButtonMounted: keepButtonMounted,
                    position: connectionPosition,
                    onAction: handleButtonClick,
                    onMouseEnter: handleButtonMouseEnter,
                    onMouseLeave: handleButtonMouseLeave,
                    handleRef: handleRef
                }) : null
            ]
        });
    }
    const portal = showActionButton && portalAction && !customPositionAndOffsets ? (0, external_ButtonHandleLayoutUtils_cjs_namespaceObject.getHandleActionPortal)({
        nodeId,
        position,
        positionPercent,
        total,
        nodeWidth,
        nodeHeight
    }) : void 0;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(react_cjs_namespaceObject.Handle, {
        ref: handleRef,
        type: type,
        position: position,
        id: id,
        onMouseEnter: markAsHovered,
        onMouseLeave: unmarkAsHovered,
        onMouseDown: unmarkAsHovered,
        className: (0, CssUtil_cjs_namespaceObject.cx)('flex! items-center! justify-center! border-0! rounded-none! bg-transparent!', visible ? 'cursor-crosshair! pointer-events-auto! opacity-100' : 'cursor-default! pointer-events-none! opacity-0'),
        style: {
            width: handleWidth,
            height: handleHeight,
            top,
            bottom,
            left,
            right,
            transform
        },
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_HandleNotch_cjs_namespaceObject.HandleNotch, {
                handleType: handleType,
                isVertical: isVertical,
                selected: selected,
                hovered: isHovered,
                showNotch: showNotches
            }),
            showActionButton ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_HandleButton_cjs_namespaceObject.HandleButton, {
                visible: showButton,
                labelVisible: resolvedLabelVisible,
                keepButtonMounted: keepButtonMounted,
                position: position,
                onAction: handleButtonClick,
                onMouseEnter: handleButtonMouseEnter,
                onMouseLeave: handleButtonMouseLeave,
                handleRef: handleRef,
                label: label,
                labelIcon: labelIcon,
                labelBackgroundColor: labelBackgroundColor,
                portal: portal
            }) : label && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_HandleLabel_cjs_namespaceObject.HandleLabel, {
                position: position,
                backgroundColor: labelBackgroundColor,
                label: label,
                labelIcon: labelIcon,
                visible: resolvedLabelVisible
            })
        ]
    });
};
const ButtonHandle = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(ButtonHandleBase);
function InwardHandleContent({ handleType, isVertical, selected, hovered, showNotch, label, labelIcon, labelBackgroundColor, labelVisible = true, layout }) {
    const labelElement = label ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        "aria-hidden": labelVisible ? void 0 : true,
        className: (0, CssUtil_cjs_namespaceObject.cx)('pointer-events-none flex items-center gap-1.5 whitespace-nowrap rounded-full border border-border bg-surface px-2 py-0.5', 'text-xs font-medium leading-4 text-foreground-muted transition-opacity duration-250', labelVisible ? 'opacity-100' : 'opacity-0'),
        style: labelBackgroundColor ? {
            backgroundColor: labelBackgroundColor
        } : void 0,
        children: [
            labelIcon,
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                children: label
            })
        ]
    }) : null;
    const notchElement = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
        className: "relative z-10 flex shrink-0",
        style: layout.notchStyle,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_HandleNotch_cjs_namespaceObject.HandleNotch, {
            handleType: handleType,
            isVertical: isVertical,
            selected: selected,
            hovered: hovered,
            showNotch: showNotch
        })
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        className: (0, CssUtil_cjs_namespaceObject.cx)('flex items-center', layout.contentDirectionClassName),
        children: [
            labelElement,
            notchElement
        ]
    });
}
const ButtonHandlesBase = ({ nodeId, handles, position, connectionPosition = position, selected = false, hovered = false, visible = true, showAddButton = true, showNotches = true, customPositionAndOffsets, shouldShowAddButtonFn = ({ showAddButton, selected, hovered })=>showAddButton && (selected || hovered), nodeWidth, nodeHeight, portalActions = false })=>{
    const finalSelected = shouldShowAddButtonFn({
        showAddButton,
        selected,
        hovered
    });
    const visibleHandles = handles.filter((h)=>h.visible ?? true);
    const hasSourceButtons = visibleHandles.some((h)=>'source' === h.type && h.onAction);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_HandleButton_cjs_namespaceObject.HandleHoverBridge, {
                position: connectionPosition,
                visible: hasSourceButtons && finalSelected
            }),
            visibleHandles.map((handle, index)=>{
                const handleVisible = handle.showHandle ?? visible;
                const labelVisible = handleVisible && ('hover' !== handle.labelVisibility || selected || hovered);
                return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ButtonHandle, {
                    id: handle.id,
                    nodeId: nodeId,
                    type: handle.type,
                    position: position,
                    connectionPosition: connectionPosition,
                    handleType: handle.handleType,
                    label: handle.label,
                    labelIcon: handle.labelIcon,
                    labelBackgroundColor: handle.labelBackgroundColor,
                    labelVisibility: handle.labelVisibility,
                    index: index,
                    total: visibleHandles.length,
                    selected: selected,
                    visible: handleVisible,
                    labelVisible: labelVisible,
                    showButton: finalSelected && handleVisible && handle.showButton,
                    onAction: handle.onAction,
                    onMouseEnter: handle.onMouseEnter,
                    onMouseLeave: handle.onMouseLeave,
                    showNotches: showNotches,
                    customPositionAndOffsets: customPositionAndOffsets,
                    nodeWidth: nodeWidth,
                    nodeHeight: nodeHeight,
                    portalAction: portalActions && 'source' === handle.type
                }, handle.id);
            })
        ]
    });
};
const ButtonHandles = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(ButtonHandlesBase);
exports.ButtonHandle = __webpack_exports__.ButtonHandle;
exports.ButtonHandles = __webpack_exports__.ButtonHandles;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ButtonHandle",
    "ButtonHandles"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
