# -*- coding: utf-8 -*-

"""Additional utilities to work with URLs."""

__all__ = ['join']

from urllib import parse


def join(base: str, *args: str) -> str:
    """
    Join multiple URLS into a single one.

    :param base: Base URL to join parts to.
    :param args: URL parts to join.
    :return: Joined URL.
    """
    arg: str
    for arg in args:

        # This should prevent trimming of the last element for relative joins:
        # parse.urljoin('https://example.org/A', 'B/C') == 'https://example.org/B/C'
        # parse.urljoin('https://example.org/A/', 'B/C') == 'https://example.org/A/B/C'
        if not base.endswith('/'):
            base += '/'

        base = parse.urljoin(base, arg)

    return base

