///|
/// Tokenizes a CUE source string using the `syntax` package lexer.
pub fn tokenize(
  source : String,
) -> Result[Array[@syntax.Token], @syntax.LexError] {
  @syntax.tokenize(source)
}

///|
/// Parses a complete CUE source file into an AST using the `syntax` package parser.
pub fn parse(source : String) -> Result[@syntax.File, @syntax.SyntaxError] {
  @syntax.parse_file(source)
}

///|
/// Parses a standalone CUE expression using the `syntax` package parser.
pub fn parse_expr(source : String) -> Result[@syntax.Expr, @syntax.SyntaxError] {
  @syntax.parse_expr(source)
}

///|
/// Parses and formats a CUE source file into canonical `cue fmt`-style text.
pub fn format(source : String) -> Result[String, @syntax.SyntaxError] {
  @syntax.format(source)
}

///|
/// Evaluates a single CUE source string into a lattice `@eval.Value`.
///
/// Uses the unified standard library package registry (`@builtins.default_eval_config`).
/// Returns `Err(errs)` if any `Bottom` errors or determined builtin constraint
/// failures are present anywhere in the evaluated value tree; incomplete
/// non-concrete schemas succeed as `Ok(val)`.
pub fn eval(
  source : String,
  limits? : @eval.EvalLimits,
  tags~ : Map[String, String?] = Map([]),
) -> Result[@eval.Value, Array[@eval.CueError]] {
  let cfg = @builtins.default_eval_config(limits?, tags~)
  let val = @eval.eval_source_with_config(source, cfg)
  let budget = @eval.EvalBudget::new(cfg.limits)
  let checked = @eval.validate_determined_constraints(
    val,
    budget,
    validator?=cfg.validator_resolver,
  )
  let errs = @eval.collect_errors(checked)
  if errs.is_empty() {
    Ok(checked)
  } else {
    Err(errs)
  }
}

///|
/// Evaluates and unifies multiple CUE source strings in the same package scope.
///
/// Uses the unified standard library package registry (`@builtins.default_eval_config`).
/// Returns `Err(errs)` if any `Bottom` errors or determined builtin constraint
/// failures are present anywhere in the evaluated value tree.
pub fn eval_all(
  sources : Array[String],
  limits? : @eval.EvalLimits,
  tags~ : Map[String, String?] = Map([]),
) -> Result[@eval.Value, Array[@eval.CueError]] {
  let cfg = @builtins.default_eval_config(limits?, tags~)
  let val = @eval.eval_sources_with_config(sources, cfg)
  let budget = @eval.EvalBudget::new(cfg.limits)
  let checked = @eval.validate_determined_constraints(
    val,
    budget,
    validator?=cfg.validator_resolver,
  )
  let errs = @eval.collect_errors(checked)
  if errs.is_empty() {
    Ok(checked)
  } else {
    Err(errs)
  }
}

///|
/// Validates CUE `data` against a CUE `schema` and verifies concreteness.
pub fn vet(
  schema : String,
  data : String,
  def_name? : String,
  limits? : @eval.EvalLimits,
  tags~ : Map[String, String?] = Map([]),
) -> Result[@eval.Value, Array[@eval.CueError]] {
  let cfg = @builtins.default_eval_config(limits?, tags~)
  @codec.vet_with_config(schema, data, cfg, def_name?)
}

///|
/// Validates strict RFC 8259 `json_data` against a CUE `schema` and verifies concreteness.
pub fn vet_json(
  schema : String,
  json_data : String,
  def_name? : String,
  limits? : @eval.EvalLimits,
  tags~ : Map[String, String?] = Map([]),
) -> Result[@eval.Value, Array[@eval.CueError]] {
  let cfg = @builtins.default_eval_config(limits?, tags~)
  @codec.vet_json_with_config(schema, json_data, cfg, def_name?)
}

///|
/// Validates YAML 1.2 Core Schema `yaml_data` against a CUE `schema` and
/// verifies concreteness.
pub fn vet_yaml(
  schema : String,
  yaml_data : String,
  def_name? : String,
  limits? : @eval.EvalLimits,
  tags~ : Map[String, String?] = Map([]),
) -> Result[@eval.Value, Array[@eval.CueError]] {
  let cfg = @builtins.default_eval_config(limits?, tags~)
  @codec.vet_yaml_with_config(schema, yaml_data, cfg, def_name?)
}

///|
/// Validates strict JSON or YAML `data` directly against a JSON Schema document
/// (`Draft-07` or `Draft 2020-12`) with the full CueSmith standard library.
pub fn vet_json_schema(
  schema_json : String,
  data : String,
  def_name~ : String = "#Schema",
  is_yaml_data~ : Bool = false,
  limits? : @eval.EvalLimits,
  tags~ : Map[String, String?] = Map([]),
) -> Result[@eval.Value, Array[@eval.CueError]] {
  let cfg = @builtins.default_eval_config(limits?, tags~)
  @codec.vet_json_schema_with_config(
    schema_json,
    data,
    cfg,
    def_name~,
    is_yaml_data~,
  )
}

///|
/// Evaluates and unifies one or more CUE source strings and exports deterministic JSON.
pub fn export_json(
  sources : Array[String],
  limits? : @eval.EvalLimits,
  indent? : Int32,
  tags~ : Map[String, String?] = Map([]),
) -> Result[String, Array[@eval.CueError]] {
  let cfg = @builtins.default_eval_config(limits?, tags~)
  @codec.export_json_with_config(sources, cfg, indent?)
}

///|
/// Evaluates and unifies one or more CUE source strings and exports deterministic
/// YAML 1.2 Core Schema text.
pub fn export_yaml(
  sources : Array[String],
  limits? : @eval.EvalLimits,
  tags~ : Map[String, String?] = Map([]),
) -> Result[String, Array[@eval.CueError]] {
  let cfg = @builtins.default_eval_config(limits?, tags~)
  @codec.export_yaml_with_config(sources, cfg)
}

///|
/// Formats an evaluated `@eval.Value` into canonical CUE text.
pub fn format_value(val : @eval.Value) -> String {
  @codec.format_value(val)
}

///|
/// Imports a JSON Schema (Draft-07 or Draft 2020-12) document into formatted,
/// self-contained CUE source defining `def_name` (default `"#Schema"`).
pub fn import_json_schema(
  schema_json : String,
  def_name~ : String = "#Schema",
  limits? : @eval.EvalLimits,
) -> Result[String, Array[@eval.CueError]] {
  @codec.import_json_schema(schema_json, def_name~, limits?)
}

///|
/// Evaluates `cue_source` using the unified standard library package registry
/// (`@builtins.default_eval_config`) and exports a deterministic JSON Schema
/// Draft 2020-12 document.
pub fn export_json_schema(
  cue_source : String,
  def_name? : String,
  limits? : @eval.EvalLimits,
  indent? : Int32,
) -> Result[String, Array[@eval.CueError]] {
  let cfg = @builtins.default_eval_config(limits?)
  @codec.export_json_schema_with_config(cue_source, cfg, def_name?, indent?)
}

///|
/// Resolves and evaluates a CUE package against a sandboxed `@modsys.ModuleFs`.
pub fn eval_module_package(
  fs : @modsys.ModuleFs,
  target_dir : String,
  pkg_name? : String,
  expr? : @syntax.Expr,
  cwd~ : String = ".",
  limits? : @modsys.ModLimits,
) -> Result[@eval.Value, Array[@modsys.ModError]] {
  @modsys.eval_package(fs, target_dir, pkg_name?, expr?, cwd~, limits?)
}

///|
/// Resolves and evaluates a CUE package from an in-memory `(path, source)`
/// file list, supporting `cue.mod/module.cue`, cross-package imports, and
/// stdlib packages.
pub fn eval_memory_module(
  files : Array[(String, String)],
  target_dir~ : String = ".",
  pkg_name? : String,
  expr? : @syntax.Expr,
  limits? : @modsys.ModLimits,
) -> Result[@eval.Value, Array[@modsys.ModError]] {
  let fs = @modsys.MemoryFs::from_files(files).to_module_fs()
  @modsys.eval_package(fs, target_dir, pkg_name?, expr?, limits?)
}

///|
/// Checks CUE lattice subsumption `b ⊑ a` ("`a` subsumes `b`") with the
/// unified standard library validator attached.
pub fn subsumes(
  a : @eval.Value,
  b : @eval.Value,
  limits? : @eval.EvalLimits,
  final_mode~ : Bool = false,
  ignore_closed~ : Bool = false,
) -> Result[Bool, @eval.CueError] {
  let cfg = @builtins.default_eval_config(limits?)
  let budget = @eval.EvalBudget::new(cfg.limits)
  let opts : @eval.SubsumesOptions = { final_mode, ignore_closed, }
  @eval.subsumes_with_options(
    a,
    b,
    budget,
    options=opts,
    validator?=cfg.validator_resolver,
  )
}

///|
/// Trims redundant concrete fields from a single CUE source string while
/// verifying semantic equivalence before and after re-parsing.
pub fn trim(
  source : String,
  path? : String,
  options? : @trim.TrimOptions,
) -> Result[@trim.TrimFileResult, Array[@trim.TrimError]] {
  let eff_path = path.unwrap_or("")
  let eff_opts = options.unwrap_or(@trim.TrimOptions::default())
  @trim.trim_source(source, path=eff_path, options=eff_opts)
}

///|
/// Trims redundant concrete fields from `data` against a read-only `schema`
/// while verifying semantic equivalence before and after re-parsing.
pub fn trim_against_schema(
  schema : String,
  data : String,
  data_path? : String,
  options? : @trim.TrimOptions,
) -> Result[@trim.TrimFileResult, Array[@trim.TrimError]] {
  let eff_path = data_path.unwrap_or("")
  let eff_opts = options.unwrap_or(@trim.TrimOptions::default())
  @trim.trim_against_schema(schema, data, data_path=eff_path, options=eff_opts)
}

///|
/// Imports `components.schemas` from an OpenAPI 3.0 or 3.1 JSON/YAML document
/// into formatted CUE definitions (`#: ...`), or a single named schema
/// when `schema_name` is `Some`.
pub fn import_openapi_schemas(
  source : String,
  schema_name? : String,
  limits? : @eval.EvalLimits,
) -> Result[String, Array[@eval.CueError]] {
  @codec.import_openapi_schemas(source, schema_name?, limits?)
}

///|
/// Compares two schema sources (`CUE`, `JSON Schema`, or `OpenAPI 3.0/3.1`)
/// for backward compatibility and returns a detailed `@compat.CompatReport`.
pub fn diff_schema(
  old_source : String,
  new_source : String,
  options? : @compat.DiffOptions,
) -> Result[@compat.CompatReport, Array[@eval.CueError]] {
  let eff_opts = options.unwrap_or(@compat.DiffOptions::default())
  @compat.diff_schemas(old_source, new_source, options=eff_opts)
}

///|
/// Compares two evaluated `@eval.Value` schemas for backward compatibility and
/// returns a detailed `@compat.CompatReport`.
pub fn diff_schema_values(
  old_val : @eval.Value,
  new_val : @eval.Value,
  old_label? : String,
  new_label? : String,
  limits? : @eval.EvalLimits,
) -> Result[@compat.CompatReport, Array[@eval.CueError]] {
  let eff_old = old_label.unwrap_or("")
  let eff_new = new_label.unwrap_or("")
  @compat.diff_values(
    old_val,
    new_val,
    old_label=eff_old,
    new_label=eff_new,
    limits?,
  )
}

///|
/// Formats a `@compat.CompatReport` as a human-readable text report.
pub fn format_compat_text(report : @compat.CompatReport) -> String {
  @compat.format_text(report)
}

///|
/// Formats a `@compat.CompatReport` as deterministic RFC 8259 JSON (default indent 2).
pub fn format_compat_json(
  report : @compat.CompatReport,
  indent? : Int32,
) -> String {
  let eff_indent = indent.unwrap_or(2)
  @compat.format_json(report, indent=eff_indent)
}

///|
/// Formats a `@compat.CompatReport` as a GitHub-Flavored Markdown PR summary report.
pub fn format_compat_markdown(report : @compat.CompatReport) -> String {
  @compat.format_markdown(report)
}

///|
/// Formats a `@compat.CompatReport` as SARIF 2.1.0 JSON (default indent 2).
pub fn format_compat_sarif(
  report : @compat.CompatReport,
  indent? : Int32,
) -> String {
  let eff_indent = indent.unwrap_or(2)
  @compat.format_sarif(report, indent=eff_indent)
}

///|
/// Resolves a CUE package against a sandboxed `@modsys.ModuleFs` and builds its
/// deterministic dependency graph (`@modsys.PackageDepGraph`).
pub fn dep_graph_package(
  fs : @modsys.ModuleFs,
  target_dir : String,
  pkg_name? : String,
  cwd~ : String = ".",
  include_stdlib~ : Bool = false,
  limits? : @modsys.ModLimits,
) -> Result[@modsys.PackageDepGraph, Array[@modsys.ModError]] {
  match @modsys.resolve_package(fs, target_dir, pkg_name?, cwd~, limits?) {
    Ok(resolved) => Ok(@modsys.build_dep_graph(resolved, include_stdlib~))
    Err(errs) => Err(errs)
  }
}

///|
/// Resolves a CUE package from in-memory `(path, source)` entries and builds its
/// deterministic dependency graph (`@modsys.PackageDepGraph`).
pub fn dep_graph_memory(
  files : Array[(String, String)],
  target_dir~ : String = ".",
  pkg_name? : String,
  include_stdlib~ : Bool = false,
  limits? : @modsys.ModLimits,
) -> Result[@modsys.PackageDepGraph, Array[@modsys.ModError]] {
  let fs = @modsys.MemoryFs::from_files(files).to_module_fs()
  dep_graph_package(fs, target_dir, pkg_name?, include_stdlib~, limits?)
}

///|
/// Formats a `@modsys.PackageDepGraph` as a Mermaid `graph TD` flowchart.
pub fn format_dep_graph_mermaid(graph : @modsys.PackageDepGraph) -> String {
  @modsys.format_graph_mermaid(graph)
}

///|
/// Formats a `@modsys.PackageDepGraph` as a Graphviz DOT `digraph cuesmith_deps`.
pub fn format_dep_graph_dot(graph : @modsys.PackageDepGraph) -> String {
  @modsys.format_graph_dot(graph)
}

///|
/// Formats a `@modsys.PackageDepGraph` as deterministic RFC 8259 JSON (default indent 2).
pub fn format_dep_graph_json(
  graph : @modsys.PackageDepGraph,
  indent? : Int32,
) -> String {
  let eff_indent = indent.unwrap_or(2)
  @modsys.format_graph_json(graph, indent=eff_indent)
}