///|
/// High-level public API for `TIMI0821/cue` — Pure MoonBit CUE Configuration
/// & Value-Lattice Unification Engine.

///|
/// Parse and evaluate a CUE source string into a `Value`.
pub fn eval(src : String) -> Value raise CueError {
  let file = parse(src)
  eval_file(file)
}

///|
/// Parse, merge, and evaluate multiple CUE source documents in a unified root vertex.
/// Order-independent: `eval_all([a, b])` produces the same lattice result as `eval_all([b, a])`.
pub fn eval_all(sources : Array[String]) -> Value raise CueError {
  let files : Array[File] = []
  for src in sources {
    files.push(parse(src))
  }
  eval_files(files)
}

///|
/// Evaluate a CUE source string and export the concrete result as `@json.Json`.
pub fn export_json(src : String) -> Json raise CueError {
  let val = eval(src)
  value_to_json(val)
}

///|
/// Evaluate and unify multiple CUE sources, exporting the concrete result as `@json.Json`.
pub fn export_json_all(sources : Array[String]) -> Json raise CueError {
  let val = eval_all(sources)
  value_to_json(val)
}

///|
/// Evaluate a CUE source string and export as a formatted JSON string.
pub fn export_json_string(
  src : String,
  indent~ : Int32 = 2,
) -> String raise CueError {
  format_json(export_json(src), indent~)
}

///|
/// Evaluate a CUE source string and export as a YAML 1.2 string.
pub fn export_yaml_string(src : String) -> String raise CueError {
  format_yaml(export_json(src))
}

///|
/// Validate a CUE source document (`cue vet`): raises the first `CueError` if any
/// constraint conflict, closed-struct violation, or unsatisfied required field exists.
pub fn vet(src : String) -> Unit raise CueError {
  let val = eval(src)
  let errs = collect_validation_errors(val, require_concrete=false)
  if !errs.is_empty() {
    raise errs[0]
  }
}

///|
/// Unify a CUE schema with a JSON payload (`schema & data`).
/// Supports both top-level schema constraints and `#Schema` definitions when the schema
/// is a plain root struct or embeds a `#Definition`.
pub fn unify_json(
  schema_src : String,
  json_src : String,
) -> Value raise CueError {
  let schema_val = eval(schema_src)
  let data_val = parse_json_value(json_src)
  unify(schema_val, data_val)
}

///|
/// Validate a JSON payload against a CUE schema (`cue vet schema.cue data.json`),
/// returning all structured field-path diagnostics (empty array means valid).
pub fn validate_json(schema_src : String, json_src : String) -> Array[CueError] {
  let unified_res : Result[Value, CueError] = Ok(
    unify_json(schema_src, json_src),
  ) catch {
    e => Err(e)
  }
  match unified_res {
    Err(e) => [e]
    Ok(val) => collect_validation_errors(val, require_concrete=true)
  }
}

///|
/// Validate a JSON payload against a CUE schema, raising the first `CueError` on failure.
pub fn vet_json(schema_src : String, json_src : String) -> Unit raise CueError {
  let errs = validate_json(schema_src, json_src)
  if !errs.is_empty() {
    raise errs[0]
  }
}

///|
/// Unify a CUE schema with a JSON payload, apply defaults & computed fields, and export JSON.
pub fn export_unified_json(
  schema_src : String,
  json_src : String,
  indent~ : Int32 = 2,
) -> String raise CueError {
  let unified = unify_json(schema_src, json_src)
  format_json(value_to_json(unified), indent~)
}

///|
/// Validate a YAML payload against a CUE schema, returning all structured diagnostics.
pub fn validate_yaml(schema_src : String, yaml_src : String) -> Array[CueError] {
  let unified_res : Result[Value, CueError] = try {
    let schema_val = eval(schema_src)
    let data_val = parse_yaml_value(yaml_src)
    Ok(unify(schema_val, data_val))
  } catch {
    e => Err(e)
  }
  match unified_res {
    Err(e) => [e]
    Ok(val) => collect_validation_errors(val, require_concrete=true)
  }
}

///|
/// Validate a YAML payload against a CUE schema, raising the first `CueError` on failure.
pub fn vet_yaml(schema_src : String, yaml_src : String) -> Unit raise CueError {
  let errs = validate_yaml(schema_src, yaml_src)
  if !errs.is_empty() {
    raise errs[0]
  }
}