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

test("Bold at the start of italic text survives paste", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Composer emphasis", messages: [] });
  await t3.open(thread);
  await screen.getByRole("textbox").tap();
  const lines = ["***Note:** see docs*", "___Note:__ see docs_", "control: **bold** and *italic* and ***both***"];
  for (let index = 0; index < lines.length; index++) {
    if (index) await browser.keyboard.press("Shift+Enter");
    await browser.evaluate((text) => {
      const editor = document.activeElement;
      if (!(editor instanceof HTMLElement) || !editor.isContentEditable) throw new Error("Composer is not focused");
      const clipboardData = new DataTransfer();
      clipboardData.setData("text/plain", text);
      editor.dispatchEvent(new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }));
    }, lines[index]);
  }
  await browser.evaluate(() => { (document.activeElement as HTMLElement)?.blur(); window.getSelection()?.removeAllRanges(); });
  await app.screenshot("blurred-composer-emphasis");
  const formatting = await browser.evaluate(() => {
    const editor = document.querySelector('[data-testid="composer-editor"]')!;
    return [...editor.querySelectorAll("strong")].filter(el => el.textContent === "Note:").map(el => ({
      bold: Number(getComputedStyle(el).fontWeight) >= 600,
      italic: getComputedStyle(el).fontStyle === "italic",
    }));
  });
  expect(formatting).toEqual([{ bold: true, italic: true }, { bold: true, italic: true }]);
});
