package com.atlassian.bitbucket.mesh.git.builder.config;

import com.atlassian.bitbucket.mesh.git.builder.AbstractGitProcessBuilder;
import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public abstract class AbstractTypedGitConfigBuilder<T extends TypedGitConfigBuilder<T>>
        extends AbstractGitProcessBuilder<T>
        implements TypedGitConfigBuilder<T> {

    protected final String name;

    private GitConfigType type;

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

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

        type = GitConfigType.ANY;
    }

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

        return self();
    }

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