"""
Shared logging utilities for safe string encoding.
"""


# Map common Unicode arrows to ASCII fallbacks to avoid Windows cp1252 errors.
ARROW_MAP = {
    ord("\u2190"): "<-",  # left arrow
    ord("\u2192"): "->",  # right arrow
    ord("\u2194"): "<->",  # left-right arrow
    ord("\u21a0"): "->",  # rightwards arrow with hook
    ord("\u21a3"): "->",  # rightwards arrow with tail
    ord("\u21a6"): "->",  # rightwards arrow with bar
    ord("\u21d0"): "<-",  # double-line left arrow
    ord("\u21d2"): "->",  # double-line right arrow
    ord("\u21d4"): "<->",  # double-line left-right arrow
    ord("\u21c4"): "<->",  # rightwards over leftwards arrows
    ord("\u21c6"): "<->",  # leftwards over rightwards arrows
}


def _s(msg: object) -> str:
    """
    Safe string encoding for Windows with arrow translation.
    Prevents UnicodeEncodeError on cp1252 consoles.

    Args:
        msg: Any object that can be converted to string

    Returns:
        Windows-safe string with arrows translated
    """
    s = str(msg)
    try:
        s = s.translate(ARROW_MAP)
        return s.encode("cp1252", "replace").decode("cp1252")
    except Exception:
        return s
