class DefaultAuthenticationFailureAggregator implements AuthenticationFailureAggregator {

    @Override
    public DittoRuntimeException aggregateAuthenticationFailures(final List<AuthenticationResult> failedAuthResults) {
        final List<DittoRuntimeException> reasonsOfFailure = getDittoRuntimeExceptionReasonsWithDescription(failedAuthResults);

        if (reasonsOfFailure.isEmpty()) {
            final String msgPattern = "The failed authentication results did not contain any failure reason of type " +
                    "<{0}> containing a description!";
            final String message = MessageFormat.format(msgPattern, DittoRuntimeException.class.getSimpleName());
            throw new IllegalArgumentException(message);
        }

        if (1 == reasonsOfFailure.size()) {
            return reasonsOfFailure.get(0);
        }

        return reasonsOfFailure.stream()
                .filter(reasonOfFailure -> !HttpStatusCode.UNAUTHORIZED.equals(reasonOfFailure.getStatusCode()))
                .findFirst()
                .orElseGet(() -> GatewayAuthenticationFailedException.newBuilder(AGGREGATED_AUTH_FAILURE_MESSAGE)
                        .description(buildAggregatedDescriptionFromDittoRuntimeExceptions(reasonsOfFailure))
                        .dittoHeaders(buildAggregatedHeaders(reasonsOfFailure))
                        .build());
    }

    private  DefaultAuthenticationFailureAggregator();

}