package jetbrains.mps.baseLanguage.editor;

/*Generated by MPS */

import org.jetbrains.mps.openapi.model.SNode;
import java.util.List;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import org.jetbrains.mps.openapi.language.SInterfaceConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SContainmentLink;

public class MethodArgumentsUtil {
  public static SNode getMethodArgumentAncestor(SNode innerExpression) {
    List<SNode> ancestors = SNodeOperations.getNodeAncestors(innerExpression, null, true);
    SNode methodCall = Sequence.fromIterable(SNodeOperations.ofConcept(ListSequence.fromList(ancestors).skip(1), CONCEPTS.IMethodCall$M9)).first();
    if (methodCall == null) {
      return null;
    }
    SNode lastAncestor = ListSequence.fromList(ancestors).getElement(ListSequence.fromList(ancestors).indexOf(methodCall) - 1);
    if (SNodeOperations.hasRole(lastAncestor, LINKS.actualArgument$pzdx)) {
      return SNodeOperations.cast(lastAncestor, CONCEPTS.Expression$mB);
    } else {
      return null;
    }
  }

  private static final class CONCEPTS {
    /*package*/ static final SInterfaceConcept IMethodCall$M9 = MetaAdapterFactory.getInterfaceConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11857355952L, "jetbrains.mps.baseLanguage.structure.IMethodCall");
    /*package*/ static final SConcept Expression$mB = MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37f506fL, "jetbrains.mps.baseLanguage.structure.Expression");
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink actualArgument$pzdx = MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11857355952L, 0xf8c78301aeL, "actualArgument");
  }
}
