import './graph.mjs';
import isUndefined from '../../../../../lodash-es@4.17.23/node_modules/lodash-es/isUndefined.mjs';
import clone from '../../../../../lodash-es@4.17.23/node_modules/lodash-es/clone.mjs';
import map from '../../../../../lodash-es@4.17.23/node_modules/lodash-es/map.mjs';

function write(g) {
  var json = {
    options: {
      directed: g.isDirected(),
      multigraph: g.isMultigraph(),
      compound: g.isCompound(),
    },
    nodes: writeNodes(g),
    edges: writeEdges(g),
  };
  if (!isUndefined(g.graph())) {
    json.value = clone(g.graph());
  }
  return json;
}

function writeNodes(g) {
  return map(g.nodes(), function (v) {
    var nodeValue = g.node(v);
    var parent = g.parent(v);
    var node = { v: v };
    if (!isUndefined(nodeValue)) {
      node.value = nodeValue;
    }
    if (!isUndefined(parent)) {
      node.parent = parent;
    }
    return node;
  });
}

function writeEdges(g) {
  return map(g.edges(), function (e) {
    var edgeValue = g.edge(e);
    var edge = { v: e.v, w: e.w };
    if (!isUndefined(e.name)) {
      edge.name = e.name;
    }
    if (!isUndefined(edgeValue)) {
      edge.value = edgeValue;
    }
    return edge;
  });
}

export { write };
