from typing import Sequence
from typing import Mapping
from typing import Any
from typing import MutableMapping
from typing import AbstractSet


def consistent_refvars(events: Sequence[Mapping[str, Any]]) -> bool:
    """Detects whether all refvars with identical IDs use same constraints.

    Args:
        events: List of Event (refer to TypeScript class Event in the frontend file for format)
            objects.

    Returns:
        True if all refvars of same ID have consistent constraints, False otherwise.
    """
    refvar_dict: MutableMapping[str, AbstractSet[str]] = {}
    for event in events:
        if "args" in event and event["args"]:
            for arg in event["args"]:
                if "refvar" not in arg or arg["refvar"] is None:
                    continue
                if (
                    arg["refvar"] in refvar_dict
                    and set(arg["constraints"]) != refvar_dict[arg["refvar"]]
                ):
                    return False
                else:
                    refvar_dict[arg["refvar"]] = set(arg["constraints"])
    return True