use crate::ast::ASTNode;
use crate::mir::join_ir::lowering::loop_scope_shape::LoopScopeShape;
use crate::mir::loop_pattern_detection::error_messages::{
    extract_body_local_names, format_unsupported_condition_error,
};
use crate::mir::loop_pattern_detection::loop_condition_scope::{LoopConditionScope, LoopConditionScopeBox};

pub(crate) fn validate_condition_scope(
    scope: &LoopScopeShape,
    condition: &ASTNode,
    loop_var_name: &str,
) -> Result<LoopConditionScope, String> {
    let loop_cond_scope = LoopConditionScopeBox::analyze(loop_var_name, &[condition], Some(scope));

    if loop_cond_scope.has_loop_body_local() {
        let body_local_names = extract_body_local_names(&loop_cond_scope.vars);
        return Err(format_unsupported_condition_error(
            "pattern4",
            &body_local_names,
        ));
    }

    Ok(loop_cond_scope)
}
