/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Markdown tree offers rendered preview even after source mode", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("markdown-demo", {
    "README.md": "# Markdown preview demo\n\nRead this **rendered document** directly from the file tree.\n\n- Preview from the context menu\n- Remember the rendering preference\n",
    "folder/notes.txt": "A regular folder",
  });
  const thread = await t3.thread({ project, title: "Read project documentation", messages: ["Start"] });
  await browser.setViewport({ width: 1440, height: 1000 });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Files", { exact: true }).tap();
  const treeAction = async (path: string, context: boolean) => {
    await browser.evaluate(({ path, context }) => {
      for (const host of document.querySelectorAll("*")) {
        const row = host.shadowRoot?.querySelector<HTMLElement>(`[data-item-path="${path}"]`);
        if (row) {
          if (context) {
            const box = row.getBoundingClientRect();
            row.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true, composed: true, cancelable: true, button: 2, clientX: box.x + 75, clientY: box.y + box.height / 2 }));
          }
          else row.click();
          return true;
        }
      }
      throw new Error(`Tree row missing: ${path}`);
    }, { path, context });
  };
  await screen.getByRole("button", "Refresh workspace files").waitFor();
  await treeAction("README.md", false);
  await screen.getByRole("button", "Show rendered markdown").waitFor();
  await screen.getByRole("button", "Show rendered markdown").tap();
  await screen.getByRole("button", "Show markdown source").tap();
  await screen.getByRole("button", "Show rendered markdown").waitFor();
  await treeAction("folder/", true);
  await screen.getByRole("menuitem", "Copy mention", { exact: true }).waitFor();
  await app.screenshot("folder-context-menu");
  await expect(screen.getByRole("menuitem", "Open preview", { exact: true })).not.toBeVisible();
  await browser.keyboard.press("Escape");
  await treeAction("README.md", true);
  await screen.getByRole("menuitem", "Copy mention", { exact: true }).waitFor();
  await app.screenshot("markdown-context-menu");
  await expect(screen.getByRole("menuitem", "Open preview", { exact: true })).toBeVisible();
  await screen.getByRole("menuitem", "Open preview", { exact: true }).tap();
  await screen.getByRole("button", "Show markdown source").waitFor();
  await app.screenshot("rendered-markdown");
  await expect(screen.getByRole("heading", "Markdown preview demo", { exact: true })).toBeVisible();
  await expect.poll(() => browser.evaluate(() => localStorage.getItem("t3code.renderMarkdown"))).toBe("true");
  await t3.open(thread);
  await screen.getByRole("button", "Show markdown source").waitFor();
  await app.screenshot("rendering-preference-persists");
  await expect(screen.getByRole("heading", "Markdown preview demo", { exact: true })).toBeVisible();
});
