from urllib.parse import urljoin, urlsplit


def make_absolutizing_url_mutator(baseurl):
    """Return a function that makes relative URLs absolute.

    Parameters
    ----------
    baseurl : string, absolute URL
        The absolute URL with which to combine relative URLs

    Returns
    -------
    A mutator function suitable for use with :meth:`wwt_data_formats.abcs.UrlContainer.mutate_urls`.

    Notes
    -----
    This function is designed for usage with :meth:`wwt_data_formats.abcs.UrlContainer.mutate_urls`.
    It returns a mutator function that can be passed to this method. The mutator will take
    relative URLs and make them absolute by combining them with the *baseurl* argument. Input URLs
    that are already absolute will be unchanged.

    """

    def mutator(url):
        if not url:
            return url
        if urlsplit(url).netloc:
            return url  # this URL is absolute
        return urljoin(baseurl, url)

    return mutator