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

import com.atlassian.soy.renderer.JsExpression;
import com.atlassian.bitbucket.commit.Commit;
import com.atlassian.bitbucket.nav.NavBuilder;

public abstract class BaseCommitNavFunction extends BaseRepoNavFunction {
    public BaseCommitNavFunction(NavBuilder navBuilder, String name, int requiredArgCount) {
        super(navBuilder, name, requiredArgCount);
    }

    protected JsNavBuilder commit(JsExpression[] jsExpressions) {
        return repo(jsExpressions).commit(jsExpressions[2]);
    }

    protected NavBuilder.Commit commit(Object[] args) {
        return repo(args).commit(getCommitId(args));
    }

    private String getCommitId(Object[] args) {
        if (args[2] instanceof Commit) {
            return ((Commit) args[2]).getId();
        }
        return (String) args[2];
    }
}
