/**
 * Canvas drawing for the graph-render tool. Takes a laid-out graph and
 * produces a PNG buffer.
 *
 * Visual contract:
 *   - Background matches the /graph page's surface colour (#FAFAF8).
 *   - Node fill comes from platform/lib/graph-style's resolveNodeColour —
 *     identical hue to the live page for the same labels.
 *   - Node label is pickShortLabel from the same shared lib, rendered
 *     below the node circle in a sans-serif fallback. Exact webfont match
 *     is filed as a follow-up.
 *   - Edges are thin grey lines with a small arrowhead. Edge type labels
 *     are intentionally omitted — they crowd the canvas at high node
 *     counts and the operator can drill into the live page if they need
 *     them.
 */
import { createCanvas } from "@napi-rs/canvas";
import { resolveNodeColour, pickShortLabel, } from "../../../../../lib/graph-style/dist/index.js";
const BACKGROUND = "#FAFAF8";
const EDGE_COLOUR = "#C9C7C2";
const NODE_RADIUS = 12;
const NODE_STROKE = "#FFFFFF";
const NODE_STROKE_WIDTH = 2;
const LABEL_COLOUR = "#1A1A1A";
const LABEL_FONT = "12px sans-serif";
const LABEL_PADDING_Y = 4;
export function drawGraph(input) {
    const { layout, nodeProperties, width, height } = input;
    const canvas = createCanvas(width, height);
    const ctx = canvas.getContext("2d");
    ctx.fillStyle = BACKGROUND;
    ctx.fillRect(0, 0, width, height);
    const nodeIndex = new Map();
    for (const n of layout.nodes)
        nodeIndex.set(n.id, n);
    // Edges first, so node circles cover the endpoints.
    ctx.strokeStyle = EDGE_COLOUR;
    ctx.lineWidth = 1;
    for (const e of layout.edges) {
        const from = nodeIndex.get(e.from);
        const to = nodeIndex.get(e.to);
        if (!from || !to)
            continue;
        ctx.beginPath();
        ctx.moveTo(from.x, from.y);
        ctx.lineTo(to.x, to.y);
        ctx.stroke();
    }
    // Nodes: filled circle, white stroke, label below.
    ctx.font = LABEL_FONT;
    ctx.textAlign = "center";
    ctx.textBaseline = "top";
    for (const n of layout.nodes) {
        const { colour } = resolveNodeColour(n.labels);
        ctx.fillStyle = colour;
        ctx.beginPath();
        ctx.arc(n.x, n.y, NODE_RADIUS, 0, Math.PI * 2);
        ctx.fill();
        ctx.lineWidth = NODE_STROKE_WIDTH;
        ctx.strokeStyle = NODE_STROKE;
        ctx.stroke();
        const props = nodeProperties[n.id] ?? {};
        const labelNode = {
            id: n.id,
            labels: n.labels,
            properties: props,
        };
        ctx.fillStyle = LABEL_COLOUR;
        ctx.fillText(pickShortLabel(labelNode), n.x, n.y + NODE_RADIUS + LABEL_PADDING_Y);
    }
    return canvas.toBuffer("image/png");
}
//# sourceMappingURL=draw.js.map