import functools
import hashlib
import threading


_get_user_id_lock = threading.Lock()


@functools.lru_cache(maxsize=1024)
def get_device_id(user_id: str, mac_addr: str) -> str:
    """
    Returns an anonymized MAC address.

    """
    mac_addr = mac_addr.replace(":", "").lower()
    return get_hash(user_id, mac_addr)


def get_hash(user_id: str, input_str: str) -> str:
    """
    Returns a HMAC of the input string with user_id, using SHA-256, and
    return the first 16 bytes.

    """
    input_str = input_str + user_id

    return hashlib.sha256(input_str.encode("utf-8")).hexdigest()[:16]
