/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.api.internal.artifacts;

import org.gradle.api.artifacts.ModuleIdentifier;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.ModuleVersionSelector;
import org.gradle.api.artifacts.component.ComponentSelector;
import org.gradle.api.artifacts.component.ModuleComponentSelector;
import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
import org.gradle.api.artifacts.component.ProjectComponentSelector;
import org.gradle.api.internal.artifacts.component.ComponentIdentifierFactory;
import org.gradle.api.internal.artifacts.ivyservice.projectmodule.LocalComponentRegistry;
import org.gradle.internal.component.local.model.LocalComponentMetadata;

public class DefaultComponentSelectorConverter implements ComponentSelectorConverter {
    private static final ModuleVersionSelector UNKNOWN_MODULE_VERSION_SELECTOR = DefaultModuleVersionSelector.newSelector("", "unknown", "");
    private final ImmutableModuleIdentifierFactory moduleIdentifierFactory;
    private final ComponentIdentifierFactory componentIdentifierFactory;
    private final LocalComponentRegistry localComponentRegistry;

    public DefaultComponentSelectorConverter(ImmutableModuleIdentifierFactory moduleIdentifierFactory, ComponentIdentifierFactory componentIdentifierFactory, LocalComponentRegistry localComponentRegistry) {
        this.moduleIdentifierFactory = moduleIdentifierFactory;
        this.componentIdentifierFactory = componentIdentifierFactory;
        this.localComponentRegistry = localComponentRegistry;
    }

    @Override
    public ModuleIdentifier getModule(ComponentSelector componentSelector) {
        if (componentSelector instanceof ModuleComponentSelector) {
            ModuleComponentSelector module = (ModuleComponentSelector) componentSelector;
            return moduleIdentifierFactory.module(module.getGroup(), module.getModule());
        }
        ModuleVersionSelector moduleVersionSelector = getSelector(componentSelector);
        return moduleIdentifierFactory.module(moduleVersionSelector.getGroup(), moduleVersionSelector.getName());
    }

    @Override
    public ModuleVersionSelector getSelector(ComponentSelector selector) {
        if (selector instanceof ModuleComponentSelector) {
            return DefaultModuleVersionSelector.newSelector((ModuleComponentSelector) selector);
        }
        if (selector instanceof ProjectComponentSelector) {
            ProjectComponentSelector projectSelector = (ProjectComponentSelector) selector;
            ProjectComponentIdentifier projectId = componentIdentifierFactory.createProjectComponentIdentifier(projectSelector);
            LocalComponentMetadata projectComponent = localComponentRegistry.getComponent(projectId);
            if (projectComponent != null) {
                ModuleVersionIdentifier moduleVersionId = projectComponent.getModuleVersionId();
                return DefaultModuleVersionSelector.newSelector(moduleVersionId.getGroup(), moduleVersionId.getName(), moduleVersionId.getVersion());
            }
        }
        return UNKNOWN_MODULE_VERSION_SELECTOR;
    }
}

@@ -868,32 +868,100 @@
 ponentSelector;%0A
+import org.gradle.api.artifacts.component.LibraryComponentSelector;%0A
 import org.gradl
@@ -1390,16 +1390,46 @@
 etadata;
+%0Aimport org.gradle.util.GUtil;
 %0A%0Apublic
@@ -3777,32 +3777,426 @@
     %7D%0A        %7D%0A
+        if (selector instanceof LibraryComponentSelector) %7B%0A            LibraryComponentSelector libraryComponentSelector = (LibraryComponentSelector) selector;%0A            String libraryName = GUtil.elvis(libraryComponentSelector.getLibraryName(), %22%22);%0A            return DefaultModuleVersionSelector.newSelector(libraryComponentSelector.getProjectPath(), libraryName, %22undefined%22);%0A        %7D%0A
         return U

Fix dependency engine for JVM software model dependency resolve