"""
Mozilla Readability-based content extractor.

Uses readabilipy (Python wrapper around Readability.js) to extract the
main article content from a page, stripping navigation, sidebars, and
other non-article elements at the DOM level.

Uses Node.js for full Readability.js support when available;
readabilipy falls back to pure-Python mode automatically.
"""

from typing import Optional

from loguru import logger

from .base import BaseExtractor


class ReadabilityExtractor(BaseExtractor):
    """Extract content using Mozilla Readability.js via readabilipy.

    Returns cleaned HTML (not plain text) so downstream extractors
    like justext can still detect headings and structure.
    """

    def extract(self, html: str) -> Optional[str]:
        if not html or not html.strip():
            return None

        try:
            from readabilipy import simple_json_from_html_string
        except ImportError:
            logger.warning(
                "readabilipy not installed — skipping Readability extraction"
            )
            return None

        try:
            # readabilipy checks for Node.js internally and falls back
            # to pure-Python mode automatically if it's not available.
            article = simple_json_from_html_string(html, use_readability=True)
        except Exception:
            logger.exception("readabilipy extraction failed")
            return None

        if not article:
            return None

        # Return HTML content only — preserves headings and structure
        # so downstream extractors (justext) can parse them properly.
        # Plain-text fallbacks are intentionally skipped: they would
        # break justext (which expects HTML) and the pipeline has its
        # own last-resort get_text() path.
        content = article.get("content")
        if content and isinstance(content, str) and content.strip():
            return content

        return None
