package jetbrains.mps.lang.core.plugin;

/*Generated by MPS */

import java.util.Map;
import org.jetbrains.mps.openapi.module.SModule;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.make.resources.IResource;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import java.util.HashMap;
import jetbrains.mps.smodel.resources.MResource;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.generator.GenerationFacade;

public final class RetainedUtil {
  private RetainedUtil() {
  }
  public static Map<SModule, Iterable<SModel>> collectModelsToRetain(Iterable<? extends IResource> input) {
    Map<SModule, Iterable<SModel>> retainedModels = MapSequence.fromMap(new HashMap<SModule, Iterable<SModel>>());
    for (IResource it : input) {
      MResource mres = ((MResource) it);
      SModule module = mres.module();
      // XXX why only generateable models?
      // guess, we have to take all models from module and exclude those we were asked to generate
      Iterable<SModel> modelsToRetain = generateableModels(module);
      MapSequence.fromMap(retainedModels).put(module, Sequence.fromIterable(modelsToRetain).subtract(Sequence.fromIterable(mres.models())).toList());
    }
    return retainedModels;
  }

  private static Iterable<SModel> generateableModels(SModule module) {
    return Sequence.fromIterable(((Iterable<SModel>) module.getModels())).where((it) -> GenerationFacade.canGenerate(it));
  }
}
