import {
  createDefinitionGraphNode,
  definitionConnectionError,
  type DefinitionGraph,
  type DefinitionGraphNodeType
} from "../src/definitionGraph";

const library: DefinitionGraphNodeType[] = [
  {
    type: "start",
    category: "control",
    category_label: "Control",
    label: "Start",
    description: "Start",
    icon: "play",
    input_ports: [],
    output_ports: [
      { id: "output", label: "Output", required: true, multiple: false, minimum_connections: 1 }
    ],
    config_fields: [],
    default_config: { mode: "manual" }
  },
  {
    type: "end",
    category: "control",
    category_label: "Control",
    label: "End",
    description: "End",
    icon: "circle-stop",
    input_ports: [
      { id: "input", label: "Input", required: true, multiple: false, minimum_connections: 1 }
    ],
    output_ports: [],
    config_fields: [],
    default_config: {}
  }
];

const graph: DefinitionGraph = {
  schema_version: 1,
  nodes: [
    { id: "start", type: "start", label: "Start", position: { x: 0, y: 0 }, config: {} },
    { id: "end", type: "end", label: "End", position: { x: 100, y: 0 }, config: {} }
  ],
  edges: []
};

if (definitionConnectionError(
  graph,
  library,
  { source: "start", target: "end" }
) !== null) {
  throw new Error("Expected a valid library-driven connection.");
}

graph.edges.push({ id: "edge", source: "start", target: "end" });
if (!definitionConnectionError(
  graph,
  library,
  { source: "start", target: "end" }
)) {
  throw new Error("Expected duplicate connection validation.");
}

const created = createDefinitionGraphNode("start", { x: 40, y: 50 }, library);
if (created.config.mode !== "manual" || created.position.x !== 40) {
  throw new Error("Expected node creation to clone library defaults.");
}
