package jetbrains.mps.lang.core.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.Sequence;
import org.jetbrains.mps.openapi.language.SProperty;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import org.jetbrains.mps.openapi.language.SDataType;
import jetbrains.mps.smodel.adapter.structure.types.SEnumerationAdapter;
import jetbrains.mps.lang.migration.runtime.base.MigrationScriptReference;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;

public class EnumPropertyPersistedById extends MigrationScriptBase {
  private final String description = "Persist enum property values by literal id";
  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_ul5hh3_a0e = CommandUtil.createScope(m);
      final SearchScope scope_ul5hh3_a0e_0 = new EditableFilteringScope(scope_ul5hh3_a0e);
      QueryExecutionContext context = () -> scope_ul5hh3_a0e_0;
      for (SNode node : Sequence.fromIterable(CommandUtil.nodes(CommandUtil.selectScope(null, context)))) {
        Iterable<SProperty> properties = SNodeOperations.getConcept(node).getProperties();
        for (SProperty prop : Sequence.fromIterable(properties)) {
          SDataType type = prop.getType();
          if (type instanceof SEnumerationAdapter) {
            SEnumerationAdapter enumm = (SEnumerationAdapter) type;
            enumm.migrateEnumProperty(node, prop);
          }
        }
      }
    }
  }
  public MigrationScriptReference getReference() {
    return new MigrationScriptReference(MetaAdapterFactory.getLanguage(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, "jetbrains.mps.lang.core"), 1);
  }

}
