import dagre from '@dagrejs/dagre';
import { Position, type Node, type Edge } from '@xyflow/svelte';

const dagreGraph = new dagre.graphlib.Graph();

const nodeWidth = 250;
const nodeHeight = 250;

function getLayoutedElements(nodes: Node[], edges: Edge[], direction = 'TB') {
	dagreGraph.setDefaultEdgeLabel(() => ({}));
	const isHorizontal = direction === 'LR';
	dagreGraph.setGraph({ rankdir: direction });
	nodes.forEach((node) => {
		dagreGraph.setNode(node.id, { width: nodeWidth, height: nodeHeight });
	});

	edges.forEach((edge) => {
		dagreGraph.setEdge(edge.source, edge.target);
	});

	dagre.layout(dagreGraph);

	nodes.forEach((node) => {
		const nodeWithPosition = dagreGraph.node(node.id);
		node.targetPosition = isHorizontal ? Position.Left : Position.Top;
		node.sourcePosition = isHorizontal ? Position.Right : Position.Bottom;

		// We are shifting the dagre node position (anchor=center center) to the top left
		// so it matches the React Flow node anchor point (top left).
		node.position = {
			x: nodeWithPosition.x - nodeWidth / 2,
			y: nodeWithPosition.y - nodeHeight / 2
		};
	});

	return { nodes, edges };
}

export { getLayoutedElements };
