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

import com.atlassian.stash.internal.web.soy.functions.SoyArgumentUtils;
import com.atlassian.bitbucket.nav.NavBuilder;
import com.atlassian.bitbucket.project.Project;

public abstract class BaseProjectNavFunction extends BaseNavFunction {

    public BaseProjectNavFunction(NavBuilder navBuilder, String name) {
        super(navBuilder, name, 1);
    }

    protected NavBuilder.Project project(Object[] args) {
        ensureValidArgumentCount(args);
        SoyArgumentUtils.ensureArgumentTypeIsOneOf(args, 0, Project.class, String.class);
        return navBuilder.project(getProjectKey(args, 0));
    }

    static String getProjectKey(Object[] args, int index) {
        Object arg = args[index];
        if (arg instanceof Project) {
            return ((Project) arg).getKey();
        } else {
            return (String) arg;
        }
    }

}

