package jetbrains.mps.lang.structure.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.SPropertyOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.lang.migration.runtime.base.Problem;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import jetbrains.mps.lang.migration.runtime.base.DeprecatedConceptNotMigratedProblem;
import jetbrains.mps.lang.migration.runtime.base.MigrationScriptReference;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.smodel.builder.SNodeBuilder;
import org.jetbrains.mps.openapi.model.SNodeAccessUtil;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SProperty;
import org.jetbrains.mps.openapi.language.SContainmentLink;

public class MigrateHelpURL extends MigrationScriptBase {
  private final String description = "replace old HelpURLs with new ones in concepts";
  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_glsjld_a0e = CommandUtil.createScope(m);
      final SearchScope scope_glsjld_a0e_0 = new EditableFilteringScope(scope_glsjld_a0e);
      QueryExecutionContext context = () -> scope_glsjld_a0e_0;
      CollectionSequence.fromCollection(CommandUtil.instances(CommandUtil.selectScope(null, context), CONCEPTS.AbstractConceptDeclaration$KA, false)).where((it) -> isNotEmptyString(SPropertyOperations.getString(it, PROPS.oldHelpURL$yJ0l))).visitAll((it) -> {
        SLinkOperations.setTarget(it, LINKS.helpURL$cn$D, _quotation_createNode_glsjld_a0a0a0a0a0a6(SPropertyOperations.getString(it, PROPS.oldHelpURL$yJ0l)));
        SPropertyOperations.assign(it, PROPS.oldHelpURL$yJ0l, null);
      });
    }
  }
  @Override
  public Iterable<Problem> check(SModule m) {
    {
      SearchScope scope_glsjld_a0f = CommandUtil.createScope(m);
      final SearchScope scope_glsjld_a0f_0 = new EditableFilteringScope(scope_glsjld_a0f);
      QueryExecutionContext context = () -> scope_glsjld_a0f_0;
      return CollectionSequence.fromCollection(CommandUtil.instances(CommandUtil.selectScope(null, context), CONCEPTS.AbstractConceptDeclaration$KA, false)).where((it) -> isNotEmptyString(SPropertyOperations.getString(it, PROPS.oldHelpURL$yJ0l))).select(new _FunctionTypes._return_P1_E0<Problem, SNode>() {
        public Problem invoke(SNode it) {
          return ((Problem) new DeprecatedConceptNotMigratedProblem(it));
        }
      });
    }
  }
  public MigrationScriptReference getReference() {
    return new MigrationScriptReference(MetaAdapterFactory.getLanguage(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, "jetbrains.mps.lang.structure"), 5);
  }

  private static SNode _quotation_createNode_glsjld_a0a0a0a0a0a6(Object parameter_1) {
    SNode quotedNode_2 = null;
    SNodeBuilder nb = new SNodeBuilder(null, null).init(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0x982eb8df2c964bd7L, 0x996311712ea622e5L, "jetbrains.mps.lang.resources"), 0x47d8f9811b73d397L, "HelpURL"));
    quotedNode_2 = nb.getResult();
    SNodeAccessUtil.setPropertyValue(quotedNode_2, MetaAdapterFactory.getProperty(0x982eb8df2c964bd7L, 0x996311712ea622e5L, 0x47d8f9811b73d397L, 0x47d8f9811b73d398L, "url"), (String) parameter_1);
    return quotedNode_2;
  }
  private static boolean isNotEmptyString(String str) {
    return str != null && str.length() > 0;
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept AbstractConceptDeclaration$KA = MetaAdapterFactory.getConcept(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x1103553c5ffL, "jetbrains.mps.lang.structure.structure.AbstractConceptDeclaration");
  }

  private static final class PROPS {
    /*package*/ static final SProperty oldHelpURL$yJ0l = MetaAdapterFactory.getProperty(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x1103553c5ffL, 0x2237c3bc85b3755cL, "oldHelpURL");
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink helpURL$cn$D = MetaAdapterFactory.getContainmentLink(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0x1103553c5ffL, 0x18b475a713ca55fbL, "helpURL");
  }
}
