{{>common/licenseInfo}}
package {{invokerPackage}}.auth;

import io.micronaut.aop.MethodInvocationContext;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.naming.NameUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.client.bind.AnnotatedClientRequestBinder;
import io.micronaut.http.client.bind.ClientRequestUriContext;
import jakarta.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import {{javaxPackage}}.annotation.Generated;


{{>common/generatedAnnotation}}
@Singleton
public class AuthorizationBinder implements AnnotatedClientRequestBinder<Authorization> {

    public static final CharSequence AUTHORIZATION_NAMES  = "micronaut.security.AUTHORIZATION_NAMES";

    @NonNull
    @Override
    public Class<Authorization> getAnnotationType() {
        return Authorization.class;
    }

    @Override
    public void bind(@NonNull MethodInvocationContext<Object, Object> context,
                     @NonNull ClientRequestUriContext uriContext,
                     @NonNull MutableHttpRequest<?> request
    ) {
        List<AnnotationValue<Authorization>> annotations = context.getAnnotationMetadata()
                .getAnnotationValuesByType(Authorization.class);

        if (CollectionUtils.isNotEmpty(annotations)) {
            List<String> authorizationNames = new ArrayList<>();
            for (AnnotationValue<Authorization> annotation: annotations) {
                annotation.get("name", String.class)
                        .filter(StringUtils::isNotEmpty)
                        .ifPresent(v -> authorizationNames.add(configurationName(v)));
            }
            request.setAttribute(AUTHORIZATION_NAMES, authorizationNames);
        }
    }

    private String configurationName(String name) {
        return NameUtils.hyphenate(name.replace('.', '_'), true);
    }
}
