// MirBuilderBox — minimal Ny→MIR(JSON v0) lowering entry (dev)
// Phase 15.7: kept optional; pipeline.v2 uses Emit flow directly.
using lang.compiler.pipeline_v2.stage1_extract_flow as Stage1ExtractFlow
using lang.compiler.pipeline_v2.emit_return_box as EmitReturnBox
using lang.compiler.pipeline_v2.emit_binop_box as EmitBinopBox
using lang.compiler.pipeline_v2.emit_compare_box as EmitCompareBox
using selfhost.shared.common.box_helpers as BoxHelpers

box MirBuilderBox {
  // Placeholder for optimizer toggle
  optimize_flag
  birth() { me.optimize_flag = 0 return 0 }
  set_optimize(v) { if v == null { v = 0 } me.optimize_flag = v return 0 }

  // Accept Stage‑1 AST JSON and emit minimal MIR(JSON v0)
  build(ast_json) {
    if ast_json == null { return EmitReturnBox.emit_return_int2(0, 0) }
    // If(cond=Compare) → CFG (branch/jump/ret)
    if ast_json.indexOf("\"type\":\"If\"") >= 0 {
      local ic = Stage1ExtractFlow.extract_if_compare(ast_json)
      if ic != null { return EmitCompareBox.emit_compare_cfg3(BoxHelpers.map_get(ic, "lhs"), BoxHelpers.map_get(ic, "rhs"), BoxHelpers.map_get(ic, "cmp"), 0, 0) }
    }
    // Return(Compare)
    local c = Stage1ExtractFlow.extract_return_compare(ast_json)
    if c != null { return EmitCompareBox.emit_compare_cfg3(BoxHelpers.map_get(c, "lhs"), BoxHelpers.map_get(c, "rhs"), BoxHelpers.map_get(c, "cmp"), 0, 0) }
    // Return(BinOp)
    local b = Stage1ExtractFlow.extract_return_binop(ast_json)
    if b != null { return EmitBinopBox.emit_binop2(BoxHelpers.map_get(b, "lhs"), BoxHelpers.map_get(b, "rhs"), BoxHelpers.map_get(b, "kind"), 0) }
    // Fallback: Return(Int)
    local v = Stage1ExtractFlow.extract_return_int(ast_json)
    return EmitReturnBox.emit_return_int2(v, 0)
  }
}

static box MirBuilderStub { main(args) { return 0 } }
