from typing import Mapping
from typing import ByteString


def encode_compact_tlv(data: Mapping[int, ByteString]) -> bytes:
    """
    Args:
        data: Mapping from tags to values

    Returns:
        COMPACT-TLV encoded data

    Raises:
        ValueError: Tag or Length too big
    """

    result = b""
    for tag, value in data.items():
        if not 0 <= tag <= 15:
            raise ValueError(f"Tag {tag} out of range [0, 15]")
        if len(value) > 15:
            raise ValueError(f"Value of tag {tag} too big: {len(value)}")
        result += bytes([tag * 16 + len(value)]) + value
    return result