package io.quarkus.container.image.deployment;

import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;

public final class ContainerImageCapabilitiesUtil {

    public final static Map<String, String> CAPABILITY_TO_EXTENSION_NAME = Map.of(
            Capability.CONTAINER_IMAGE_JIB, "quarkus-container-image-jib",
            Capability.CONTAINER_IMAGE_DOCKER, "quarkus-container-image-docker",
            Capability.CONTAINER_IMAGE_S2I, "quarkus-container-image-s2i",
            Capability.CONTAINER_IMAGE_OPENSHIFT, "quarkus-container-image-openshift",
            Capability.CONTAINER_IMAGE_BUILDPACK, "quarkus-container-image-buildpack");

    private ContainerImageCapabilitiesUtil() {
    }

    /**
     * Returns the active container image capability or throws an {@code IllegalStateException} if more than one are active
     */
    public static Optional<String> getActiveContainerImageCapability(Capabilities capabilities) {
        Set<String> activeContainerImageCapabilities = ContainerImageCapabilitiesUtil
                .getContainerImageCapabilities(capabilities);
        if (activeContainerImageCapabilities.size() > 1) {
            throw new IllegalStateException(String.join(" and ", activeContainerImageCapabilities)
                    + " were detected, at most one container-image extension can be present.\n"
                    + "Either remove the unneeded ones, or select one by adding the property 'quarkus.container-image.builder=<extension name (without the `container-image-` prefix)>' in application.properties or as a system property.");
        }
        return activeContainerImageCapabilities.isEmpty() ? Optional.empty()
                : Optional.of(activeContainerImageCapabilities.iterator().next());
    }

    private static Set<String> getContainerImageCapabilities(Capabilities capabilities) {
        Set<String> activeContainerImageCapabilities = new HashSet<>();
        for (String capability : capabilities.getCapabilities()) {
            if (capability.toLowerCase().contains("container.image")) {
                if (!CAPABILITY_TO_EXTENSION_NAME.containsKey(capability)) {
                    throw new IllegalArgumentException("Unknown container image capability: " + capability);
                }
                activeContainerImageCapabilities.add(CAPABILITY_TO_EXTENSION_NAME.get(capability));
            }
        }
        return activeContainerImageCapabilities;
    }
}

@@ -767,16 +767,385 @@
 ack%22);%0A%0A
+    private final static Map%3CString, String%3E CAPABILITY_TO_BUILDER_NAME = Map.of(%0A            Capability.CONTAINER_IMAGE_JIB, %22jib%22,%0A            Capability.CONTAINER_IMAGE_DOCKER, %22docker%22,%0A            Capability.CONTAINER_IMAGE_S2I, %22s2i%22,%0A            Capability.CONTAINER_IMAGE_OPENSHIFT, %22openshift%22,%0A            Capability.CONTAINER_IMAGE_BUILDPACK, %22buildpack%22);%0A%0A
     priv
@@ -1907,46 +1907,184 @@
  by 
-adding the property 'quarkus.c
+setting any of the following configuration properties: %22%0A                    + createBuilderSelectionPropertySuggestion(capabilities));%0A        %7D%0A        return activeC
 ontainer
 -ima
@@ -2083,62 +2083,100 @@
 iner
--i
+I
 mage
-.builder=%3Cextension name (without the %60c
+Capabilities.isEmpty() ? Optional.empty()%0A                : Optional.of(activeC
 ontainer
 -ima
@@ -2175,213 +2175,583 @@
 iner
--i
+I
 mage
--%60 prefix)%3E' in applica
+Capabilities.iterator().next());%0A    %7D%0A%0A    private static StringBuilder createBuilderSelec
 tion
-.p
+P
 ropert
-ies or as a system property.%22);%0A        %7D%0A        return activeContainerImageCapabilities.isEmpty() ? Optional.empty()%0A                : Optional.of(activeC
+ySuggestion(Capabilities capabilities) %7B%0A        StringBuilder suggestion = new StringBuilder();%0A        boolean isFirst = true;%0A        for (String capability : capabilities.getCapabilities()) %7B%0A            if (!isContainerImageCapability(capability)) %7B%0A                continue;%0A            %7D%0A            if (!isFirst) %7B%0A                suggestion.append(%22, %22);%0A            %7D%0A            isFirst = false;%0A            suggestion.append('%5C'').append(%22quarkus.c
 ontainer
 Imag
@@ -2750,44 +2750,147 @@
 iner
-I
+-i
 mage
-Capabilities.iterator().next())
+.builder=%22)%0A                    .append(CAPABILITY_TO_BUILDER_NAME.get(capability)).append('%5C'');%0A        %7D%0A        return suggestion
 ;%0A  
@@ -3143,59 +3143,45 @@
 if (
-capability.toLowerCase().contains(%22container.image%22
+isContainerImageCapability(capability
 )) %7B
@@ -3563,11 +3563,162 @@
 s;%0A    %7D
+%0A%0A    private static boolean isContainerImageCapability(String capability) %7B%0A        return capability.toLowerCase().contains(%22container.image%22);%0A    %7D
 %0A%7D%0A

Improve error message when multiple container image extensions are present