// blocks_locator.hako — BlocksLocatorBox
// Responsibility: locate blocks[] array in function JSON

using selfhost.vm.boxes.result_box as Result
using selfhost.shared.json.core.json_cursor as JsonCursorBox
using selfhost.vm.hakorune-vm.json_scan_guard as JsonScanGuardBox

static box BlocksLocatorBox {
  locate(func_json) {
    // Find key "blocks"
    local kpos = JsonCursorBox.index_of_from(func_json, r#""blocks""#, 0)
    if kpos < 0 { return Result.Err("blocks key not found") }
    // find ':' then '['
    local i = kpos + r#""blocks""#.length()
    loop(i < func_json.length()) {
      local ch = func_json.substring(i, i+1)
      if ch == ":" { i = i + 1  break }
      if ch == " " || ch == "\n" || ch == "\r" || ch == "\t" { i = i + 1  continue }
      return Result.Err("blocks key format invalid")
    }
    loop(i < func_json.length()) {
      local ch = func_json.substring(i, i+1)
      if ch == " " || ch == "\n" || ch == "\r" || ch == "\t" { i = i + 1  continue }
      if ch == "[" { break }
      return Result.Err("blocks array not found")
    }
    local lbr = i
    local arr_end = JsonScanGuardBox.seek_array_end(func_json, lbr, 200000)
    if arr_end < 0 { return Result.Err("blocks array end not found") }
    // exclude brackets
    local content = func_json.substring(lbr+1, arr_end)
    local meta = new MapBox()
    meta.set("start", lbr+1)
    meta.set("end", arr_end)
    meta.set("content", content)
    return Result.Ok(meta)
  }
}
