///|
priv struct VariableKey {
  local_name : String
  namespace_uri : String?
} derive(Eq)

///|
priv struct VariableBinding {
  key : VariableKey
  value : @value.XPathValue
}

///|
pub struct DynamicContext {
  priv document_ : @model.Document
  priv context_node_ : @model.NodeId
  priv position_ : Int
  priv size_ : Int
  priv variables_ : Array[VariableBinding]
  priv limits_ : EvaluationLimits
}

///|
fn invalid_dynamic_context(message : String) -> @diagnostic.XPathError {
  Evaluate(span=None, message~)
}

///|
fn ensure_node_belongs_to_document(
  document : @model.Document,
  node : @model.NodeId,
  message : String,
) -> Unit raise @diagnostic.XPathError {
  ignore(
    document.kind(node) catch {
      _ => raise invalid_dynamic_context(message)
    },
  )
}

///|
fn variable_key(name : @model.QName) -> VariableKey {
  { local_name: name.local_name(), namespace_uri: name.namespace_uri() }
}

///|
pub fn DynamicContext::new(
  document : @model.Document,
  context_node : @model.NodeId,
) -> DynamicContext raise @diagnostic.XPathError {
  ensure_node_belongs_to_document(
    document, context_node, "context node does not belong to the active document",
  )
  {
    document_: document,
    context_node_: context_node,
    position_: 1,
    size_: 1,
    variables_: [],
    limits_: EvaluationLimits::safe_defaults(),
  }
}

///|
pub fn DynamicContext::with_variable(
  self : DynamicContext,
  name : @model.QName,
  value : @value.XPathValue,
) -> DynamicContext raise @diagnostic.XPathError {
  match value {
    Nodes(nodes) => {
      let owner_root = nodes.document().root()
      ensure_node_belongs_to_document(
        self.document_,
        owner_root,
        "node-set variable belongs to a different document",
      )
    }
    _ => ()
  }
  let key = variable_key(name)
  let variables = self.variables_.copy()
  let mut replaced = false
  for index in 0.. @value.XPathValue? {
  let key = variable_key(name)
  for binding in self.variables_ {
    if binding.key == key {
      return Some(binding.value)
    }
  }
  None
}

///|
pub fn DynamicContext::with_focus(
  self : DynamicContext,
  context_node : @model.NodeId,
  position : Int,
  size : Int,
) -> DynamicContext raise @diagnostic.XPathError {
  if size < 1 {
    raise invalid_dynamic_context("context size must be at least one")
  }
  if position < 1 || position > size {
    raise invalid_dynamic_context(
      "context position must be within the one-based context size",
    )
  }
  ensure_node_belongs_to_document(
    self.document_,
    context_node,
    "focus node does not belong to the active document",
  )
  {
    document_: self.document_,
    context_node_: context_node,
    position_: position,
    size_: size,
    variables_: self.variables_,
    limits_: self.limits_,
  }
}

///|
pub fn DynamicContext::with_limits(
  self : DynamicContext,
  limits : EvaluationLimits,
) -> DynamicContext {
  {
    document_: self.document_,
    context_node_: self.context_node_,
    position_: self.position_,
    size_: self.size_,
    variables_: self.variables_,
    limits_: limits,
  }
}

///|
pub fn DynamicContext::limits(self : DynamicContext) -> EvaluationLimits {
  self.limits_
}

///|
pub fn DynamicContext::document(self : DynamicContext) -> @model.Document {
  self.document_
}

///|
pub fn DynamicContext::context_node(self : DynamicContext) -> @model.NodeId {
  self.context_node_
}

///|
pub fn DynamicContext::position(self : DynamicContext) -> Int {
  self.position_
}

///|
pub fn DynamicContext::size(self : DynamicContext) -> Int {
  self.size_
}