package jetbrains.mps.findUsages;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import java.util.Set;
import org.jetbrains.mps.openapi.module.SearchScope;
import org.jetbrains.mps.openapi.util.Consumer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.mps.openapi.util.ProgressMonitor;
import java.util.Collection;
import org.jetbrains.mps.openapi.persistence.FindUsagesParticipant;
import org.jetbrains.mps.openapi.persistence.PersistenceFacade;
import java.util.HashSet;
import jetbrains.mps.smodel.ConceptDescendantsCache;
import org.jetbrains.mps.openapi.model.SModel;
import java.util.LinkedHashSet;
import jetbrains.mps.util.IterableUtil;
import org.jetbrains.mps.openapi.model.EditableSModel;
import org.jetbrains.mps.openapi.util.SubProgressKind;

@GeneratedClass(node = "r:54a768d9-9f11-4443-98d8-70ab3a783c52(jetbrains.mps.findUsages)/8568892084424435470", model = "r:54a768d9-9f11-4443-98d8-70ab3a783c52(jetbrains.mps.findUsages)")
/*package*/ class InstancesSearchType extends SearchType<SNode, SAbstractConcept> {
  private final boolean myExact;
  /*package*/ InstancesSearchType(boolean exact) {
    myExact = exact;
  }
  @Override
  public void search(Set<SAbstractConcept> elements, SearchScope scope, Consumer<SNode> consumer, @NotNull ProgressMonitor monitor) {
    assert !(elements.contains(null));
    Collection<FindUsagesParticipant> participants = PersistenceFacade.getInstance().getFindUsagesParticipants();
    monitor.start("Finding usages...", participants.size() + 5);
    try {
      Set<SAbstractConcept> queryConcepts = new HashSet<SAbstractConcept>(elements);
      if (!(myExact)) {
        for (SAbstractConcept concept : elements) {
          queryConcepts.addAll(ConceptDescendantsCache.getInstance().getDescendants(concept));
        }
      }
      monitor.advance(1);
      Collection<SModel> current = new LinkedHashSet<SModel>();
      Collection<SModel> simpleSearch = new LinkedHashSet<SModel>();
      for (SModel m : IterableUtil.asCollection(scope.getModels())) {
        if (m instanceof EditableSModel && ((EditableSModel) m).isChanged()) {
          simpleSearch.add(m);
        } else {
          current.add(m);
        }
      }
      for (FindUsagesParticipant participant : participants) {
        final Set<SModel> next = new HashSet<SModel>(current);
        participant.findInstances(current, queryConcepts, consumer, (SModel m) -> next.remove(m));
        current = next;
        monitor.advance(1);
      }
      ProgressMonitor subMonitor = monitor.subTask(4, SubProgressKind.DEFAULT);
      subMonitor.start("", current.size() + simpleSearch.size());
      showNoFastFindTipIfNeeded(current);
      current.addAll(simpleSearch);
      final InstanceLookup nif = new InstanceLookup(queryConcepts, consumer);
      for (SModel m : current) {
        subMonitor.step(m.getName().getSimpleName());
        nif.collectInstances(m, subMonitor.subTask(1));
        if (subMonitor.isCanceled()) {
          break;
        }
      }
      subMonitor.done();
    } finally {
      monitor.done();
    }
  }
}
