"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__, {
    ConnectedHandlesProvider: ()=>ConnectedHandlesProvider,
    useConnectedHandles: ()=>useConnectedHandles
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const EMPTY_SET = Object.freeze(new Set());
class ConnectedHandlesStore {
    map = new Map();
    listeners = new Map();
    subscribe(nodeId, listener) {
        let nodeListeners = this.listeners.get(nodeId);
        if (!nodeListeners) {
            nodeListeners = new Set();
            this.listeners.set(nodeId, nodeListeners);
        }
        nodeListeners.add(listener);
        return ()=>{
            nodeListeners.delete(listener);
            if (0 === nodeListeners.size) this.listeners.delete(nodeId);
        };
    }
    getSnapshot(nodeId) {
        return this.map.get(nodeId) ?? EMPTY_SET;
    }
    update(edges) {
        const nextMap = buildConnectedHandlesMap(edges);
        const prevMap = this.map;
        const finalMap = new Map();
        const changedNodeIds = new Set();
        for (const [nodeId, nextSet] of nextMap){
            const prevSet = prevMap.get(nodeId);
            if (setsEqual(prevSet, nextSet)) finalMap.set(nodeId, prevSet);
            else {
                finalMap.set(nodeId, nextSet);
                changedNodeIds.add(nodeId);
            }
        }
        for (const nodeId of prevMap.keys())if (!nextMap.has(nodeId)) changedNodeIds.add(nodeId);
        if (0 === changedNodeIds.size) return;
        this.map = finalMap;
        for (const nodeId of changedNodeIds){
            const nodeListeners = this.listeners.get(nodeId);
            if (nodeListeners) for (const listener of nodeListeners)listener();
        }
    }
}
function setsEqual(a, b) {
    if (a === b) return true;
    if (!a || !b) return false;
    if (a.size !== b.size) return false;
    for (const value of a)if (!b.has(value)) return false;
    return true;
}
function buildConnectedHandlesMap(edges) {
    const map = new Map();
    for (const edge of edges){
        if (edge.sourceHandle) {
            let set = map.get(edge.source);
            if (!set) {
                set = new Set();
                map.set(edge.source, set);
            }
            set.add(edge.sourceHandle);
        }
        if (edge.targetHandle) {
            let set = map.get(edge.target);
            if (!set) {
                set = new Set();
                map.set(edge.target, set);
            }
            set.add(edge.targetHandle);
        }
    }
    return map;
}
const ConnectedHandlesContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
function ConnectedHandlesProvider({ edges, children }) {
    const storeRef = (0, external_react_namespaceObject.useRef)(void 0);
    if (!storeRef.current) storeRef.current = new ConnectedHandlesStore();
    (0, external_react_namespaceObject.useEffect)(()=>{
        storeRef.current.update(edges);
    }, [
        edges
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ConnectedHandlesContext.Provider, {
        value: storeRef.current,
        children: children
    });
}
function useConnectedHandles(nodeId) {
    const store = (0, external_react_namespaceObject.useContext)(ConnectedHandlesContext);
    const subscribe = (0, external_react_namespaceObject.useCallback)((onStoreChange)=>{
        if (!store) return ()=>{};
        return store.subscribe(nodeId, onStoreChange);
    }, [
        store,
        nodeId
    ]);
    const getSnapshot = (0, external_react_namespaceObject.useCallback)(()=>store?.getSnapshot(nodeId) ?? EMPTY_SET, [
        store,
        nodeId
    ]);
    return (0, external_react_namespaceObject.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
}
exports.ConnectedHandlesProvider = __webpack_exports__.ConnectedHandlesProvider;
exports.useConnectedHandles = __webpack_exports__.useConnectedHandles;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ConnectedHandlesProvider",
    "useConnectedHandles"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
