package com.atlassian.bitbucket.scm.git.command.revlist;

import com.atlassian.bitbucket.commit.CommitListMergeFilter;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * Enumerates the possible merge commit modes for {@code git rev-list}, controlling use of the {@code --merges},
 * {@code --no-merges} and {@code --no-min-parents} flags.
 *
 * @since 4.8
 */
public enum GitRevListMerges {

    /**
     * Apply {@code git rev-list}'s default behaviour.
     */
    DEFAULT,
    /**
     * Exclude merge commits.
     */
    EXCLUDE("--no-merges"),
    /**
     * Include merge commits. This does not exclude non-merge commits.
     */
    INCLUDE("--no-min-parents"),
    /**
     * Include <i>only</i> merge commits.
     */
    ONLY("--merges");

    private final String flag;

    GitRevListMerges() {
        flag = null;
    }

    GitRevListMerges(String flag) {
        this.flag = flag;
    }

    @Nullable
    public String getFlag() {
        return flag;
    }

    public boolean isFlagged() {
        return flag != null;
    }

    /**
     * @param value the {@link CommitListMergeFilter} value
     * @return a coerced value to a {@link CommitListMergeFilter}
     * @throws IllegalArgumentException if an unsupported {@link CommitListMergeFilter} is provided
     */
    @Nonnull
    public static GitRevListMerges fromFilter(@Nonnull CommitListMergeFilter value) {
        if (value == CommitListMergeFilter.EXCLUDE) {
            return EXCLUDE;
        }
        if (value == CommitListMergeFilter.INCLUDE) {
            return INCLUDE;
        }
        if (value == CommitListMergeFilter.ONLY) {
            return ONLY;
        }

        // This should _never_ happen unless it's null due to the test for this class
        throw new IllegalArgumentException("Value not a valid commit list merge filter value: " + value);
    }
}