// Copyright (c) Microsoft. All rights reserved.
using Microsoft.PowerFx;
using Microsoft.PowerFx.Types;
namespace Microsoft.Agents.ObjectModel;
///
/// Extension methods for .
///
internal static class BoolExpressionExtensions
{
///
/// Evaluates the given using the provided .
///
/// Expression to evaluate.
/// Recalc engine to use for evaluation.
/// The evaluated boolean value, or null if the expression is null or cannot be evaluated.
internal static bool? Eval(this BoolExpression? expression, RecalcEngine? engine)
{
if (expression is null)
{
return null;
}
if (expression.IsLiteral)
{
return expression.LiteralValue;
}
if (engine is null)
{
return null;
}
if (expression.IsExpression)
{
return engine.Eval(expression.ExpressionText!).AsBoolean();
}
else if (expression.IsVariableReference)
{
var formulaValue = engine.Eval(expression.VariableReference!.VariableName);
if (formulaValue is BooleanValue booleanValue)
{
return booleanValue.Value;
}
if (formulaValue is StringValue stringValue && bool.TryParse(stringValue.Value, out bool result))
{
return result;
}
}
return null;
}
}