"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__, {
    HandleHoverBridge: ()=>HandleHoverBridge,
    HandleButton: ()=>HandleButton
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const index_cjs_namespaceObject = require("../../layouts/index.cjs");
const react_cjs_namespaceObject = require("../../xyflow/react.cjs");
const external_react_namespaceObject = require("react");
const CssUtil_cjs_namespaceObject = require("../../utils/CssUtil.cjs");
const external_NodeViewportOverlay_cjs_namespaceObject = require("../NodeViewportOverlay.cjs");
const external_CanvasInlineButton_cjs_namespaceObject = require("./CanvasInlineButton.cjs");
const external_HandleLabel_cjs_namespaceObject = require("./HandleLabel.cjs");
const BUTTON_POSITION = {
    [react_cjs_namespaceObject.Position.Top]: 'flex-col-reverse bottom-full left-1/2 -translate-x-1/2',
    [react_cjs_namespaceObject.Position.Bottom]: 'flex-col top-full left-1/2 -translate-x-1/2',
    [react_cjs_namespaceObject.Position.Left]: 'flex-row-reverse right-full top-1/2 -translate-y-1/2',
    [react_cjs_namespaceObject.Position.Right]: 'flex-row left-full top-1/2 -translate-y-1/2'
};
const DRAG_THRESHOLD = 5;
const HandleButton = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(({ visible, labelVisible, keepButtonMounted = false, position, onAction, onMouseEnter, onMouseLeave, handleRef, label, labelIcon, labelBackgroundColor, portal })=>{
    const didDragRef = (0, external_react_namespaceObject.useRef)(false);
    const teardownRef = (0, external_react_namespaceObject.useRef)(null);
    (0, external_react_namespaceObject.useEffect)(()=>()=>teardownRef.current?.(), []);
    const handleClick = (0, external_react_namespaceObject.useCallback)((e)=>{
        e.stopPropagation();
        if (didDragRef.current) return;
        onAction(e);
    }, [
        onAction
    ]);
    const handlePointerDown = (0, external_react_namespaceObject.useCallback)((e)=>{
        teardownRef.current?.();
        didDragRef.current = false;
        const startX = e.clientX;
        const startY = e.clientY;
        const onMove = (moveEvt)=>{
            if (didDragRef.current) return;
            const dx = moveEvt.clientX - startX;
            const dy = moveEvt.clientY - startY;
            if (dx * dx + dy * dy < DRAG_THRESHOLD * DRAG_THRESHOLD) return;
            didDragRef.current = true;
            const handleEl = handleRef?.current;
            if (handleEl) {
                const rect = handleEl.getBoundingClientRect();
                handleEl.dispatchEvent(new MouseEvent('mousedown', {
                    bubbles: true,
                    cancelable: true,
                    clientX: rect.left + rect.width / 2,
                    clientY: rect.top + rect.height / 2
                }));
                handleEl.dispatchEvent(new MouseEvent('mouseout', {
                    bubbles: true,
                    relatedTarget: document.body
                }));
            }
        };
        const cleanup = ()=>{
            document.removeEventListener('pointermove', onMove);
            document.removeEventListener('pointerup', cleanup);
            document.removeEventListener('pointercancel', cleanup);
            teardownRef.current = null;
        };
        document.addEventListener('pointermove', onMove);
        document.addEventListener('pointerup', cleanup);
        document.addEventListener('pointercancel', cleanup);
        teardownRef.current = cleanup;
    }, []);
    const addButton = keepButtonMounted ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CanvasInlineButton_cjs_namespaceObject.CanvasInlineButton, {
        "aria-label": "Add node",
        "aria-hidden": visible ? void 0 : true,
        disabled: visible ? void 0 : true,
        onClick: handleClick,
        onPointerDown: handlePointerDown,
        onMouseEnter: onMouseEnter,
        onMouseLeave: onMouseLeave,
        className: (0, CssUtil_cjs_namespaceObject.cx)('nodrag nopan transition-opacity duration-250', visible ? 'pointer-events-auto opacity-100' : 'pointer-events-none opacity-0 disabled:opacity-0')
    }) : visible && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CanvasInlineButton_cjs_namespaceObject.CanvasInlineButton, {
        "aria-label": "Add node",
        onClick: handleClick,
        onPointerDown: handlePointerDown,
        onMouseEnter: onMouseEnter,
        onMouseLeave: onMouseLeave,
        className: "nodrag nopan pointer-events-auto animate-fade-in"
    });
    const content = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        className: (0, CssUtil_cjs_namespaceObject.cx)('absolute flex items-center pointer-events-none', BUTTON_POSITION[position]),
        children: [
            addButton,
            label && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(InlineLabel, {
                label: label,
                labelIcon: labelIcon,
                backgroundColor: labelBackgroundColor,
                visible: labelVisible
            })
        ]
    });
    const shouldRenderPortal = Boolean(keepButtonMounted || visible || label && labelVisible);
    if (portal) {
        if (!shouldRenderPortal) return null;
        const { nodeId, ...anchor } = portal;
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_NodeViewportOverlay_cjs_namespaceObject.NodeViewportOverlay, {
            nodeId: nodeId,
            anchor: anchor,
            layer: "nodeHandleAffordance",
            children: content
        });
    }
    return content;
});
const InlineLabel = ({ label, labelIcon, backgroundColor, visible = true })=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        "aria-hidden": visible ? void 0 : true,
        className: (0, CssUtil_cjs_namespaceObject.cx)('px-1.5 py-0.5 rounded-sm whitespace-nowrap select-none transition-opacity duration-250 z-10', visible ? 'opacity-100' : 'opacity-0'),
        style: backgroundColor ? {
            backgroundColor
        } : void 0,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(index_cjs_namespaceObject.Row, {
            align: "center",
            gap: 4,
            children: [
                labelIcon,
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                    className: "text-xs font-bold text-foreground-muted",
                    style: backgroundColor ? void 0 : external_HandleLabel_cjs_namespaceObject.LABEL_SHADOW_STYLE,
                    children: label
                })
            ]
        })
    });
const BRIDGE_BASE = 'absolute pointer-events-auto cursor-default';
const BRIDGE_POSITION = {
    [react_cjs_namespaceObject.Position.Top]: 'bottom-full left-0 w-full',
    [react_cjs_namespaceObject.Position.Bottom]: 'top-full left-0 w-full',
    [react_cjs_namespaceObject.Position.Left]: 'right-full top-0 h-full',
    [react_cjs_namespaceObject.Position.Right]: 'left-full top-0 h-full'
};
const HandleHoverBridge = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(({ position, visible })=>{
    if (!visible) return null;
    const isVertical = position === react_cjs_namespaceObject.Position.Top || position === react_cjs_namespaceObject.Position.Bottom;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: (0, CssUtil_cjs_namespaceObject.cx)(BRIDGE_BASE, BRIDGE_POSITION[position], isVertical ? 'h-15' : 'w-15')
    });
});
exports.HandleButton = __webpack_exports__.HandleButton;
exports.HandleHoverBridge = __webpack_exports__.HandleHoverBridge;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "HandleButton",
    "HandleHoverBridge"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
