package jetbrains.mps.kotlin.migration;

/*Generated by MPS */

import jetbrains.mps.lang.migration.runtime.base.MigrationScriptBase;
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.mps.openapi.module.SModule;
import org.jetbrains.mps.openapi.module.SearchScope;
import jetbrains.mps.lang.smodel.query.runtime.CommandUtil;
import jetbrains.mps.project.EditableFilteringScope;
import jetbrains.mps.lang.smodel.query.runtime.QueryExecutionContext;
import jetbrains.mps.internal.collections.runtime.CollectionSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.lang.migration.runtime.base.Problem;
import jetbrains.mps.lang.migration.runtime.base.NotMigratedNode;
import jetbrains.mps.lang.migration.runtime.base.MigrationScriptReference;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SReferenceLink;
import org.jetbrains.mps.openapi.language.SProperty;

public class ClearLambdaLiteralHack extends MigrationScriptBase {
  private final String description = "Remove deprecated typesystem-related structure elements";
  public String getCaption() {
    return description;
  }
  @Override
  public boolean isRerunnable() {
    return true;
  }
  public SNode execute(final SModule m) {
    doExecute(m);
    return null;
  }
  public void doExecute(final SModule m) {
    {
      SearchScope scope_dshwou_a0e = CommandUtil.createScope(m);
      final SearchScope scope_dshwou_a0e_0 = new EditableFilteringScope(scope_dshwou_a0e);
      QueryExecutionContext context = () -> scope_dshwou_a0e_0;
      CollectionSequence.fromCollection(CommandUtil.instances(CommandUtil.selectScope(null, context), CONCEPTS.LambdaLiteral$Bd, false)).visitAll((it) -> {
        SNodeOperations.deleteNode(SLinkOperations.getTarget(it, LINKS._itTypeHolder_hack$yOMR));
        SNodeOperations.deleteNode(SLinkOperations.getTarget(it, LINKS._thisTypeHolder_hack$QRo4));
      });

      CollectionSequence.fromCollection(CommandUtil.instances(CommandUtil.selectScope(null, context), CONCEPTS.FunctionCallExpression$EQ, false)).visitAll((it) -> {
        SLinkOperations.setTarget(it, LINKS._receiver$fya4, null);
        SPropertyOperations.remove(it, PROPS._receiverIndex$ELkw);
      });
    }
  }
  @Override
  public Iterable<Problem> check(SModule m) {
    {
      SearchScope scope_dshwou_a0f = CommandUtil.createScope(m);
      final SearchScope scope_dshwou_a0f_0 = new EditableFilteringScope(scope_dshwou_a0f);
      QueryExecutionContext context = () -> scope_dshwou_a0f_0;
      return CollectionSequence.fromCollection(CommandUtil.instances(CommandUtil.selectScope(null, context), CONCEPTS.LambdaLiteral$Bd, false)).where((it) -> (SLinkOperations.getTarget(it, LINKS._itTypeHolder_hack$yOMR) != null) || (SLinkOperations.getTarget(it, LINKS._thisTypeHolder_hack$QRo4) != null)).concat(CollectionSequence.fromCollection(CommandUtil.instances(CommandUtil.selectScope(null, context), CONCEPTS.FunctionCallExpression$EQ, false)).where((it) -> SNodeOperations.getReference(it, LINKS._receiver$fya4) != null)).select((it) -> {
        return new NotMigratedNode(it) {
          @Override
          public String getMessage() {
            return "deprecated structure elements not migrated";
          }
        };
      });
    }
  }
  public MigrationScriptReference getReference() {
    return new MigrationScriptReference(MetaAdapterFactory.getLanguage(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, "jetbrains.mps.kotlin"), 9);
  }


  private static final class CONCEPTS {
    /*package*/ static final SConcept LambdaLiteral$Bd = MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af517L, "jetbrains.mps.kotlin.structure.LambdaLiteral");
    /*package*/ static final SConcept FunctionCallExpression$EQ = MetaAdapterFactory.getConcept(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x40b4c3a5339a6979L, "jetbrains.mps.kotlin.structure.FunctionCallExpression");
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink _itTypeHolder_hack$yOMR = MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af517L, 0x37f51ee20f70b404L, "_itTypeHolder_hack");
    /*package*/ static final SContainmentLink _thisTypeHolder_hack$QRo4 = MetaAdapterFactory.getContainmentLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x28bef6d7551af517L, 0x37f51ee20f709e5fL, "_thisTypeHolder_hack");
    /*package*/ static final SReferenceLink _receiver$fya4 = MetaAdapterFactory.getReferenceLink(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x40b4c3a5339a6979L, 0x6b93446a6f90602eL, "_receiver");
  }

  private static final class PROPS {
    /*package*/ static final SProperty _receiverIndex$ELkw = MetaAdapterFactory.getProperty(0x6b3888c1980244d8L, 0x8baff8e6c33ed689L, 0x40b4c3a5339a6979L, 0x6b93446a6f906849L, "_receiverIndex");
  }
}
