///|
fn Document::checked_index(
  self : Document,
  node : NodeId,
) -> Int raise @diagnostic.XPathError {
  checked_index(self.token, self.nodes, node)
}

///|
fn Document::ids_for(self : Document, indices : Array[Int]) -> Array[NodeId] {
  indices.map(index => node_id(self.token, index))
}

///|
/// Return the unique XPath root node.
pub fn Document::root(self : Document) -> NodeId {
  node_id(self.token, self.root_index)
}

///|
/// Return a node's kind, rejecting an invalid or foreign identity.
pub fn Document::kind(
  self : Document,
  node : NodeId,
) -> NodeKind raise @diagnostic.XPathError {
  self.nodes[self.checked_index(node)].kind
}

///|
/// Return the expanded name for element, attribute, namespace and PI nodes.
pub fn Document::name(
  self : Document,
  node : NodeId,
) -> QName? raise @diagnostic.XPathError {
  self.nodes[self.checked_index(node)].name
}

///|
fn Document::descendant_text(self : Document, index : Int) -> String {
  let output = StringBuilder()
  let stack : Array[Int] = []
  push_reversed(stack, self.nodes[index].children)
  while stack.pop() is Some(current) {
    let record = self.nodes[current]
    if record.kind == Text {
      output.write_string(record.value)
    } else if record.kind == Root || record.kind == Element {
      push_reversed(stack, record.children)
    }
  }
  output.to_string()
}

///|
/// Compute the XPath 1.0 string-value of a node.
pub fn Document::string_value(
  self : Document,
  node : NodeId,
) -> String raise @diagnostic.XPathError {
  let index = self.checked_index(node)
  let record = self.nodes[index]
  match record.kind {
    Root | Element => self.descendant_text(index)
    Attribute | Namespace | Text | ProcessingInstruction | Comment =>
      record.value
  }
}

///|
/// Return the parent/owner relation; root has no parent.
pub fn Document::parent(
  self : Document,
  node : NodeId,
) -> NodeId? raise @diagnostic.XPathError {
  match self.nodes[self.checked_index(node)].parent {
    Some(index) => Some(node_id(self.token, index))
    None => None
  }
}

///|
/// Return child-axis nodes in document order.
pub fn Document::children(
  self : Document,
  node : NodeId,
) -> Array[NodeId] raise @diagnostic.XPathError {
  let record = self.nodes[self.checked_index(node)]
  self.ids_for(record.children)
}

///|
/// Return attributes in source order. They are not child nodes.
pub fn Document::attributes(
  self : Document,
  element : NodeId,
) -> Array[NodeId] raise @diagnostic.XPathError {
  let record = self.nodes[self.checked_index(element)]
  if record.kind != Element {
    raise model_error("attributes() requires an element identity")
  }
  self.ids_for(record.attributes)
}

///|
/// Return in-scope namespace nodes in deterministic prefix order.
pub fn Document::namespaces(
  self : Document,
  element : NodeId,
) -> Array[NodeId] raise @diagnostic.XPathError {
  let record = self.nodes[self.checked_index(element)]
  if record.kind != Element {
    raise model_error("namespaces() requires an element identity")
  }
  self.ids_for(record.namespaces)
}

///|
/// Find the element whose explicitly typed ID exactly matches `value`.
pub fn Document::element_by_id(self : Document, value : String) -> NodeId? {
  for entry in self.id_index {
    if entry.0 == value {
      return Some(node_id(self.token, entry.1))
    }
  }
  None
}

///|
/// Return the stable document-order rank used for NodeSet normalization.
pub fn Document::document_order(
  self : Document,
  node : NodeId,
) -> Int raise @diagnostic.XPathError {
  self.nodes[self.checked_index(node)].order
}