from __future__ import annotations

import string

# Extensions that should never produce embeddable text even if parsed as code.
_NON_EMBEDDABLE_EXTENSIONS = frozenset(
    {
        ".avif",
        ".bin",
        ".bmp",
        ".dll",
        ".exe",
        ".gif",
        ".gz",
        ".ico",
        ".jpeg",
        ".jpg",
        ".pdf",
        ".png",
        ".so",
        ".svg",
        ".tar",
        ".webp",
        ".woff",
        ".woff2",
        ".zip",
    }
)


def is_non_embeddable_path(file_path: str | None) -> bool:
    if not file_path:
        return False

    lowered = file_path.strip().lower()
    for ext in _NON_EMBEDDABLE_EXTENSIONS:
        if lowered.endswith(ext):
            return True
    return False


def is_embeddable_text(text: str | None, *, file_path: str | None = None) -> bool:
    """Return True when text is safe and useful to send to the embedding provider."""
    if is_non_embeddable_path(file_path):
        return False

    if not text or not text.strip():
        return False

    if "\x00" in text:
        return False

    # Skip chunks dominated by replacement chars from tolerant BSON decoding.
    if text.count("\ufffd") > max(3, len(text) // 20):
        return False

    printable = sum(1 for ch in text if ch in string.printable or ch.isspace())
    if len(text) > 0 and printable / len(text) < 0.85:
        return False

    return True
