import type { GraphModel } from "./components/Graph";

export function graphToDot(g: GraphModel, cutNodeIds: Set<string> = new Set()): string {
  // Directed graph, use neato layout so we can use pos attributes
  const lines = [];
  lines.push('digraph G {');
  lines.push('  graph [splines=true, overlap=false, rankdir=LR];');
  lines.push('  node [shape=rectangle, style=filled, fillcolor="white", fontsize=12];');

  // nodes
  for (const n of g.nodes) {
    const attrs = [];
    attrs.push(`label=\"${n.label}\"`);
    attrs.push(`id=\"${n.id}\"`)
    if (cutNodeIds.has(n.id)) {
      attrs.push(`fillcolor="#d9d9d9"`, `style="filled,dashed"`);
    }
    lines.push(`  \"${n.id}\" [${attrs.join(', ')}];`);
  }

  // edges
  for (const e of g.edges) {
    const attrs = [];
    attrs.push(`id=\"${e.id}\"`)
    lines.push(`  \"${e.from}\" -> \"${e.to}\" [${attrs.join(', ')}];`);
  }

  // close
  lines.push('}');
  return lines.join('\n');
}