class OrientNpmGroupDataFacet extends FacetSupport {

  @Nullable
  @VisibleForTesting
  protected Content buildMergedPackageRoot(final Map<Repository, Response> responses, final Context context)
      throws IOException
  {
    checkNotNull(responses);
    checkNotNull(context);

    if (responses.isEmpty()) {
      log.debug("Unable to create package root for repository {}. Members had no metadata to merge.",
          context.getRepository().getName());

      return null;
    }

    List<Content> contents = responses
        .values().stream().map(response -> (Content) response.getPayload()).collect(toList());

    NestedAttributesMap result;
    NpmPackageId packageId = NpmPaths.packageId(matcherState(context));

    if (shouldServeFirstResult(contents, packageId)) {
      result = parseContent(contents.get(0));
    }
    else {
      log.debug("Merging results from {} repositories", responses.size());

      // we make the last package the dominant one, by reversing the list
      reverse(contents);

      result = mergeContents(contents);
    }

    NpmMetadataUtils.rewriteTarballUrl(context.getRepository().getName(), result);

    return saveToCache(packageId, result);
  }

  @Inject public  OrientNpmGroupDataFacet(
      @Named("${nexus.npm.mergeGroupMetadata:-true}") final boolean mergeMetadata);

}