// box_type_inspector_box.hako — Runtime box type metadata helper
// Responsibility:
//   - Wrap env.box_introspect.* extern calls for Stage1 code.
//   - Provide describe/kind/is_map/is_array helpers without depending on `"" + MapBox`.

static box BoxTypeInspectorBox {
  method _describe(value) {
    local args = new ArrayBox()
    args.push(value)
    local info = hostbridge.extern_invoke("env.box_introspect", "kind", args)
    if env.get("HAKO_BOX_INTROSPECT_TRACE") == "1" {
      if info != null {
        print("[box_introspect:hako] kind=" + ("" + info.get("kind")) +
              " is_map=" + ("" + info.get("is_map")) +
              " is_array=" + ("" + info.get("is_array")))
      } else {
        print("[box_introspect:hako] info=null")
      }
    }
    return info
  }

  method kind(value) {
    local info = me._describe(value)
    if info == null { return "Unknown" }
    local k = info.get("kind")
    if k == null { return "Unknown" }
    return "" + k
  }

  method is_map(value) {
    local info = me._describe(value)
    if info != null {
      local flag = info.get("is_map")
      if flag != null { return flag }
    }
    // Fallback: detect MapBox from repr when extern is unavailable
    local repr = "" + value
    if repr.indexOf("MapBox(") == 0 { return true }
    return false
  }

  method is_array(value) {
    local info = me._describe(value)
    if info != null {
      local flag = info.get("is_array")
      if flag != null { return flag }
    }
    // Fallback: detect ArrayBox from repr when extern is unavailable
    local repr = "" + value
    if repr.indexOf("ArrayBox(") == 0 { return true }
    return false
  }

  method describe(value) {
    // Expose full Map so callers can inspect additional fields (type_name/type_id/etc.)
    return me._describe(value)
  }
}
