package tools.devnull.boteco.plugins.definition;

import tools.devnull.boteco.ServiceRegistry;
import tools.devnull.boteco.plugins.definition.spi.Definition;
import tools.devnull.boteco.plugins.definition.spi.DefinitionProvider;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static tools.devnull.boteco.Predicates.id;

public class Lookup {

  private final ServiceRegistry registry;
  private final String provider;
  private final String term;

  public Lookup(ServiceRegistry registry, String provider, String term) {
    this.registry = registry;
    this.provider = provider;
    this.term = term;
  }

  public Lookup(ServiceRegistry registry, String term) {
    this.registry = registry;
    this.provider = null;
    this.term = term;
  }

  public String term() {
    return term;
  }

  public List<Definition> lookup() {
    List<Definition> result;
    if (provider == null) {
      result = registry.locate(DefinitionProvider.class).all()
          .stream()
          .map(p -> p.lookup(term))
          .filter(Objects::nonNull)
          .collect(Collectors.toList());
    } else {
      DefinitionProvider definitionProvider = registry.locate(DefinitionProvider.class)
          .filter(id(provider))
          .one();
      result = Collections.singletonList(definitionProvider.lookup(term));
    }
    return result;
  }

}

@@ -1317,76 +1317,205 @@
     
-result = Collections.singletonList(definitionProvider.lookup(term));
+Definition lookup = definitionProvider.lookup(term);%0A      if (lookup != null) %7B%0A        result = Collections.singletonList(lookup);%0A      %7D else %7B%0A        result = Collections.emptyList();%0A      %7D
 %0A   

handle null values