import type { Edge, Node } from '@vue-flow/core';

export function definitionToFlowElements(definition?: {
  edges?: any[];
  nodes?: any[];
}): { edges: Edge[]; nodes: Node[] } {
  const rawNodes = definition?.nodes || [];
  const rawEdges = definition?.edges || [];

  const nodes: Node[] = rawNodes.map((node: any) => ({
    ...node,
    position: {
      x: Number(node.position?.x) || 0,
      y: Number(node.position?.y) || 0,
    },
    data: {
      ...(node.data || {}),
      executionStatus: undefined,
      executionResult: undefined,
    },
  }));

  const edges: Edge[] = rawEdges.map((edge: any) => ({
    ...edge,
    type: edge.type || 'addButton',
    data: {
      ...(edge.data || {}),
      status: undefined,
    },
  }));

  return { nodes, edges };
}
