import re


def set_base_url(domain_url, version=3, protocol='https', return_url=True):
    """This function gets the base URL for API calls when supplied with a domain URL. (e.g. ``community.example.com``)

    .. versionchanged:: 3.2.0
       Added the ``return_url`` parameter to determine if the base URL should be returned by the function.

    :param domain_url: The domain URL of the environment, with or without the http/https prefix
    :type domain_url: str
    :param version: The version of the REST API to utilize (Default: ``3``)
    :type version: int
    :param protocol: The protocol to leverage for the domain prefix if not already supplied (Default: ``https``)
    :type protocol: str
    :param return_url: Determines if the base URL should be returned by the function (``True`` by default)
    :type return_url: bool
    :returns: The base URL for API calls in string format (e.g. ``https://community.example.com/api/core/v3``)
    :raises: :py:exc:`TypeError`, :py:exc:`ValueError`
    """
    # Define global variable and dictionaries
    global base_url
    versions = {
        2: '/api/core/v2',
        3: '/api/core/v3'
    }
    protocols = {
        80: 'http://',
        443: 'https://',
        'http': 'http://',
        'https': 'https://'
    }

    # Add the appropriate protocol prefix if not present
    if not domain_url.startswith('http'):
        domain_url = f"{protocols.get(protocol)}{domain_url}"

    # Append the appropriate API path to the URL and return the bse URL
    domain_url = re.sub('/$', '', domain_url)
    base_url = f"{domain_url}{versions.get(version)}"
    if return_url:
        return base_url
    return