from pathlib import Path
import tempfile
import atexit
import shutil


def create_temp_dir(remove_at_exit: bool = True) -> Path:
    """Create a temporary directory.

    Args:
        remove_at_exit (bool, optional): If True, delete directory when
            application exists. Defaults to True.

    Returns:
        Path: The path to the created directory.
    """
    temp_dir = Path(tempfile.mkdtemp())
    if remove_at_exit:
        atexit.register(lambda: shutil.rmtree(temp_dir, ignore_errors=True))
    return temp_dir