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

import com.atlassian.soy.renderer.JsExpression;
import com.atlassian.soy.renderer.SoyClientFunction;
import com.google.common.collect.ContiguousSet;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;

import java.util.Set;

public class TextUtilFunction implements SoyClientFunction {

    private static final Set<Integer> VALID_ARG_SIZES = ContiguousSet.create(Range.closed(2, 10), DiscreteDomain.integers());

    @Override
    public JsExpression generate(JsExpression... jsExpressions) {
        StringBuilder sb = new StringBuilder();
        sb.append("require('bitbucket/internal/util/text')[").append(jsExpressions[0].getText()).append("](");
        for (int i = 1; i < jsExpressions.length; i++) {
            if (i > 1) {
                sb.append(", ");
            }
            sb.append(jsExpressions[i].getText());
        }
        sb.append(")");
        return new JsExpression(sb.toString());
    }

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

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