// Moved from apps/selfhost-compiler/builder/ssa/local.hako — LocalSSA minimal（安全：挙動不変）
// 目的:
// - Stage‑1 JSON / MIR(JSON v0) のうち、既知の最小パターンで
//   「ブロック内にオペランドが材化されている（LocalSSA）」ことを軽く検証・整形する。
// - 既定では再配線（Copy挿入）等は行わず、将来の拡張に備えた安全な足場のみを提供する。

using selfhost.shared.common.string_helpers as StringHelpers
static box LocalSSA {
  // --- 内部ヘルパ ---
  // trace fields are created on demand (dynamic properties)
  _index_of(hay, needle) { return hay.indexOf(needle) }
  _last_index_of(hay, needle) { return hay.lastIndexOf(needle) }
  _index_of_from(hay, needle, pos) {
    // Delegate to StringHelpers with adjusted argument order
    return StringHelpers.index_of(hay, pos, needle)
  }
  _read_digits(text, pos) { return StringHelpers.read_digits(text, pos) }
  _count_occurs(hay, needle) {
    if hay == null { return 0 }
    if needle == null { return 0 }
    local n = 0
    local i = 0
    loop(true) {
      local p = hay.indexOf(needle)
      if p < 0 { break }
      n = n + 1
      hay = hay.substring(p + needle.length(), hay.length())
    }
    return n
  }
  _to_int(digits) {
    // Delegate to StringHelpers (handles digits-only strings correctly)
    return StringHelpers.to_i64(digits)
  }
  _seek_obj_start(text, from_pos) {
    local i = from_pos
    local start = from_pos
    loop(i >= 0) {
      local ch = text.substring(i, i+1)
      if ch == "{" { start = i  break }
      i = i - 1
    }
    return start
  }
  _seek_obj_end(text, obj_start) {
    local i = obj_start
    local depth = 0
    loop(true) {
      local ch = text.substring(i, i+1)
      if ch == "" { break }
      if ch == "{" { depth = depth + 1 } else { if ch == "}" { depth = depth - 1 } }
      if depth == 0 { return i + 1 }
      i = i + 1
    }
    return i
  }
  _block_insts_start(mjson, before_pos) {
    // find the nearest instructions array start that encloses before_pos
    local prefix = mjson.substring(0, before_pos)
    local p = prefix.lastIndexOf("\"instructions\":[")
    if p < 0 { return -1 }
    return p + 16 // len of "\"instructions\":["
  }
  _block_insts_end(mjson, insts_start) {
    // matching bracket for this instructions array
    local i = insts_start
    local depth = 1
    loop(true) {
      local ch = mjson.substring(i, i+1)
      if ch == "" { break }
      if ch == "[" { depth = depth + 1 } else { if ch == "]" { depth = depth - 1 } }
      if depth == 0 { return i }
      i = i + 1
    }
    return i
  }
  _max_dst_id(mjson) {
    local pos = 0
    local maxv = 0
    loop(true) {
      local p = me._index_of_from(mjson, "\"dst\":", pos)
      if p < 0 { break }
      local digits = me._read_digits(mjson, p + 6)
      if digits != "" {
        local acc = me._to_int(digits)
        if acc > maxv { maxv = acc }
      }
      pos = p + 6
    }
    return maxv
  }
  // --- 公開API（安全No-Opルール） ---
  ensure_recv(stage1_json) { return stage1_json }
  ensure_args(stage1_json) { return stage1_json }
  ensure_cmp(stage1_json) { return stage1_json }
  ensure_cond(stage1_json) { return stage1_json }
  ensure_calls(stage1_json) { return stage1_json }
}
