import re
import html

EMAIL_REGEX = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'

# Obfuskovaci patterny - jen tohle
OBFUSCATION_PATTERNS = [
    # [at] [dot]
    (r'([a-zA-Z0-9._%+-]+)\s*\[at\]\s*([a-zA-Z0-9.-]+)\s*\[dot\]\s*([a-zA-Z]+)', r'\1@\2.\3'),
    # (at) (dot)
    (r'([a-zA-Z0-9._%+-]+)\s*\(at\)\s*([a-zA-Z0-9.-]+)\s*\(dot\)\s*([a-zA-Z]+)', r'\1@\2.\3'),
    # AT DOT (velke)
    (r'([a-zA-Z0-9._%+-]+)\s*AT\s*([a-zA-Z0-9.-]+)\s*DOT\s*([a-zA-Z]+)', r'\1@\2.\3'),
    # [@] [.]
    (r'([a-zA-Z0-9._%+-]+)\s*\[@\]\s*([a-zA-Z0-9.-]+)\s*\[\.\]\s*([a-zA-Z]+)', r'\1@\2.\3'),
    # espacios y puntos - jmeno at domena dot com
    (r'([a-zA-Z0-9._%+-]+)\s+at\s+([a-zA-Z0-9.-]+)\s+dot\s+([a-zA-Z]+)', r'\1@\2.\3', re.IGNORECASE),
]

def extract_emails_obfuscated(text):
    """Ziska emaily z obfuskovanych patternu."""
    if not text:
        return set()
    
    # Dekoduj HTML entity
    text = html.unescape(text)
    
    # Aplikuj vsechny patterny
    for pattern in OBFUSCATION_PATTERNS:
        if len(pattern) == 3:
            regex, replacement, flags = pattern
            text = re.sub(regex, replacement, text, flags=flags)
        else:
            regex, replacement = pattern
            text = re.sub(regex, replacement, text)
    
    # Najdi vsechny emaily
    emails = set(re.findall(EMAIL_REGEX, text))
    
    return emails