using System;
using Syntaxiom.Symbols;

namespace Syntaxiom.Nodes
{
    public class AssignLocalValue : LeafNode
    {
        private readonly ISymbol _from;
        private readonly string _to;

        public AssignLocalValue(ISymbol from, string toVariableName)
        {
            if (string.IsNullOrWhiteSpace(toVariableName))
                throw new ArgumentException("toVariableName must be a valid identifier");

            _from = from;
            _to = toVariableName;
        }

        public override void Evaluate(IEvaluationContext context, IOutput output)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));

            if (output == null)
                throw new ArgumentNullException(nameof(output));

            var from = _from?.ResolveReference(context);

            context.CurrentScope.SetValue(_to, from);
        }
    }
}