from typing import Dict
from pathlib import Path
from typing import Optional


def prioritize(paths: Dict[int, Path], purpose: str) -> Optional[Path]:
    """Returns highest-priority and existing filepath from ``paths``.

    Finds existing configuration or data file in ``paths`` with highest
    priority and returns it, otherwise returns ``None``.
    """
    for key in sorted(paths.keys(), reverse=True):
        if purpose == "config":
            if paths[key].exists():
                return paths[key]
        if purpose == "data":
            return paths[key]