using System.Text.Json.Nodes;
using w4c_workflows.Models.Nodes;
namespace w4c_workflows.Services.Nodes.Executors;
///
/// Conditional branch node. Evaluates the configured comparison once per item
/// and routes the item to output 0 (true) or output 1 (false). The comparison
/// itself lives in so every branch node
/// (if / filter / switch) behaves identically.
///
public sealed class IfNodeExecutor : INodeExecutor
{
public string Type => "core.if";
public Task RunAsync(NodeExecutionContext context, CancellationToken ct)
{
var op = context.Parameters["operator"] is JsonValue value && value.TryGetValue(out var text)
? text
: NodeConditionEvaluator.DefaultOperator;
var caseSensitive = context.Parameters["caseSensitive"] is JsonValue flag && flag.TryGetValue(out var cs) && cs;
var left = context.Parameters["left"];
var right = context.Parameters["right"];
bool matched;
try
{
matched = NodeConditionEvaluator.Evaluate(left, op, right, caseSensitive);
}
catch (ArgumentException ex)
{
return Task.FromResult(NodeExecutionOutcome.Failed(ex.Message, "invalid_condition"));
}
var trueItems = new List();
var falseItems = new List();
foreach (var item in context.Input(0))
(matched ? trueItems : falseItems).Add(item);
return Task.FromResult(new NodeExecutionOutcome
{
Outputs = new List> { trueItems, falseItems },
});
}
}