package com.atlassian.stash.internal.web.soy.functions.nav;

import com.atlassian.soy.renderer.SoyServerFunction;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.http.HttpScmProtocol;
import com.atlassian.bitbucket.auth.AuthenticationContext;
import com.google.common.collect.ImmutableSet;

import java.util.Set;

/**
 * Uses the {@link HttpScmProtocol} to format clone URLs.
 */
public class CloneUrlFunction implements SoyServerFunction<String> {

    private static final Set<Integer> VALID_SIZES = ImmutableSet.of(2, 3);

    private final AuthenticationContext authenticationContext;
    private final HttpScmProtocol protocol;

    public CloneUrlFunction(AuthenticationContext authenticationContext, HttpScmProtocol protocol) {
        this.authenticationContext = authenticationContext;
        this.protocol = protocol;
    }

    @Override
    public String apply(Object... objects) {
        return protocol.getCloneUrl((Repository) objects[1], authenticationContext.getCurrentUser());
    }

    @Override
    public String getName() {
        return "nav_clone";
    }

    @Override
    public Set<Integer> validArgSizes() {
        return VALID_SIZES;
    }
}
