"""Shared accessor for the ``web.enable_javascript_rendering`` setting.

The setting is read from a thread-local settings context (set on the
research's main thread) or from an explicit snapshot dict (passed
through closures so it works on LangGraph ``ToolNode`` worker threads
where ``threading.local()`` state from the research thread does not
propagate). Defaults to ``False`` when neither is available.

Why disabled by default: the production Docker image ships without
Chromium, so the headless-browser fallback (Crawl4AI/Playwright)
cannot succeed for the majority of users; before this gate landed each
attempt failed loudly and contributed to the memory growth reported
in issue #3826. In limited (mostly accidental) internal benchmark
comparisons between dev instances that happened to have Chromium
installed and routine Docker runs that did not, JS rendering did not
measurably improve research quality, and most regular benchmark runs
are on Docker without Chromium anyway. Users who specifically need
JS rendering can install Chromium (``playwright install --with-deps
chromium``) and toggle the setting in the UI.
"""

from __future__ import annotations

from typing import Optional

from ..config.thread_settings import get_bool_setting_from_snapshot


def read_js_rendering_setting(settings_snapshot: Optional[dict]) -> bool:
    """Return the current value of ``web.enable_javascript_rendering``.

    Args:
        settings_snapshot: Optional dict captured at the boundary where
            the calling code crosses into a worker thread. Pass the
            strategy's ``self.settings_snapshot`` if available; pass
            ``None`` when the caller has no snapshot — the helper will
            try thread-local context and finally fall back to ``False``.

    Returns:
        ``True`` only when the setting is explicitly enabled. ``bool(...)``
        coerces ``Any`` to a definite bool for mypy ``warn_return_any``.
    """
    return bool(
        get_bool_setting_from_snapshot(
            "web.enable_javascript_rendering",
            default=False,
            settings_snapshot=settings_snapshot,
        )
    )
