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

import com.atlassian.soy.renderer.SoyServerFunction;
import com.atlassian.bitbucket.util.TextUtils;

import java.util.Collections;
import java.util.Set;

public class PathTruncateFunction implements SoyServerFunction<String> {
    public static final Set<Integer> VALID_ARG_SIZES = Collections.singleton(4);

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

    @Override
    public String apply(Object... args) {
        String path = SoyArgumentUtils.getStringArgument(args, 0);
        Number startSegments = SoyArgumentUtils.getArgument(args, 1, Number.class);
        Number endSegments = SoyArgumentUtils.getArgument(args, 2, Number.class);
        Number maxLength = SoyArgumentUtils.getArgument(args, 3, Number.class);
        return TextUtils.truncatePath(path, startSegments.intValue(), endSegments.intValue(), maxLength.intValue());
    }

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