///|
#derive(Debug, Eq, ToJson)
pub(all) enum Rooting {
  Rooted
  Unrooted
  Unknown
}

///|
#derive(Debug, Eq)
pub(all) enum PhyloError {
  Syntax(Int32, String)
  InvalidLength(Int32, String)
  MissingLength(Int32)
  DuplicateTaxon(String)
  AnonymousTaxon(Int32)
  RootedTreeRequired
  RootingConflict
  PolytomyNotSupported(Int32, Int32)
  UnaryNotSupported(Int32)
  TaxaSetMismatch(Array[String], Array[String])
  TraitMismatch(Array[String], Array[String])
  UnsupportedTrait(String)
  UnknownTaxon(String)
  EmptySelection
  EmptyIngroup
  DuplicateSelection(String)
  NonMonophyleticOutgroup
  NumericOverflow
}

///|
priv suberror Failure {
  Fault(PhyloError)
}

///|
struct Node {
  mut parent : Int32?
  children : Array[Int32]
  mut label : String?
  mut length : Double?
  comments : Array[String]
}

///|
fn new_node(parent : Int32?, label : String?) -> Node {
  { parent, children: [], label, length: None, comments: [], }
}

///|
pub struct PhyloTree {
  arena : Array[Node]
  root : Int32
  rooting : Rooting
  comments : Array[String]
}

///|
#derive(Debug, Eq, ToJson)
pub(all) struct NodeRecord {
  id : Int32
  parent : Int32?
  children : Array[Int32]
  label : String?
  length : Double?
  comments : Array[String]
}

///|
#derive(Debug, ToJson)
pub(all) struct RFComparisonReport {
  mode : Rooting
  taxa : Array[String]
  shared : Array[Array[String]]
  only_left : Array[Array[String]]
  only_right : Array[Array[String]]
  distance : Int32
}

///|
#derive(Debug, ToJson)
pub(all) struct ParsimonyResult {
  score : Int32
  candidates : Array[Array[Char]]
  assignment : Array[Char?]
  changed_edges : Array[(Int32, Int32)]
  missing_taxa : Array[String]
}

///|
#derive(Debug, ToJson)
pub(all) struct DistanceMatrix {
  taxa : Array[String]
  values : Array[Array[Double]]
}

///|
#derive(Debug, ToJson)
pub(all) struct TransformWitness {
  old_to_new : Array[Int32?]
  retained_taxa : Array[String]
  edge_paths : Array[Array[Int32]]
  diameter_endpoints : Array[String]
  diameter : Double?
  split_edge : Array[Int32]
  offset : Double?
}

///|
pub(all) struct TransformResult {
  tree : PhyloTree
  witness : TransformWitness
}

///|
pub fn PhyloTree::root_id(self : PhyloTree) -> Int32 {
  self.root
}

///|
pub fn PhyloTree::rooting(self : PhyloTree) -> Rooting {
  self.rooting
}

///|
pub fn PhyloTree::nodes(self : PhyloTree) -> Array[NodeRecord] {
  self.arena.mapi((id, n) => {
    id,
    parent: n.parent,
    children: n.children.copy(),
    label: n.label,
    length: n.length,
    comments: n.comments.copy(),
  })
}

///|
pub fn PhyloTree::tree_comments(self : PhyloTree) -> Array[String] {
  self.comments.copy()
}

///|
fn traversal(tree : PhyloTree) -> Array[Int32] {
  let order = []
  let stack = [tree.root]
  while stack.length() > 0 {
    let n = stack.pop().unwrap()
    order.push(n)
    for i = tree.arena[n].children.length() - 1; i >= 0; i = i - 1 {
      stack.push(tree.arena[n].children[i])
    }
  }
  order
}

///|
fn leaf_ids(tree : PhyloTree) -> Array[Int32] {
  let ids = traversal(tree).filter(n => tree.arena[n].children.is_empty())
  ids.sort_by((a, b) => {
    tree.arena[a].label.unwrap().lexical_compare(tree.arena[b].label.unwrap())
  })
  ids
}

///|
pub fn PhyloTree::taxa(self : PhyloTree) -> Array[String] {
  leaf_ids(self).map(n => self.arena[n].label.unwrap())
}

///|
fn compare_labels(a : Array[String], b : Array[String]) -> Int32 {
  for i = 0; i < a.length() && i < b.length(); i = i + 1 {
    let order = a[i].lexical_compare(b[i])
    if order != 0 {
      return order
    }
  }
  a.length().compare(b.length())
}