package com.atlassian.bitbucket.internal.ssh.scm.protocol;

import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.ScmUrlFormatter;
import com.atlassian.bitbucket.scm.ScmUrlRequest;
import com.atlassian.bitbucket.scm.http.RepositoryUrlFragment;

import javax.annotation.Nonnull;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @since 3.4
 */
public class DefaultSshUrlFormatter implements ScmUrlFormatter {

    @Nonnull
    @Override
    public URI format(@Nonnull ScmUrlRequest request) throws URISyntaxException {
        Repository repository = request.getRepository();
        URI uri = request.getBaseUrl();
        String path = RepositoryUrlFragment.fromRepository(repository).toPath(uri.getPath());

        return new URI(uri.getScheme(), repository.getScmId(), uri.getHost(), uri.getPort(),
                path, uri.getQuery(), uri.getFragment());
    }
}
