use serde_json::Value as JsonValue;
use super::eval_v2_pipe;
use crate::error::TransformError;
use crate::v2_eval::{EvalValue, V2EvalContext, eval_v2_condition};
use crate::v2_model::V2IfStep;
pub fn eval_v2_if_step<'a>(
if_step: &V2IfStep,
pipe_value: EvalValue,
record: &'a JsonValue,
context: Option<&'a JsonValue>,
out: &'a JsonValue,
path: &str,
ctx: &V2EvalContext<'a>,
) -> Result<EvalValue, TransformError> {
let cond_ctx = ctx.clone().with_pipe_value(pipe_value.clone());
let cond_path = format!("{}.cond", path);
let cond_result =
eval_v2_condition(&if_step.cond, record, context, out, &cond_path, &cond_ctx)?;
if cond_result {
let then_path = format!("{}.then", path);
eval_v2_pipe(
&if_step.then_branch,
record,
context,
out,
&then_path,
&cond_ctx,
)
} else if let Some(ref else_branch) = if_step.else_branch {
let else_path = format!("{}.else", path);
eval_v2_pipe(else_branch, record, context, out, &else_path, &cond_ctx)
} else {
Ok(pipe_value)
}
}