"""Title normalization, applied identically to photo titles and BGG names.

Casefold, strip accents (é → e), map & → "and", drop punctuation and
leading/embedded articles, collapse whitespace.
"""

from __future__ import annotations

import re
import unicodedata

_ARTICLES = {"the", "a", "an"}
_NON_ALNUM = re.compile(r"[^a-z0-9 ]+")


def normalize_title(title: str) -> str:
    s = unicodedata.normalize("NFKD", title)
    s = "".join(c for c in s if not unicodedata.combining(c))
    s = s.casefold().replace("&", " and ")
    s = _NON_ALNUM.sub(" ", s)
    tokens = [t for t in s.split() if t not in _ARTICLES]
    return " ".join(tokens)
