from urllib.parse import urlparse, urlunparse

def split_base_uri_tiled_viewer(uri):
    """
    Similar to _split_base_uri_containers() but parses out '/metadata' from base_uri.
    
    Splits the base uri from the containers and returns both base uri and a container list.
    This assumes that the given uri contains an api version string, metadata and container names
    separated by slashes, e.g. api/v1/metadata/data/special_container
    """
    parsed_url = urlparse(uri)
    path = parsed_url.path

    # Path is either empty or contains a leading slash only
    if len(path) < 2:
        return uri, []

    # Check if the path contains an api string that could be followed by containers
    if "/api" not in path:
        return uri, []

    # Get the base URL (scheme, netloc)
    base_url = f"{parsed_url.scheme}://{parsed_url.netloc}"

    # Split the path into segments
    segments = path.strip("/").split("/")

    # Find where the container names start (after metadata)
    container_start_idx = -1
    for i, segment in enumerate(segments):
        if segment == "metadata":
            container_start_idx = i + 1
            break

    if container_start_idx == -1 or container_start_idx >= len(segments):
        return uri, []

    # Base URI is everything up to and including 'metadata'
    base_path = "/".join(segments[:container_start_idx-1])
    base_uri = f"{base_url}/{base_path}"

    # Container names are everything after 'metadata'
    container_names = segments[container_start_idx:]

    return base_uri, container_names