///|
priv struct CoreFunctionSignature {
  function : XPathCoreFunction
  min_arity : Int
  max_arity : Int?
}

///|
fn core_function_signature(name : String) -> CoreFunctionSignature? {
  match name {
    "id" => Some({ function: Id, min_arity: 1, max_arity: Some(1) })
    "last" => Some({ function: Last, min_arity: 0, max_arity: Some(0) })
    "position" => Some({ function: Position, min_arity: 0, max_arity: Some(0) })
    "count" => Some({ function: Count, min_arity: 1, max_arity: Some(1) })
    "local-name" =>
      Some({ function: LocalName, min_arity: 0, max_arity: Some(1) })
    "namespace-uri" =>
      Some({ function: NamespaceUri, min_arity: 0, max_arity: Some(1) })
    "name" => Some({ function: Name, min_arity: 0, max_arity: Some(1) })
    "string" => Some({ function: String, min_arity: 0, max_arity: Some(1) })
    "concat" => Some({ function: Concat, min_arity: 2, max_arity: None })
    "starts-with" =>
      Some({ function: StartsWith, min_arity: 2, max_arity: Some(2) })
    "contains" => Some({ function: Contains, min_arity: 2, max_arity: Some(2) })
    "substring-before" =>
      Some({ function: SubstringBefore, min_arity: 2, max_arity: Some(2) })
    "substring-after" =>
      Some({ function: SubstringAfter, min_arity: 2, max_arity: Some(2) })
    "substring" =>
      Some({ function: Substring, min_arity: 2, max_arity: Some(3) })
    "string-length" =>
      Some({ function: StringLength, min_arity: 0, max_arity: Some(1) })
    "normalize-space" =>
      Some({ function: NormalizeSpace, min_arity: 0, max_arity: Some(1) })
    "translate" =>
      Some({ function: Translate, min_arity: 3, max_arity: Some(3) })
    "boolean" => Some({ function: Boolean, min_arity: 1, max_arity: Some(1) })
    "not" => Some({ function: Not, min_arity: 1, max_arity: Some(1) })
    "true" => Some({ function: True, min_arity: 0, max_arity: Some(0) })
    "false" => Some({ function: False, min_arity: 0, max_arity: Some(0) })
    "lang" => Some({ function: Lang, min_arity: 1, max_arity: Some(1) })
    "number" => Some({ function: Number, min_arity: 0, max_arity: Some(1) })
    "sum" => Some({ function: Sum, min_arity: 1, max_arity: Some(1) })
    "floor" => Some({ function: Floor, min_arity: 1, max_arity: Some(1) })
    "ceiling" => Some({ function: Ceiling, min_arity: 1, max_arity: Some(1) })
    "round" => Some({ function: Round, min_arity: 1, max_arity: Some(1) })
    _ => None
  }
}

///|
fn raise_static(
  span : @diagnostic.SourceSpan,
  message : String,
) -> Unit raise @diagnostic.XPathError {
  raise Static(span=Some(span), message~)
}

///|
fn resolve_xpath_name(
  name : XPathName,
  static_context : @context.StaticContext,
) -> Unit raise @diagnostic.XPathError {
  match name.prefix {
    None => name.set_namespace(None)
    Some(prefix) =>
      match static_context.resolve_prefix(prefix) {
        Some(namespace_uri) => name.set_namespace(Some(namespace_uri))
        None => raise_static(name.span, "unknown namespace prefix '\{prefix}'")
      }
  }
}

///|
fn validate_function_arity(
  call : XPathFunctionCall,
  signature : CoreFunctionSignature,
) -> Unit raise @diagnostic.XPathError {
  let actual = call.arguments.length()
  let too_few = actual < signature.min_arity
  let too_many = match signature.max_arity {
    Some(maximum) => actual > maximum
    None => false
  }
  if too_few || too_many {
    let expected = match signature.max_arity {
      None => "at least \{signature.min_arity}"
      Some(maximum) =>
        if maximum == signature.min_arity {
          "\{maximum}"
        } else {
          "\{signature.min_arity}..\{maximum}"
        }
    }
    raise_static(
      call.name.span,
      "function '\{call.name.lexical}' expects \{expected} argument(s), found \{actual}",
    )
  }
}

///|
fn validate_function_call(
  call : XPathFunctionCall,
) -> Unit raise @diagnostic.XPathError {
  if call.name.prefix is Some(_) {
    raise_static(
      call.name.span,
      "extension function '\{call.name.lexical}' is not registered",
    )
  }
  match core_function_signature(call.name.local_name) {
    None =>
      raise_static(
        call.name.span,
        "unknown XPath 1.0 core function '\{call.name.lexical}'",
      )
    Some(signature) => {
      validate_function_arity(call, signature)
      call.set_core(signature.function)
    }
  }
}

///|
fn validate_parsed_xpath(
  parsed : ParsedXPath,
  static_context : @context.StaticContext,
) -> XPathExpr raise @diagnostic.XPathError {
  for name in parsed.names {
    resolve_xpath_name(name, static_context)
  }
  for call in parsed.functions {
    validate_function_call(call)
  }
  parsed.expression
}