///|
/// Flowchart elements. Connections and region members are references, not children.
#derive(Eq, Debug)
pub(all) enum FlowchartElement {
  Diagram(String)
  Lane(@flowchart.Lane)
  Group(@flowchart.Group)
  Node(@flowchart.Node)
  Connection(@flowchart.Connection)
  Note(@flowchart.Note)
  Region(@flowchart.Region)
  Branch(@flowchart.Branch)
}

///|
pub extend FlowchartElement with Eq::{equal, not_equal}

///|
pub extend FlowchartElement with @debug.Debug::{to_repr}

///|
pub fn FlowchartElement::kind_name(self : FlowchartElement) -> String {
  match self {
    Diagram(_) => "diagram"
    Lane(_) => "lane"
    Group(_) => "group"
    Node(_) => "node"
    Connection(_) => "connection"
    Note(_) => "note"
    Region(_) => "region"
    Branch(_) => "branch"
  }
}

///|
pub fn FlowchartElement::id(self : FlowchartElement) -> String? {
  match self {
    Diagram(id) => Some(id)
    Lane(v) => Some(v.id)
    Group(v) => Some(v.id)
    Node(v) => Some(v.id)
    Connection(v) => Some(v.id)
    Note(v) => Some(v.id)
    Region(v) => Some(v.id)
    Branch(v) => Some(v.id)
  }
}

///|
fn binding_span(origin : @flowchart.SourceBinding?) -> @diagrams.Span? {
  match origin {
    Some(o) => o.span
    None => None
  }
}

///|
/// Index a flowchart. Root children follow field order: lanes, groups, nodes,
/// notes, connections and regions. A group belongs to its parent group, else
/// its lane; a node or note belongs to its innermost group, else its lane; a
/// region belongs to its parent region and owns its branches. Connections and
/// region members stay references. A repeated container identifier fails only
/// when a child names it (`ambiguous-lane`, `ambiguous-group`, `ambiguous-region`).
pub fn Tree::flowchart(
  view : @flowchart.Flowchart,
  limits~ : IndexLimits = IndexLimits::new(),
  revision~ : Int32 = 0,
) -> Result[Tree, String] {
  let b = Builder::new(Flowchart(Diagram(view.id)), limits)
  for lane in view.lanes {
    b.register(
      "lane",
      lane.id,
      b.add(Flowchart(Lane(lane)), slot=b.slot("lane")),
    )
  }
  let groups = view.groups.map(group => {
    let index = b.add(Flowchart(Group(group)), slot=b.slot("group"))
    b.register("group", group.id, index)
    index
  })
  let nodes = view.nodes.map(node => {
    b.add(
      Flowchart(Node(node)),
      span=binding_span(node.origin),
      slot=b.slot("node"),
    )
  })
  let notes = view.notes.map(note => {
    b.add(
      Flowchart(Note(note)),
      span=binding_span(note.origin),
      slot=b.slot("note"),
    )
  })
  for edge in view.connections {
    b.add(
      Flowchart(Connection(edge)),
      span=binding_span(edge.origin),
      slot=b.slot("connection"),
    )
    |> ignore
  }
  let regions = view.regions.map(region => {
    let index = b.add(
      Flowchart(Region(region)),
      span=binding_span(region.origin),
      slot=b.slot("region"),
    )
    b.register("region", region.id, index)
    for j, branch in region.branches {
      b.add(
        Flowchart(Branch(branch)),
        span=binding_span(branch.origin),
        parent=index,
        slot=j,
      )
      |> ignore
    }
    index
  })
  fn place(index : Int32, group : String?, lane : String?) -> Unit {
    match group {
      Some(id) => b.own(index, b.container("group", id))
      None => if lane is Some(id) { b.own(index, b.container("lane", id)) }
    }
  }
  for i, group in view.groups {
    place(groups[i], group.parent, group.lane)
  }
  for i, node in view.nodes {
    place(nodes[i], node.groups.last(), node.lane)
  }
  for i, note in view.notes {
    place(notes[i], note.groups.last(), note.lane)
  }
  for i, region in view.regions {
    if region.parent is Some(id) {
      b.own(regions[i], b.container("region", id))
    }
  }
  b.finish(revision)
}