"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__, {
    d3HierarchyLayout: ()=>d3HierarchyLayout
});
const react_cjs_namespaceObject = require("../../xyflow/react.cjs");
const external_d3_hierarchy_namespaceObject = require("d3-hierarchy");
const getPosition = (x, y, direction)=>{
    switch(direction){
        case 'TD':
            return {
                x,
                y
            };
        case 'LR':
            return {
                x: y,
                y: x
            };
        case 'BT':
            return {
                x: -x,
                y: -y
            };
        case 'RL':
            return {
                x: -y,
                y: x
            };
    }
};
const layout = (0, external_d3_hierarchy_namespaceObject.tree)().separation(()=>1);
const rootNode = {
    id: 'd3-hierarchy-root',
    x: 0,
    y: 0,
    position: {
        x: 0,
        y: 0
    },
    data: {}
};
const DEFAULT_NODE_WIDTH = 200;
const DEFAULT_NODE_HEIGHT = 100;
const d3HierarchyLayout = (nodes, edges, options)=>Promise.resolve((()=>{
        const isHorizontal = 'RL' === options.direction || 'LR' === options.direction;
        const initialNodes = [];
        let maxNodeWidth = 0;
        let maxNodeHeight = 0;
        for (const node of nodes){
            const nodeWithPosition = {
                ...node,
                ...node.position
            };
            initialNodes.push(nodeWithPosition);
            const nodeWidth = node.measured?.width ?? DEFAULT_NODE_WIDTH;
            const nodeHeight = node.measured?.height ?? DEFAULT_NODE_HEIGHT;
            maxNodeWidth = Math.max(maxNodeWidth, nodeWidth);
            maxNodeHeight = Math.max(maxNodeHeight, nodeHeight);
        }
        const nodeSize = isHorizontal ? [
            maxNodeHeight + options.spacing[1],
            maxNodeWidth + options.spacing[0]
        ] : [
            maxNodeWidth + options.spacing[0],
            maxNodeHeight + options.spacing[1]
        ];
        layout.nodeSize(nodeSize);
        const getParentId = (node)=>{
            if (node.id === rootNode.id) return;
            const incomers = (0, react_cjs_namespaceObject.getIncomers)(node, nodes, edges);
            return incomers[0]?.id || rootNode.id;
        };
        const hierarchy = (0, external_d3_hierarchy_namespaceObject.stratify)().id((d)=>d.id).parentId(getParentId)([
            rootNode,
            ...initialNodes
        ]);
        const root = layout(hierarchy);
        const layoutNodes = new Map();
        for (const node of root)if (node.id) layoutNodes.set(node.id, node);
        const nextNodes = nodes.map((node)=>{
            const layoutNode = layoutNodes.get(node.id);
            if (!layoutNode) throw new Error(`Layout node not found for ${node.id}`);
            const { x, y } = layoutNode;
            const position = getPosition(x, y, options.direction);
            const nodeWidth = node.measured?.width ?? DEFAULT_NODE_WIDTH;
            const nodeHeight = node.measured?.height ?? DEFAULT_NODE_HEIGHT;
            const offsetPosition = {
                x: position.x - nodeWidth / 2,
                y: position.y - nodeHeight / 2
            };
            return {
                ...node,
                position: offsetPosition
            };
        });
        return {
            nodes: nextNodes,
            edges
        };
    })());
exports.d3HierarchyLayout = __webpack_exports__.d3HierarchyLayout;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "d3HierarchyLayout"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
