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

const TABLE = "| Name | Role | Score |\n| :--- | :---: | ---: |\n| Alice | Engineer | 10 |\n| Bob | Designer | 20 |";
const EMPTY = "| Item | Note |\n| --- | --- |\n| Alpha |  |";

test("Selecting rendered table cells copies Markdown and complete table HTML", WEB, async ({ t3, app, browser, screen }) => {
  await browser.emulateMedia({ colorScheme: "dark" });
  const project = await t3.project("table-copy");
  const thread = await t3.thread({ project, title: "Table selection", messages: [TABLE + "\n\n" + EMPTY] });
  await t3.open(thread);
  await screen.getByRole("button", /^Agent/).first().tap();
  await screen.getByRole("columnheader", "Name", { exact: true }).waitFor();
  await browser.evaluate(() => {
    (window as any).tableCopies = [];
    window.addEventListener("copy", event => {
      (window as any).tableCopies.push({ text: event.clipboardData?.getData("text/plain") ?? "", html: event.clipboardData?.getData("text/html") ?? "" });
    });
    return null;
  });
  const cases = [
    { label: "full-table", from: "Name", to: "20", text: "| Name | Role | Score |\n| --- | :---: | ---: |\n| Alice | Engineer | 10 |\n| Bob | Designer | 20 |", cells: 9 },
    { label: "body-rows", from: "Alice", to: "20", text: "| Alice | Engineer | 10 |\n| --- | :---: | ---: |\n| Bob | Designer | 20 |", cells: 6 },
    { label: "two-cells", from: "Alice", to: "Engineer", text: "| Alice | Engineer |\n| --- | :---: |", cells: 2 },
    { label: "single-cell", from: "Alice", to: "Alice", text: "Alice", cells: 0 },
    { label: "empty-final-cell", from: "Item", to: "", text: EMPTY, cells: 4 },
  ];
  const actual = [];
  for (const sample of cases) {
    await screen.getByRole("textbox").fill("");
    await browser.evaluate(({ from, to }) => {
      const cells = [...document.querySelectorAll("[data-user-message-body] th, [data-user-message-body] td")];
      const start = cells.find(cell => cell.textContent === from)!;
      const end = cells.find(cell => cell.textContent === to)!;
      const range = document.createRange();
      range.setStart(start.firstChild ?? start, 0);
      range.setEnd(end.firstChild ?? end, end.firstChild ? (end.firstChild.textContent?.length ?? 0) : 0);
      window.getSelection()?.removeAllRanges();
      window.getSelection()?.addRange(range);
      return null;
    }, sample);
    await browser.keyboard.press("ControlOrMeta+C");
    const copy = await browser.evaluate(() => {
      const data = (window as any).tableCopies.at(-1);
      const doc = new DOMParser().parseFromString(data.html, "text/html");
      return { text: data.text, cells: doc.querySelectorAll("table th, table td").length };
    });
    actual.push({ label: sample.label, ...copy });
    await screen.getByRole("textbox").tap();
    await browser.keyboard.press("ControlOrMeta+V");
    await app.screenshot(sample.label + "-pasted");
  }
  expect(actual, "Multi-cell copies retain Markdown columns, alignments, rows, empty cells, and table HTML; single cells stay inline").toEqual(cases.map(({ label, text, cells }) => ({ label, text, cells })));
});
