from pathlib import Path
from typing import Optional
from typing import Union
from typing import List
from typing import Iterator
import itertools


def directories(
    PATH: Path,
    INCLUDES: Optional[Union[str, List[str]]] = None,
    EXCLUDES: Optional[Union[str, List[str]]] = None,
) -> Iterator[Path]:
    """Iterate over the directories of a given path."""
    # Parse inputs.
    if INCLUDES is None:
        INCLUDES = [
            "*",
        ]
    elif type(INCLUDES) is str:
        INCLUDES = [
            INCLUDES,
        ]
    if EXCLUDES is None:
        EXCLUDES = []
    elif type(EXCLUDES) is str:
        EXCLUDES = [
            EXCLUDES,
        ]

    # Filter directory.
    FILTER_INCLUDING = itertools.chain.from_iterable(PATH.glob(F) for F in INCLUDES)
    FILTER_EXCLUDING = itertools.chain.from_iterable(PATH.glob(F) for F in EXCLUDES)
    return iter(set(FILTER_INCLUDING) - set(FILTER_EXCLUDING))