// lower_if_compare_varvar_box.hako — If(Compare Var vs Var) with prior Local Int bindings
// Pattern:
//   Local a=Int A; Local b=Int B;
//   If(cond=Compare(op, lhs=Var a, rhs=Var b)) { Return(Int X) } else { Return(Int Y) }
// Lowers to const A/B → compare(op) → branch → ret X/Y

using "hako.mir.builder.internal.pattern_util" as PatternUtilBox
using selfhost.shared.json.utils.json_frag as JsonFragBox

static box LowerIfCompareVarVarBox {
  try_lower(program_json) {
    local s = "" + program_json
    local k_if = JsonFragBox.index_of_from(s, "\"type\":\"If\"", 0)
    if k_if < 0 { return null }
    local k_cmp = JsonFragBox.index_of_from(s, "\"type\":\"Compare\"", k_if)
    if k_cmp < 0 { return null }
    // LHS/RHS Var names
    local lhs_name = null
    local klhs = JsonFragBox.index_of_from(s, "\"lhs\":{\"type\":\"Var\"", k_cmp)
    if klhs >= 0 {
      local k_name_lhs = JsonFragBox.index_of_from(s, "\"name\":", klhs); if k_name_lhs < 0 { return null }
      lhs_name = JsonFragBox.read_string_after(s, k_name_lhs + 7)
    }
    local rhs_name = null
    local krhs = JsonFragBox.index_of_from(s, "\"rhs\":{\"type\":\"Var\"", k_cmp)
    if krhs >= 0 {
      local k_name_rhs = JsonFragBox.index_of_from(s, "\"name\":", krhs); if k_name_rhs < 0 { return null }
      rhs_name = JsonFragBox.read_string_after(s, k_name_rhs + 7)
    }
    if lhs_name == null || rhs_name == null { return null }
    // Resolve locals
    local aval = PatternUtilBox.find_local_int_before(s, lhs_name, k_if)
    local bval = PatternUtilBox.find_local_int_before(s, rhs_name, k_if)
    if aval == null || bval == null { return null }
    // op map
    local k_op = JsonFragBox.index_of_from(s, "\"op\":", k_cmp); if k_op < 0 { return null }
    local sym = JsonFragBox.read_string_after(s, k_op + 5)
    if sym == null { return null }
    // Keep original operator symbol for JSON emission
    if sym == null { return null }
    // then/else Return(Int) — boundary limited
    local tb = PatternUtilBox.then_array_bounds(s, k_if); if tb == null { return null }
    local cp = tb.indexOf(":"); if cp < 0 { return null }
    local lb_then = JsonFragBox._str_to_int(tb.substring(0, cp))
    local rb_then = JsonFragBox._str_to_int(tb.substring(cp + 1, tb.length()))
    local eb = PatternUtilBox.else_array_bounds_after_then(s, k_if); if eb == null { return null }
    local cp2 = eb.indexOf(":"); if cp2 < 0 { return null }
    local lb_else = JsonFragBox._str_to_int(eb.substring(0, cp2))
    local rb_else = JsonFragBox._str_to_int(eb.substring(cp2 + 1, eb.length()))
    // then within bounds
    local rt1 = JsonFragBox.index_of_from(s, "\"type\":\"Return\"", lb_then); if rt1 < 0 || rt1 >= rb_then { return null }
    local ti1 = JsonFragBox.index_of_from(s, "\"type\":\"Int\"", rt1); if ti1 < 0 || ti1 >= rb_then { return null }
    local kv_then = JsonFragBox.index_of_from(s, "\"value\":", ti1); if kv_then < 0 || kv_then >= rb_then { return null }
    local then_val = JsonFragBox.read_int_after(s, kv_then + 8); if then_val == null { return null }
    // else within bounds
    local rt2 = JsonFragBox.index_of_from(s, "\"type\":\"Return\"", lb_else); if rt2 < 0 || rt2 >= rb_else { return null }
    local ti2 = JsonFragBox.index_of_from(s, "\"type\":\"Int\"", rt2); if ti2 < 0 || ti2 >= rb_else { return null }
    local kv_else = JsonFragBox.index_of_from(s, "\"value\":", ti2); if kv_else < 0 || kv_else >= rb_else { return null }
    local else_val = JsonFragBox.read_int_after(s, kv_else + 8); if else_val == null { return null }

    // Emit MIR(JSON v0) as string
    local mir = "{\"functions\":[{\"name\":\"main\",\"params\":[],\"locals\":[],\"blocks\":[" +
      "{\"id\":0,\"instructions\":[{\"op\":\"const\",\"dst\":1,\"value\":{\"type\":\"i64\",\"value\":" + aval + "}}," +
      "{\"op\":\"const\",\"dst\":2,\"value\":{\"type\":\"i64\",\"value\":" + bval + "}}," +
      "{\"op\":\"compare\",\"operation\":\"" + sym + "\",\"lhs\":1,\"rhs\":2,\"dst\":3}," +
      "{\"op\":\"branch\",\"cond\":3,\"then\":1,\"else\":2}]}," +
      "{\"id\":1,\"instructions\":[{\"op\":\"const\",\"dst\":4,\"value\":{\"type\":\"i64\",\"value\":" + then_val + "}},{\"op\":\"ret\",\"value\":4}]}," +
      "{\"id\":2,\"instructions\":[{\"op\":\"const\",\"dst\":5,\"value\":{\"type\":\"i64\",\"value\":" + else_val + "}},{\"op\":\"ret\",\"value\":5}]}]}]}"
    return mir
  }
}
