"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__, {
    useNodeSelection: ()=>useNodeSelection,
    useSetNodeSelection: ()=>useSetNodeSelection
});
const react_cjs_namespaceObject = require("../../../xyflow/react.cjs");
const external_react_namespaceObject = require("react");
function applySelection(nodes, nodeId) {
    return nodes.map((node)=>{
        const shouldBeSelected = node.id === nodeId;
        if (node.selected === shouldBeSelected) return node;
        return {
            ...node,
            selected: shouldBeSelected
        };
    });
}
function useNodeSelection(nodeId, maintainSelection = true) {
    const nodes = (0, react_cjs_namespaceObject.useNodes)();
    const { setNodes } = (0, react_cjs_namespaceObject.useReactFlow)();
    const [selectedNodeId, setSelectedNodeId] = (0, external_react_namespaceObject.useState)(nodeId || null);
    (0, react_cjs_namespaceObject.useOnSelectionChange)({
        onChange: (0, external_react_namespaceObject.useCallback)(({ nodes: selectedNodes })=>{
            if (!nodeId) 1 === selectedNodes.length && selectedNodes[0] ? setSelectedNodeId(selectedNodes[0].id) : setSelectedNodeId(null);
        }, [
            nodeId
        ])
    });
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (maintainSelection && selectedNodeId) setNodes((nds)=>applySelection(nds, selectedNodeId));
    }, [
        selectedNodeId,
        maintainSelection,
        setNodes
    ]);
    const selectedNode = nodes.find((node)=>node.id === selectedNodeId);
    return {
        selectedNodeId,
        setSelectedNodeId,
        selectedNode
    };
}
function useSetNodeSelection() {
    const { setNodes } = (0, react_cjs_namespaceObject.useReactFlow)();
    const setSelectedNodeId = (0, external_react_namespaceObject.useCallback)((nodeId)=>{
        setNodes((nds)=>applySelection(nds, nodeId));
    }, [
        setNodes
    ]);
    return {
        setSelectedNodeId
    };
}
exports.useNodeSelection = __webpack_exports__.useNodeSelection;
exports.useSetNodeSelection = __webpack_exports__.useSetNodeSelection;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "useNodeSelection",
    "useSetNodeSelection"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
