// Copyright (C) 2009-2024 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Denis Krjuchkov
// Created:    2009.11.06

using System.Collections.Generic;
using Xtensive.Core;

namespace Xtensive.Sql.Dml
{
  public class SqlDynamicFilter : SqlExpression
  {
    public object Id { get; private set; }

    public List<SqlExpression> Expressions { get; private set; }

    internal override object Clone(SqlNodeCloneContext context)
    {
      if (context.NodeMapping.TryGetValue(this, out var value)) {
        return value;
      }
      var clone = new SqlDynamicFilter(Id);
      foreach (var expression in Expressions) {
        clone.Expressions.Add((SqlExpression) expression.Clone(context));
      }

      context.NodeMapping[this] = clone;
      return clone;
    }

    public override void AcceptVisitor(ISqlVisitor visitor)
    {
      visitor.Visit(this); 
    }

    public override void ReplaceWith(SqlExpression expression)
    {
      var replacingExpression = ArgumentValidator.EnsureArgumentIs<SqlDynamicFilter>(expression);
      Id = replacingExpression.Id;
      Expressions.Clear();
      Expressions.AddRange(replacingExpression.Expressions);
    }


    // Constructors

    internal SqlDynamicFilter(object id)
      : base(SqlNodeType.DynamicFilter)
    {
      Id = id;
      Expressions = new List<SqlExpression>();
    }
  }
}