from pathlib import Path
from typing import List
import itertools


def find_typeshed_search_paths(typeshed_root: Path) -> List[Path]:
    """
    Given the root of typeshed, find all subdirectories in it that can be used
    as search paths for Pyre.
    """
    search_path = []
    third_party_root = typeshed_root / "stubs"
    third_party_subdirectories = (
        sorted(third_party_root.iterdir()) if third_party_root.is_dir() else []
    )
    for typeshed_subdirectory in itertools.chain(
        [typeshed_root / "stdlib"], third_party_subdirectories
    ):
        if typeshed_subdirectory.is_dir():
            search_path.append(typeshed_subdirectory)
    return search_path