import importlib
import pkgutil
import inspect


def criteria_finder(parent_module_name="chalicelib.criteria"):
    """
    A helper function returning a set with all classes in the chalicelib.criteria submodules
    having a class attribute named "active" with value `True`.
    """
    parent_module = importlib.import_module(parent_module_name)
    active_criteria = set()

    for loader, module_name, ispkg in pkgutil.iter_modules(parent_module.__path__):

        parent_module_import_path = f"{parent_module.__name__}.{module_name}"

        members = inspect.getmembers(
            importlib.import_module(parent_module_import_path), inspect.isclass
        )
        for name, cls in members:
            is_criteria_default = "CriteriaDefault" in [
                supercls.__name__ for supercls in inspect.getmro(cls)
            ]
            if is_criteria_default:
                active_criteria.add(f"{parent_module_import_path}.{name}")

    return active_criteria