///|
fn main {
  run_cli(@env.args())
}

///|
fn run_cli(args : Array[String]) -> Unit {
  let normalized = normalize_args(args)
  if normalized.is_empty() ||
    normalized[0] == "help" ||
    normalized[0] == "--help" {
    print_help()
    return
  }
  if normalized[0] == "version" || normalized[0] == "--version" {
    println(
      "problem-tool \{@problem.library_version()} (6P66006/moon-problem-details)",
    )
    return
  }
  let command = normalized[0]
  let rest = normalized[1:].to_owned()
  match command {
    "parse" => command_parse(rest)
    "validate" => command_validate(rest)
    "canonicalize" => command_canonicalize(rest)
    "make" => command_make(rest)
    "registry" => command_registry(rest)
    "audit" => command_audit(rest)
    "stats" => command_stats(rest)
    "batch" => command_batch(rest)
    _ => print_cli_error("UnknownCommand", "unknown command: \{command}")
  }
}

///|
/// Native and JS include the executable name; wasm runners may not.
fn normalize_args(args : Array[String]) -> Array[String] {
  if args.is_empty() {
    return []
  }
  if is_command(args[0]) {
    args
  } else {
    args[1:].to_owned()
  }
}

///|
fn is_command(value : String) -> Bool {
  value == "parse" ||
  value == "validate" ||
  value == "canonicalize" ||
  value == "make" ||
  value == "registry" ||
  value == "audit" ||
  value == "stats" ||
  value == "batch" ||
  value == "version" ||
  value == "help" ||
  value == "--version" ||
  value == "--help"
}

///|
fn print_help() -> Unit {
  println("problem-tool 0.1.0 — RFC 9457 offline toolkit")
  println(
    "usage: problem-tool  [options]",
  )
  println("input: --input ")
  println(
    "validate: --status  --content-type  --base-uri ",
  )
  println(
    "make: --status  --title  [--type ] [--detail ] [--instance ]",
  )
  println("registry: problem-tool registry ")
  println("batch: repeat --input ; add --strict or --permissive")
}