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

import com.atlassian.bitbucket.content.DiffWhitespace;

import javax.annotation.Nonnull;

/**
 * A flag for indicating how whitespace should be handled in a diff, such as being {@link #IGNORE_ALL_SPACE ignored}.
 */
public enum GitDiffWhitespace {

    /**
     * The default whitespace setting, where the exact changes of the diff are shown, whitespace and all.
     */
    SHOW(null),

    /**
     * Ignore all whitespace in the diff.
     */
    IGNORE_ALL_SPACE("--ignore-all-space");

    private final String flag;

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

    public String getFlag() {
        return flag;
    }

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

    /**
     * @param value the {@link DiffWhitespace} value
     * @return a coerced value to a {@link GitDiffWhitespace} value or {@link #SHOW}, but never {@code null}.
     * @throws IllegalArgumentException if an unsupported {@link DiffWhitespace} is provided
     */
    @Nonnull
    public static GitDiffWhitespace fromDiffWhitespace(@Nonnull DiffWhitespace value) {
        if (value == DiffWhitespace.IGNORE_ALL) {
            return IGNORE_ALL_SPACE;
        } else if (value == DiffWhitespace.SHOW) {
            return SHOW;
        } else {
            // This should _never_ happen unless it's null due to the test for this class
            throw new IllegalArgumentException("Value not a valid whitespace value: " + value);
        }
    }
}
