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

import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermissionService;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.soy.renderer.SoyServerFunction;
import com.google.common.collect.ImmutableSet;

import java.util.Set;

import static com.google.common.base.Preconditions.checkArgument;

public class HasRepositoryWriteFunction implements SoyServerFunction<Boolean> {

    private final PermissionService permissionService;

    public HasRepositoryWriteFunction(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

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

    @Override
    public Boolean apply(Object... args) {
        checkArgument(args[0] instanceof Repository, "Repository must be a repository object");
        Repository repository = (Repository) args[0];
        return permissionService.hasRepositoryPermission(repository, Permission.REPO_WRITE);
    }

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