from unittest.mock import patch
from src.markdown_helper import MarkdownRenderer
from src.markdown_table import parse_tables

def _mock_table_calls(mock_imgui):
 mock_imgui.TableFlags_ = type("T", (), {"borders": 1, "row_bg": 2, "resizable": 4})()
 mock_imgui.begin_table.return_value = True
 mock_imgui.table_next_column = lambda: None
 mock_imgui.table_next_row = lambda: None
 mock_imgui.table_headers_row = lambda: None
 mock_imgui.text = lambda *a, **k: None
 mock_imgui.end_table = lambda: None

def test_tables_in_crlf_text_all_get_masked():
 text = (
  "# Title\r\n"
  "\r\n"
  "| A | B |\r\n"
  "|---|---|\r\n"
  "| 1 | 2 |\r\n"
  "\r\n"
  "Some prose.\r\n"
  "\r\n"
  "| X | Y |\r\n"
  "|---|---|\r\n"
  "| 3 | 4 |\r\n"
 )
 blocks = parse_tables(text)
 assert len(blocks) == 2
 with patch("src.markdown_helper.imgui_md") as mock_md, patch("src.markdown_helper.imgui") as mock_imgui, patch("src.markdown_table.imgui") as mock_table_imgui:
  _mock_table_calls(mock_table_imgui)
  MarkdownRenderer().render(text, context_id="t")
  full = "".join(str(c) for c in mock_md.render.call_args_list)
  for needle in ["| A | B |", "|---|", "| 1 | 2 |", "| X | Y |", "| 3 | 4 |"]:
   assert needle not in full, f"Raw table text leaked to imgui_md: {needle!r} in calls: {full!r}"

def test_duplicate_table_content_both_get_replaced():
 text = (
  "| A | B |\r\n"
  "|---|---|\r\n"
  "| 1 | 2 |\r\n"
  "\r\n"
  "Middle prose.\r\n"
  "\r\n"
  "| A | B |\r\n"
  "|---|---|\r\n"
  "| 1 | 2 |\r\n"
 )
 blocks = parse_tables(text)
 assert len(blocks) == 2
 with patch("src.markdown_helper.imgui_md") as mock_md, patch("src.markdown_helper.imgui") as mock_imgui, patch("src.markdown_table.imgui") as mock_table_imgui:
  _mock_table_calls(mock_table_imgui)
  MarkdownRenderer().render(text, context_id="dup")
  full = "".join(str(c) for c in mock_md.render.call_args_list)
  assert "| A | B |" not in full, f"Raw table text leaked on duplicate: {full!r}"


def test_render_applies_current_syntax_palette(monkeypatch):
    """When a theme is active, the render path should call apply_syntax_palette
    so new code-block editors pick up the theme's syntax palette."""
    from unittest.mock import patch
    from src import theme_2 as theme

    class _Tracker:
        last_palette = None
        last_call = False

    def fake_apply(palette_id):
        _Tracker.last_palette = palette_id
        _Tracker.last_call = True

    monkeypatch.setattr(theme, "apply_syntax_palette", fake_apply)
    monkeypatch.setattr(theme, "_current_palette", "solarized_dark")
    monkeypatch.setattr(theme, "get_syntax_palette_for_theme", lambda n: "dark")

    from src.markdown_helper import MarkdownRenderer
    with patch("src.markdown_helper.imgui_md") as mock_md, patch("src.markdown_helper.imgui") as mock_imgui, patch("src.markdown_table.imgui") as mock_table_imgui:
        _mock_table_calls(mock_table_imgui)
        MarkdownRenderer().render("hello world", context_id="p")
        assert _Tracker.last_call, "expected apply_syntax_palette to be called"

