using System;
using Syntaxiom.Symbols;

namespace Syntaxiom.Nodes
{
    public class ConditionalBlockNode : BranchNode
    {
        private readonly ISymbol _condition;

        public ConditionalBlockNode(ISymbol condition)
        {
            if (condition == null)
                throw new ArgumentNullException(nameof(condition));

            _condition = condition;
        }

        public override void Evaluate(IEvaluationContext context, IOutput output)
        {
            if (!_condition.ResolveTruthiness(context))
                return;

            base.Evaluate(context, output);

            if (context.ExitCode < ExitCode.ClosestBlock)
                context.ExitCode = ExitCode.ClosestBlock;
        }
    }
}