package jetbrains.mps.ide.migration;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.lang.migration.runtime.base.BaseScriptReference;
import jetbrains.mps.lang.migration.runtime.base.BaseScript;
import java.util.List;
import org.jetbrains.mps.openapi.module.SModuleReference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.mps.openapi.module.SModule;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import java.util.Collection;
import org.jetbrains.mps.openapi.module.SRepository;
import jetbrains.mps.project.Project;
import jetbrains.mps.internal.collections.runtime.ListSequence;

/**
 * Collection of modules with a script to apply to them.
 * To put it another way, {@link jetbrains.mps.ide.migration.ScriptApplied } done right
 */
@GeneratedClass(node = "a5b1c28d-abeb-49a6-a58c-559039616d64/r:a9597bdf-0806-4a79-8ace-88240c6b9878(jetbrains.mps.migration.component/jetbrains.mps.ide.migration)/4068647021694757046", model = "a5b1c28d-abeb-49a6-a58c-559039616d64/r:a9597bdf-0806-4a79-8ace-88240c6b9878(jetbrains.mps.migration.component/jetbrains.mps.ide.migration)")
public abstract class AppliedScript {
  private final BaseScriptReference<?> myScriptRef;
  protected BaseScript myScript;
  private final List<SModuleReference> myModules;
  private final List<ScriptApplied<BaseScriptReference>> myLegacyValues;

  /**
   * use solely for scenarios when there's no script instance, iow represents broken migration scenario
   */
  public AppliedScript(@NotNull final BaseScriptReference<?> scriptRef, Iterable<SModule> affectedModules) {
    myScriptRef = scriptRef;
    myScript = null;
    myModules = Sequence.fromIterable(affectedModules).select((this0) -> this0.getModuleReference()).toList();
    myLegacyValues = Sequence.fromIterable(affectedModules).select(new _FunctionTypes._return_P1_E0<ScriptApplied<BaseScriptReference>, SModule>() {
      public ScriptApplied<BaseScriptReference> invoke(SModule it) {
        return new ScriptApplied<BaseScriptReference>(it, scriptRef);
      }
    }).toList();
  }

  public AppliedScript(@NotNull final BaseScript script, Iterable<SModule> affectedModules) {
    myScriptRef = script.getReference();
    myScript = script;
    myModules = Sequence.fromIterable(affectedModules).select((this0) -> this0.getModuleReference()).toList();
    myLegacyValues = Sequence.fromIterable(affectedModules).select(new _FunctionTypes._return_P1_E0<ScriptApplied<BaseScriptReference>, SModule>() {
      public ScriptApplied<BaseScriptReference> invoke(SModule it) {
        return new ScriptApplied<BaseScriptReference>(it, script);
      }
    }).toList();
  }

  @NotNull
  public BaseScriptReference<?> scriptReference() {
    return myScriptRef;
  }

  public boolean scriptPresent() {
    return myScript != null;
  }

  public String caption() {
    return (myScript != null ? myScript.getCaption() : "");
  }

  public abstract Collection<ScriptApplied> toBeExecutedImmediately(SRepository repo);

  public abstract void refreshScriptInstances(Project mpsProject);

  public Iterable<ScriptApplied> asLegacy() {
    return ListSequence.fromList(myLegacyValues).ofType(ScriptApplied.class);
  }

  public Iterable<SModuleReference> affectedModules() {
    return myModules;
  }
}
