"""
Google Translate wrapper and location→language detection.
Depends on: sources (for LOCATION_LANGS).
"""

import json
import re
import urllib.parse
import urllib.request
from functools import lru_cache

from engine.sources import LOCATION_LANGS

TRANSLATE_CACHE_SIZE = 500


@lru_cache(maxsize=TRANSLATE_CACHE_SIZE)
def _translate_cached(title: str, target_lang: str) -> str:
    """Cached translation implementation."""
    try:
        q = urllib.parse.quote(title)
        url = (
            "https://translate.googleapis.com/translate_a/single"
            f"?client=gtx&sl=en&tl={target_lang}&dt=t&q={q}"
        )
        req = urllib.request.Request(url, headers={"User-Agent": "mainline/0.1"})
        resp = urllib.request.urlopen(req, timeout=5)
        data = json.loads(resp.read())
        result = "".join(p[0] for p in data[0] if p[0]) or title
    except Exception:
        result = title
    return result


def detect_location_language(title):
    """Detect if headline mentions a location, return target language."""
    title_lower = title.lower()
    for pattern, lang in LOCATION_LANGS.items():
        if re.search(pattern, title_lower):
            return lang
    return None


def translate_headline(title: str, target_lang: str) -> str:
    """Translate headline via Google Translate API (zero dependencies)."""
    return _translate_cached(title, target_lang)
