import re


def add_css_class(component, className):
    """
    Update the className property of a Dash component to include a CSS class name.
    If one or more classes already exist, the provided className will  be appended
    to the list. If the provided className is already present, no change will be made
    to the component.

    Note: This function mutates the provided component's className property in-place.

    :param component: Dash component
    :param className: CSS class name string
    """
    if not className:
        return

    if isinstance(className, list):
        # Join list to string for uniform processing below
        className = " ".join(className)
    elif not isinstance(className, str):
        raise ValueError(
            "className must be a string, but received value of type {typ}".format(
                typ=type(className)
            )
        )

    def normalize_and_split(classes_str):
        if not classes_str:
            return []

        classes_str = classes_str.strip()
        if not classes_str:
            return []

        classes_str = re.sub(r"\s+", " ", classes_str)
        return classes_str.split(" ")

    existing_classes = normalize_and_split(getattr(component, "className", None))
    new_classes = [
        cn for cn in normalize_and_split(className) if cn not in existing_classes
    ]
    all_classes = existing_classes + new_classes
    if new_classes is None:
        return
    else:
        component.className = " ".join(all_classes)