from typing import Tuple, Dict, Any, Set, List
from .normalize import normalize_mac, is_mac

def extract_gateway_macs(data: object) -> Tuple[Set[str], Dict[str, Any], List[str]]:
    """
    ritorna:
      - allowed MAC (validi)
      - mapping mac->record
      - lista raw MAC invalidi (normalizzati quando possibile)
    """
    allowed: Set[str] = set()
    by_mac: Dict[str, Any] = {}
    invalid: List[str] = []

    if not isinstance(data, list):
        return allowed, by_mac, invalid

    for item in data:
        if not isinstance(item, dict):
            continue
        raw = item.get("mac", "")
        mac = normalize_mac(raw)
        if mac and is_mac(mac):
            allowed.add(mac)
            by_mac[mac] = item
        else:
            if raw:
                invalid.append(str(raw))

    return allowed, by_mac, invalid
