"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__, {
    useAutoLayout: ()=>useAutoLayout,
    useMaintainNodesInView: ()=>useMaintainNodesInView,
    useEnsureNodesInView: ()=>useEnsureNodesInView,
    useFitView: ()=>useFitView
});
const react_cjs_namespaceObject = require("../../xyflow/react.cjs");
const external_react_namespaceObject = require("react");
const external_BaseCanvas_constants_cjs_namespaceObject = require("./BaseCanvas.constants.cjs");
const waitForNodeMeasurements = (getNodes)=>new Promise((resolve)=>{
        const startTime = Date.now();
        const timeoutInMs = 500;
        const check = ()=>{
            const currentNodes = getNodes();
            if (currentNodes.every((node)=>node.measured)) resolve();
            else if (Date.now() - startTime > timeoutInMs) resolve();
            else requestAnimationFrame(check);
        };
        check();
    });
const useAutoLayout = (nodes, initialAutoLayout, fitViewOptions)=>{
    const [isReady, setIsReady] = (0, external_react_namespaceObject.useState)(false);
    const hasRunLayout = (0, external_react_namespaceObject.useRef)(false);
    const timeoutRef = (0, external_react_namespaceObject.useRef)(null);
    const reactFlow = (0, react_cjs_namespaceObject.useReactFlow)();
    const nodesRef = (0, external_react_namespaceObject.useRef)([]);
    const prevNodeIds = (0, external_react_namespaceObject.useRef)('');
    const currentNodeIds = nodes?.map((n)=>n.id).sort().join(',') ?? '';
    const isNewNodeSet = currentNodeIds !== prevNodeIds.current && currentNodeIds?.length > 0;
    if (isNewNodeSet) {
        hasRunLayout.current = false;
        prevNodeIds.current = currentNodeIds;
    }
    nodesRef.current = nodes;
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!initialAutoLayout) return void setIsReady(true);
        if (hasRunLayout.current && !isNewNodeSet) return;
        if (nodes?.length === 0) return;
        const runLayout = async ()=>{
            try {
                await waitForNodeMeasurements(()=>reactFlow.getNodes());
                await initialAutoLayout();
                hasRunLayout.current = true;
                timeoutRef.current = setTimeout(()=>{
                    const options = fitViewOptions || external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.fitViewOptions;
                    const duration = isReady ? options.duration : 0;
                    reactFlow.fitView({
                        ...options,
                        duration
                    });
                    setIsReady(true);
                    timeoutRef.current = null;
                }, external_BaseCanvas_constants_cjs_namespaceObject.FIT_VIEW_DELAY_MS);
            } catch  {
                setIsReady(true);
            }
        };
        runLayout();
        return ()=>{
            if (timeoutRef.current) {
                clearTimeout(timeoutRef.current);
                timeoutRef.current = null;
            }
        };
    }, [
        nodes?.length,
        reactFlow,
        initialAutoLayout,
        isNewNodeSet,
        fitViewOptions,
        isReady
    ]);
    return {
        isReady
    };
};
const useFitView = (nodes, delay, fitViewOptions)=>{
    const reactFlow = (0, react_cjs_namespaceObject.useReactFlow)();
    const currentViewport = reactFlow.getViewport();
    const timeoutRef = (0, external_react_namespaceObject.useRef)(null);
    (0, external_react_namespaceObject.useEffect)(()=>()=>{
            if (timeoutRef.current) {
                clearTimeout(timeoutRef.current);
                timeoutRef.current = null;
            }
        }, []);
    const fitView = (0, external_react_namespaceObject.useCallback)(()=>{
        if (timeoutRef.current) clearTimeout(timeoutRef.current);
        timeoutRef.current = setTimeout(()=>{
            reactFlow.fitView({
                ...fitViewOptions ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.fitViewOptions,
                minZoom: currentViewport.zoom,
                maxZoom: currentViewport.zoom,
                nodes: nodes && nodes.length > 0 ? nodes : void 0
            });
            timeoutRef.current = null;
        }, delay ?? 0);
    }, [
        reactFlow,
        currentViewport,
        nodes,
        delay,
        fitViewOptions
    ]);
    return {
        fitView
    };
};
const useEnsureNodesInView = ()=>{
    const reactFlow = (0, react_cjs_namespaceObject.useReactFlow)();
    const ensureNodesInView = (0, external_react_namespaceObject.useCallback)((nodeIds, options)=>{
        const nodes = reactFlow.getNodes().filter((node)=>nodeIds.includes(node.id));
        if (0 === nodes.length) return;
        const fitViewOptions = {
            ...external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.fitViewOptions,
            nodes,
            padding: options?.padding ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.fitViewOptions.padding,
            duration: options?.duration ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.fitViewOptions.duration,
            minZoom: options?.minZoom ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.zoom.min,
            maxZoom: options?.maxZoom ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.zoom.max
        };
        if (options?.maintainZoom) {
            const currentZoom = reactFlow.getViewport().zoom;
            fitViewOptions.minZoom = currentZoom;
            fitViewOptions.maxZoom = currentZoom;
        }
        reactFlow.fitView(fitViewOptions);
    }, [
        reactFlow
    ]);
    const ensureAllNodesInView = (0, external_react_namespaceObject.useCallback)((options)=>{
        const fitViewOptions = {
            ...external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.fitViewOptions,
            padding: options?.padding ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.fitViewOptions.padding,
            duration: options?.duration ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.fitViewOptions.duration,
            minZoom: options?.minZoom ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.zoom.min,
            maxZoom: options?.maxZoom ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.zoom.max
        };
        if (options?.maintainZoom) {
            const currentZoom = reactFlow.getViewport().zoom;
            fitViewOptions.minZoom = currentZoom;
            fitViewOptions.maxZoom = currentZoom;
        }
        reactFlow.fitView(fitViewOptions);
    }, [
        reactFlow
    ]);
    const centerNode = (0, external_react_namespaceObject.useCallback)((nodeId, options)=>{
        const node = reactFlow.getInternalNode(nodeId);
        if (!node) return;
        const nodeWidth = node.measured?.width ?? node.width;
        const nodeHeight = node.measured?.height ?? node.height;
        if (!nodeWidth || !nodeHeight) return;
        const x = node.position.x + nodeWidth / 2;
        const y = node.position.y + nodeHeight / 2;
        const zoom = options?.maintainZoom ? reactFlow.getViewport().zoom : 1;
        reactFlow.setCenter(x, y, {
            zoom,
            duration: options?.duration ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.fitViewOptions.duration
        });
    }, [
        reactFlow
    ]);
    return {
        ensureNodesInView,
        ensureAllNodesInView,
        centerNode
    };
};
const useMaintainNodesInView = (nodeIds, fitViewOptions)=>{
    const flowStoreApi = (0, react_cjs_namespaceObject.useStoreApi)();
    const reactFlowInstance = (0, react_cjs_namespaceObject.useReactFlow)();
    const resizeTimeoutRef = (0, external_react_namespaceObject.useRef)(null);
    const nodeIdsRef = (0, external_react_namespaceObject.useRef)(nodeIds);
    (0, external_react_namespaceObject.useEffect)(()=>{
        nodeIdsRef.current = nodeIds;
    }, [
        nodeIds
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        const reactFlowContainer = flowStoreApi.getState().domNode;
        if (!reactFlowContainer) return;
        const handleResize = ()=>{
            const currentNodeIds = nodeIdsRef.current;
            if (void 0 === currentNodeIds) return;
            let nodesToMaintain = currentNodeIds;
            if (0 === currentNodeIds.length) nodesToMaintain = reactFlowInstance.getNodes().map((node)=>node.id);
            if (resizeTimeoutRef.current) clearTimeout(resizeTimeoutRef.current);
            resizeTimeoutRef.current = setTimeout(()=>{
                if (0 === nodesToMaintain.length) return;
                const currentZoom = reactFlowInstance.getViewport().zoom;
                reactFlowInstance.fitView({
                    nodes: nodesToMaintain.map((id)=>({
                            id
                        })),
                    padding: fitViewOptions?.padding ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.fitViewOptions.padding,
                    duration: fitViewOptions?.duration ?? external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.fitViewOptions.duration,
                    minZoom: currentZoom,
                    maxZoom: currentZoom
                });
            }, external_BaseCanvas_constants_cjs_namespaceObject.BASE_CANVAS_DEFAULTS.maintainNodesInView.debounceMs);
        };
        const resizeObserver = new ResizeObserver(handleResize);
        resizeObserver.observe(reactFlowContainer);
        return ()=>{
            resizeObserver.disconnect();
            if (resizeTimeoutRef.current) clearTimeout(resizeTimeoutRef.current);
        };
    }, [
        flowStoreApi,
        reactFlowInstance,
        fitViewOptions
    ]);
};
exports.useAutoLayout = __webpack_exports__.useAutoLayout;
exports.useEnsureNodesInView = __webpack_exports__.useEnsureNodesInView;
exports.useFitView = __webpack_exports__.useFitView;
exports.useMaintainNodesInView = __webpack_exports__.useMaintainNodesInView;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "useAutoLayout",
    "useEnsureNodesInView",
    "useFitView",
    "useMaintainNodesInView"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
