using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

namespace Microsoft.Dafny;

public class DisjunctivePattern : ExtendedPattern {
  public readonly List<ExtendedPattern> Alternatives;
  public DisjunctivePattern(IOrigin tok, List<ExtendedPattern> alternatives, bool isGhost = false) : base(tok, isGhost) {
    Contract.Requires(alternatives != null && alternatives.Count > 0);
    this.Alternatives = alternatives;
  }

  public override IEnumerable<INode> Children => Alternatives;
  public override IEnumerable<INode> PreResolveChildren => Children;

  public override IEnumerable<Expression> SubExpressions {
    get {
      foreach (var alternative in Alternatives) {
        foreach (var ee in alternative.SubExpressions) {
          yield return ee;
        }
      }
    }
  }

  public override void Resolve(ModuleResolver resolver, ResolutionContext resolutionContext,
    Type sourceType, bool isGhost, bool inStatementContext,
    bool inPattern, bool inDisjunctivePattern) {

    if (inPattern) {
      resolver.reporter.Error(MessageSource.Resolver, Tok, "Disjunctive patterns are not allowed inside other patterns");
    }

    foreach (var alternative in Alternatives) {
      alternative.Resolve(resolver, resolutionContext, sourceType, isGhost, inStatementContext, true, true);
    }
  }
}