package com.atlassian.stash.internal.applinks;

import com.atlassian.integration.jira.applinks.LinkableEntityResolver;
import com.atlassian.plugin.spring.AvailableToPlugins;
import com.atlassian.bitbucket.project.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @since 2.7
 */
@AvailableToPlugins(LinkableEntityResolver.class)
@Component("linkableEntityResolver")
public class ProjectEntityResolver implements LinkableEntityResolver {

    private final ProjectService projectService;

    @Autowired
    public ProjectEntityResolver(ProjectService projectService) {
        this.projectService = projectService;
    }

    @Override
    public Object resolve(@Nonnull String key) {
        checkNotNull(key, "key");
        checkArgument(!key.trim().isEmpty(), "A non-blank entity key is required");

        return projectService.getByKey(key);
    }
}
