package com.atlassian.bitbucket.internal.importer.github;

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

import static com.atlassian.bitbucket.internal.importer.ImporterConstants.GITHUB_ENTERPRISE_API_EXT;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang3.StringUtils.stripToNull;

public abstract class AbstractUrlBuilder<T> {

    private final String defaultBaseUrl;
    private String url;

    protected AbstractUrlBuilder(@Nonnull String defaultBaseUrl) {
        this.defaultBaseUrl = requireNonNull(defaultBaseUrl, "defaultBaseUrl");
    }

    @Nonnull
    protected abstract T self();

    @Nonnull
    public T url(@Nullable String value) {
        url = stripToNull(value);
        return self();
    }

    @Nonnull
    protected String base() {
        return url != null ? url + GITHUB_ENTERPRISE_API_EXT : defaultBaseUrl;
    }
}