import type { Node, Edge } from "@xyflow/react";
import type { LessonPlanNode, LessonPlanEdge } from "../types";

/**
 * 纯函数：将课案 nodes/edges 映射为 React Flow 格式。
 * 从 node-editor.tsx 抽取，便于单元测试。
 */

export function toRfNodes(
  nodes: LessonPlanNode[],
  selectedNodeId: string | null,
): Node[] {
  return nodes.map((n) => ({
    id: n.id,
    type: "lesson",
    position: n.position,
    data: { node: n } as Record<string, unknown>,
    selected: n.id === selectedNodeId,
  }));
}

export function toRfEdges(edges: LessonPlanEdge[]): Edge[] {
  return edges.map((e) => ({
    ...e,
    animated: true,
    style: { stroke: "#1976d2", strokeWidth: 2 },
  }));
}

/**
 * 将 React Flow edges 转回课案 edges 格式。
 */
export function fromRfEdges(
  rfEdges: Edge[],
): LessonPlanEdge[] {
  return rfEdges.map((e) => ({
    id: e.id,
    source: e.source,
    target: e.target,
    sourceHandle: e.sourceHandle ?? null,
    targetHandle: e.targetHandle ?? null,
  }));
}
