import { encodeBase64 } from '@saulx/utils';
function makeLabel(node) {
    return `${node.key}\n${encodeBase64(node.hash).substring(0, 5)}`;
}
function wrapFormatter(dataFormatter, node) {
    return `\n${dataFormatter(node.data).replace('"', '\\"')}`;
}
// This can be visualized with Graphviz dot.
// Online: https://dreampuf.github.io/GraphvizOnline/?engine=dot
export default function draw(csmt, dataFormatter) {
    const root = csmt.getRoot();
    const lines = [];
    const nodes = [];
    let i = 0;
    const walk = (node, prev) => {
        const cur = i;
        const left = node.left;
        const right = node.right;
        const isLeaf = !left && !right;
        nodes.push(`n${cur} [label="${makeLabel(node) + ((dataFormatter && node.data) ? wrapFormatter(dataFormatter, node) : '')}"${isLeaf ? ' shape=box' : ''}];`);
        if (cur > 0) {
            lines.push(`n${prev} -- n${cur}`);
        }
        if (left) {
            i++;
            walk(left, cur);
        }
        if (right) {
            i++;
            walk(right, cur);
        }
    };
    if (root) {
        walk(root, i);
    }
    return `graph ethane {\n${nodes.join('\n')}\n${lines.join('\n')}\n}`;
}
//# sourceMappingURL=draw-dot.js.map