///|
#derive(Eq, Debug)
pub(all) struct Diagnostic {
  code : String
  offset : Int32
  message : String
}

///|
pub(all) suberror JmesPathError {
  JmesPathError(Diagnostic)
}

///|
fn[T] fail(
  code : String,
  offset : Int32,
  message : String,
) -> T raise JmesPathError {
  raise JmesPathError({ code, offset, message, })
}

///|
#derive(Eq, Debug)
pub(all) struct Limits {
  max_expression_chars : Int32
  max_ast_depth : Int32
  max_steps : Int32
  max_results : Int32
}

///|
pub fn default_limits() -> Limits {
  {
    max_expression_chars: 16384,
    max_ast_depth: 256,
    max_steps: 1000000,
    max_results: 100000,
  }
}

///|
#derive(Eq, Debug)
enum CompareOp {
  Eq
  Ne
  Lt
  Le
  Gt
  Ge
}

///|
#derive(Eq, Debug)
enum Expr {
  Current
  Field(String)
  Literal(Json)
  ContainerLiteral(Int32, Json)
  Subexpression(Expr, Expr)
  Index(Expr, Int32)
  Slice(Expr, Int32?, Int32?, Int32?)
  ListWildcard(Expr)
  ObjectWildcard(Expr)
  Flatten(Expr)
  Projection(Expr, Expr)
  Filter(Expr, Expr)
  Pipe(Expr, Expr)
  Or(Expr, Expr)
  And(Expr, Expr)
  Not(Expr)
  Compare(CompareOp, Expr, Expr)
  MultiList(Array[Expr])
  MultiHash(Array[(String, Expr)])
  Function(String, Array[Expr], Int32)
  ExpressionRef(Expr)
}

///|
pub struct Compiled {
  source : String
  expr : Expr
  limits : Limits
}

///|
pub fn Compiled::source(self : Compiled) -> String {
  self.source
}

///|
fn validate_limits(limits : Limits) -> Unit raise JmesPathError {
  if limits.max_expression_chars < 1 ||
    limits.max_ast_depth < 1 ||
    limits.max_ast_depth > 256 ||
    limits.max_steps < 1 ||
    limits.max_results < 0 {
    fail(
      "invalid_value", 0, "expression, depth, and step limits must be positive; AST depth must be at most 256; result limit must be nonnegative",
    )
  }
}

///|
pub fn version() -> String {
  "0.1.36"
}

///|
pub extend Diagnostic with Eq::{equal, not_equal}

///|
pub extend Diagnostic with @debug.Debug::{to_repr}

///|
pub extend Limits with Eq::{equal, not_equal}

///|
pub extend Limits with @debug.Debug::{to_repr}

///|
pub extend CompareOp with Eq::{equal, not_equal}

///|
pub extend CompareOp with @debug.Debug::{to_repr}

///|
pub extend Expr with Eq::{equal, not_equal}

///|
pub extend Expr with @debug.Debug::{to_repr}