"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__, {
    useNodeCollapse: ()=>useNodeCollapse
});
const react_cjs_namespaceObject = require("../../xyflow/react.cjs");
const external_react_namespaceObject = require("react");
function useNodeCollapse({ nodeId, collapseConfig }) {
    const { setNodes, setEdges } = (0, react_cjs_namespaceObject.useReactFlow)();
    const isCollapseEnabled = collapseConfig?.enabled ?? false;
    const isCollapsed = collapseConfig?.collapsed ?? false;
    const handleIds = collapseConfig?.handleIds ?? [];
    const connectedNodeIds = (0, react_cjs_namespaceObject.useStore)((state)=>{
        if (!isCollapseEnabled || 0 === handleIds.length) return new Set();
        const connectedIds = new Set();
        state.edges.forEach((edge)=>{
            if (edge.source === nodeId && handleIds.includes(edge.sourceHandle ?? '')) connectedIds.add(edge.target);
            if (edge.target === nodeId && handleIds.includes(edge.targetHandle ?? '')) connectedIds.add(edge.source);
        });
        return connectedIds;
    }, (a, b)=>a.size === b.size && [
            ...a
        ].every((id)=>b.has(id)));
    const hasCollapsibleConnections = connectedNodeIds.size > 0;
    const collapse = (0, external_react_namespaceObject.useCallback)(()=>{
        if (!isCollapseEnabled || 0 === connectedNodeIds.size) return;
        setNodes((nodes)=>nodes.map((node)=>connectedNodeIds.has(node.id) ? {
                    ...node,
                    hidden: true
                } : node));
        setEdges((edges)=>edges.map((edge)=>connectedNodeIds.has(edge.source) || connectedNodeIds.has(edge.target) ? {
                    ...edge,
                    hidden: true
                } : edge));
        collapseConfig?.onCollapseChange?.(true);
    }, [
        isCollapseEnabled,
        connectedNodeIds,
        setNodes,
        setEdges,
        collapseConfig
    ]);
    const expand = (0, external_react_namespaceObject.useCallback)(()=>{
        if (!isCollapseEnabled || 0 === connectedNodeIds.size) return;
        setNodes((nodes)=>nodes.map((node)=>connectedNodeIds.has(node.id) ? {
                    ...node,
                    hidden: false
                } : node));
        setEdges((edges)=>edges.map((edge)=>connectedNodeIds.has(edge.source) || connectedNodeIds.has(edge.target) ? {
                    ...edge,
                    hidden: false
                } : edge));
        collapseConfig?.onCollapseChange?.(false);
    }, [
        isCollapseEnabled,
        connectedNodeIds,
        setNodes,
        setEdges,
        collapseConfig
    ]);
    const toggle = (0, external_react_namespaceObject.useCallback)(()=>{
        if (isCollapsed) expand();
        else collapse();
    }, [
        isCollapsed,
        expand,
        collapse
    ]);
    return {
        isCollapsed,
        isCollapseEnabled,
        hasCollapsibleConnections,
        expand,
        collapse,
        toggle
    };
}
exports.useNodeCollapse = __webpack_exports__.useNodeCollapse;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "useNodeCollapse"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
