using System.Linq.Expressions;

namespace DateTimeFiltering
{
    public class PredicateRewriter : ExpressionVisitor
    {
        private readonly ParameterExpression newParameter;

        private PredicateRewriter(ParameterExpression newParameter)
        {
            this.newParameter = newParameter;
        }

        protected override Expression VisitParameter(ParameterExpression p)
        {
            return this.newParameter;
        }

        public static Expression Rewrite(LambdaExpression lambda, ParameterExpression newParameter)
        {
            var rewriter = new PredicateRewriter(newParameter);
            return rewriter.Visit(lambda.Body);
        }
    }
}