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

import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.bitbucket.i18n.KeyedMessage;
import com.atlassian.bitbucket.repository.Repository;
import com.google.common.collect.Iterables;
import org.apache.commons.lang3.StringUtils;

import java.util.Set;
import java.util.stream.Collectors;

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

public class MergeBaseExitHandler extends AbstractMergeBaseExitHandler {

    protected final Set<String> commits;

    public MergeBaseExitHandler(I18nService i18nService, Repository repository, Set<String> commits) {
        super(i18nService, repository);

        checkArgument(commits != null && commits.size() > 1,  "at least two commits must be given");
        checkArgument(commits.stream().allMatch(StringUtils::isNoneBlank),
                "all of the commits must be non-blank");

        this.commits = commits;
    }

    @Override
    protected KeyedMessage noCommonAncestorMessage() {
        return i18nService.createKeyedMessage("bitbucket.git.no.common.ancestor",
                Iterables.getFirst(commits, null), commits.stream().skip(1).collect(Collectors.joining(", ")));
    }

}
