"""UnicodeNormalizer --- applies Unicode normalization form before scanning."""

from __future__ import annotations

from .base import BaseNormalizer


class UnicodeNormalizer(BaseNormalizer):
    """Applies Unicode normalization form (default: NFKC).

    NFKC is the recommended form for security --- it handles fullwidth ASCII
    (U+FF21 -> A), compatibility fractions, ligatures, and more. Does NOT
    map Cyrillic/Greek lookalikes to ASCII --- use HomoglyphNormalizer for that.

    Args:
        form: Normalization form --- 'NFC', 'NFKC', 'NFD', or 'NFKD'.
              Default is 'NFKC'.
    """

    def __init__(self, form: str = "NFKC") -> None:
        if form not in ("NFC", "NFKC", "NFD", "NFKD"):
            raise ValueError(f"Invalid Unicode normalization form: {form!r}")
        self._form = form

    def to_normalizer_spec(self) -> dict:
        return {"type": "unicode", "config": {"form": self._form}}
