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.soy.renderer.JsExpression;
import com.atlassian.bitbucket.repository.Ref;

public class ListRepoCommitsNavFunction extends BaseRepoNavFunction {

    public ListRepoCommitsNavFunction(NavBuilder navBuilder) {
        super(navBuilder, "nav_list_commits", SoyArgumentUtils.argCountFromTo(2, 4));
    }

    @Override
    protected JsNavBuilder initJsBuilder(JsExpression... jsExpressions) {
        JsNavBuilder commits = repo(jsExpressions).commits();
        if (jsExpressions.length > 2) {
            commits = commits.callMethod("until", jsExpressions[2]);
        }
        return commits.withParamsIfAvailable(jsExpressions, 3);
    }

    @Override
    protected NavBuilder.Builder initBuilder(Object... args) {
        NavBuilder.ListCommits commits = repo(args).commits();
        if (args.length > 2) {
            Object arg = args[2];
            if (arg instanceof Ref) {
                commits = commits.until((Ref) arg);
            } else if (arg instanceof String) {
                commits = commits.until((String) arg);
            }
        }
        return commits;
    }

}



