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

import com.atlassian.stash.internal.scm.git.command.AbstractGitCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.config.GitConfigType;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.scm.git.command.config.TypedGitConfigBuilder;

import javax.annotation.Nonnull;

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

/**
 * @since 2.5
 */
public abstract class AbstractTypedGitConfigBuilder<T extends TypedGitConfigBuilder<T>>
        extends AbstractGitCommandBuilder<T>
        implements TypedGitConfigBuilder<T> {

    protected final String name;

    private GitConfigType type;

    protected AbstractTypedGitConfigBuilder(GitScmCommandBuilder builder, String name) {
        super(builder);

        this.name = checkNotBlank(name, "name");

        type = GitConfigType.ANY;
    }

    @Nonnull
    @Override
    public T type(@Nonnull GitConfigType value) {
        type = checkNotNull(value, "type");

        return self();
    }

    @Override
    protected void applyArguments() {
        if (type.isFlagged()) {
            builder.argument(type.getFlag());
        }
    }
}
