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

import com.atlassian.soy.renderer.JsExpression;
import com.atlassian.soy.renderer.SoyClientFunction;
import com.atlassian.soy.renderer.SoyServerFunction;
import com.atlassian.bitbucket.util.NumberUtils;

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

/**
 * @since 3.1
 */
public class FormatSizeInBytesFunction implements SoyClientFunction, SoyServerFunction<String> {

    @Override
    public String apply(Object... objects) {
        return NumberUtils.formatSize(((Number) objects[0]).doubleValue());
    }

    @Override
    public JsExpression generate(JsExpression... args) {
        return new JsExpression("require('bitbucket/internal/util/text').formatSizeInBytes(" + args[0].getText() + ")");
    }

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

    @Override
    public Set<Integer> validArgSizes() {
        return Collections.singleton(1);
    }
}
