use crate::mir::basic_block::EdgeArgs;
use crate::mir::builder::control_flow::edgecfg::api::{compose, EdgeStub, ExitKind, Frag};
use crate::mir::join_ir::lowering::inline_boundary::JumpArgsLayout;
use std::collections::BTreeMap;

pub(super) fn empty_args() -> EdgeArgs {
    EdgeArgs {
        layout: JumpArgsLayout::CarriersOnly,
        values: vec![],
    }
}

pub(super) fn normal_exit_frag(
    entry: crate::mir::BasicBlockId,
    args: &EdgeArgs,
) -> Frag {
    let mut exits = BTreeMap::new();
    exits.insert(
        ExitKind::Normal,
        vec![EdgeStub {
            from: entry,
            kind: ExitKind::Normal,
            target: None,
            args: args.clone(),
        }],
    );
    Frag {
        entry,
        exits,
        wires: vec![],
        branches: vec![],
    }
}

pub(super) fn build_body_if_frag(
    body_bb: crate::mir::BasicBlockId,
    cond: crate::mir::ValueId,
    then_bb: crate::mir::BasicBlockId,
    else_bb: crate::mir::BasicBlockId,
    step_bb: crate::mir::BasicBlockId,
    empty_args: &EdgeArgs,
) -> Frag {
    let then_frag = normal_exit_frag(then_bb, empty_args);
    let else_frag = normal_exit_frag(else_bb, empty_args);

    let step_frag = Frag {
        entry: step_bb,
        exits: BTreeMap::new(),
        wires: vec![],
        branches: vec![],
    };

    compose::if_(
        body_bb,
        cond,
        then_frag,
        empty_args.clone(),
        else_frag,
        empty_args.clone(),
        step_frag,
    )
}
