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

test("Bracketed and multiline thread labels paste as a whole thread chip", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("release");
  const linked = await t3.thread({ project, title: "Release checklist", messages: ["Checklist"], model: "multi-turn" });
  const target = await t3.thread({ project, title: "Follow-up", messages: ["Start"], model: "multi-turn" });
  await t3.open(target);
  const composer = screen.getByRole("textbox");
  const results: string[] = [];
  for (const [label, capture] of [["Release", "flat-label-control"], ["Release [draft]", "nested-bracket-label"], ["Release [draft]\nnotes", "multiline-bracket-label"]]) {
    await composer.fill("");
    await composer.tap();
    await browser.evaluate(({ threadId, label }: { threadId: string; label: string }) => {
      const environmentId = decodeURIComponent(window.location.pathname.split("/")[1]!);
      const href = `https://app.t3.codes/threads/${environmentId}/${encodeURIComponent(threadId)}`;
      const clipboardData = new DataTransfer();
      clipboardData.setData("text/plain", `See [${label}](${href})`);
      const editor = document.activeElement;
      if (!editor?.matches('[contenteditable="true"]')) throw new Error("Composer is not focused");
      return editor.dispatchEvent(new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }));
    }, { threadId: linked.threadId, label });
    await composer.getByText("Release checklist", { exact: true }).waitFor();
    await app.screenshot(capture!);
    results.push(await browser.evaluate(() => document.querySelector('[role="textbox"]')!.textContent!.replace(/\u200b/g, "").replace(/\s+/g, " ").trim()));
  }
  expect(results).toEqual(["See RERelease checklist", "See RERelease checklist", "See RERelease checklist"]);
});
