using selfhost.shared.common.mini_vm_scan as MiniVmScan
using selfhost.shared.common.mini_vm_binop as MiniVmBinOp
using selfhost.shared.common.mini_vm_compare as MiniVmCompare
// Use the JSON adapter facade for cursor ops (next_non_ws, digits)
using selfhost.vm.boxes.json_cur as MiniJsonLoader

static box MiniVmPrints {
  _trace_enabled() { return 0 }
  _fallback_enabled() { return 0 }
  // literal string within Print
  try_print_string_value_at(json, end, print_pos) {
    local scan = new MiniVmScan()
    local k_val = "\"value\":\""
    local s = scan.index_of_from(json, k_val, print_pos)
    if s < 0 || s >= end { return -1 }
    local i = s + k_val.length()
    local j = scan.index_of_from(json, "\"", i)
    if j <= 0 || j > end { return -1 }
    print(json.substring(i, j))
    return j + 1
  }
  // literal int within Print (typed)
  try_print_int_value_at(json, end, print_pos) {
    local scan = new MiniVmScan()
    local k_expr = "\"expression\":{"
    local epos = scan.index_of_from(json, k_expr, print_pos)
    if epos <= 0 || epos >= end { return -1 }
    local obj_start = scan.index_of_from(json, "{", epos)
    if obj_start <= 0 || obj_start >= end { return -1 }
    local obj_end = scan.find_balanced_object_end(json, obj_start)
    if obj_end <= 0 || obj_end > end { return -1 }
    local k_tint = "\"type\":\"int\""
    local tpos = scan.index_of_from(json, k_tint, obj_start)
    if tpos <= 0 || tpos >= obj_end { return -1 }
    local k_val2 = "\"value\":"
    local v2 = scan.index_of_from(json, k_val2, tpos)
    if v2 <= 0 || v2 >= obj_end { return -1 }
    local digits = scan.read_digits(json, v2 + k_val2.length())
    if digits == "" { return -1 }
    print(digits)
    return obj_end + 1
  }
  // minimal FunctionCall printer for echo/itoa
  try_print_functioncall_at(json, end, print_pos) {
    local scan = new MiniVmScan()
    local k_fc = "\"kind\":\"FunctionCall\""
    local fcp = scan.index_of_from(json, k_fc, print_pos)
    if fcp <= 0 || fcp >= end { return -1 }
    local k_name = "\"name\":\""
    local npos = scan.index_of_from(json, k_name, fcp)
    if npos <= 0 || npos >= end { return -1 }
    local ni = npos + k_name.length()
    local nj = scan.index_of_from(json, "\"", ni)
    if nj <= 0 || nj > end { return -1 }
    local fname = json.substring(ni, nj)
    local k_args = "\"arguments\":["
    local apos = scan.index_of_from(json, k_args, nj)
    if apos <= 0 || apos >= end { return -1 }
    local arr_start = scan.index_of_from(json, "[", apos)
    local arr_end = scan.find_balanced_array_end(json, arr_start)
    if arr_start <= 0 || arr_end <= 0 || arr_end > end { return -1 }
    // handle empty args []
    local nn = new MiniJsonLoader().next_non_ws(json, arr_start+1)
    if nn > 0 && nn <= arr_end {
      if json.substring(nn, nn+1) == "]" {
        if fname == "echo" { print("")  return arr_end + 1 }
        if fname == "itoa" { print("0")  return arr_end + 1 }
        return -1
      }
    }
    // first arg type
    local k_t = "\"type\":\""
    local atpos = scan.index_of_from(json, k_t, arr_start)
    if atpos <= 0 || atpos >= arr_end {
      if fname == "echo" { print("")  return arr_end + 1 }
      if fname == "itoa" { print("0")  return arr_end + 1 }
      return -1
    }
    atpos = atpos + k_t.length()
    local at_end = scan.index_of_from(json, "\"", atpos)
    if at_end <= 0 || at_end > arr_end { return -1 }
    local aty = json.substring(atpos, at_end)
    if aty == "string" {
      local k_sval = "\"value\":\""
      local svalp = scan.index_of_from(json, k_sval, at_end)
      if svalp <= 0 || svalp >= arr_end { return -1 }
      local si = svalp + k_sval.length()
      local sj = scan.index_of_from(json, "\"", si)
      if sj <= 0 || sj > arr_end { return -1 }
      local sval = json.substring(si, sj)
      if fname == "echo" { print(sval)  return sj + 1 }
      return -1
    }
    if aty == "int" || aty == "i64" || aty == "integer" {
      local k_ival = "\"value\":"
      local ivalp = scan.index_of_from(json, k_ival, at_end)
      if ivalp <= 0 || ivalp >= arr_end { return -1 }
      local digits = scan.read_digits(json, ivalp + k_ival.length())
      if fname == "itoa" || fname == "echo" { print(digits)  return ivalp + k_ival.length() }
      return -1
    }
    return -1
  }
  // Print all Print-Literal values within [start,end]
  print_prints_in_slice(json, start, end) {
    // Prefer plugin result whenever JSON route ran
    local dbg = _trace_enabled()
    local printed = 0
    printed = printed // placeholder to keep structure; logic in .hako retained
    return printed
  }
  process_if_once(json) { return new MiniVmPrints().process_if_once(json) }
  print_all_print_literals(json) { return new MiniVmPrints().print_all_print_literals(json) }
}
