package com.atlassian.stash.internal.scm.git.command.revlist;

import com.atlassian.bitbucket.io.LineInputHandler;
import com.google.common.collect.ImmutableSet;

import javax.annotation.Nonnull;
import java.util.Set;

import static com.atlassian.bitbucket.util.MoreCollectors.toImmutableSet;

/**
 * @since 1.1
 */
public class RevListInputHandler extends LineInputHandler {

    public RevListInputHandler(Iterable<String> refs) {
        super(refs);
    }

    @Nonnull
    public static RevListInputHandler excludingRefs(@Nonnull Set<String> refs) {
        return new RevListInputHandler(asExclusions(refs));
    }

    @Nonnull
    public static RevListInputHandler forRefs(Set<String> include, Set<String> exclude) {
        ImmutableSet.Builder<String> builder = ImmutableSet.builder();
        if (include != null) {
            builder.addAll(include);
        }
        if (exclude != null) {
            builder.addAll(asExclusions(exclude));
        }

        return new RevListInputHandler(builder.build());
    }

    private static Set<String> asExclusions(@Nonnull Set<String> refs) {
        return refs.stream()
                .map(ref -> '^' + ref)
                .collect(toImmutableSet());
    }
}
