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

const folder = "시안".normalize("NFD");
const filename = "시안.txt".normalize("NFD");
const file = `${folder}/${filename}`;

test("NFD Korean names compose for display while original paths still open", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Korean filenames", {
    [`${folder}/a.txt`]: "Original NFD folder opened successfully.\n",
    [file]: "ORIGINAL NFD PATH OPENED\nThis file exists only under decomposed Korean names.\n",
    "보고서/b.txt": "NFC control folder\n",
    "보고서/a.txt": "NFC same-basename control\n",
  });
  const thread = await t3.thread({ project, title: "Korean filename rendering", messages: [
    `Compare @${folder}/ and @보고서/\n\nSee [${filename}](${file}#L1) and [a.txt](${folder}/a.txt#L1), [a.txt](보고서/a.txt#L1).`,
  ] });
  await t3.open(thread);
  await screen.getByText("first fixture turn complete").waitFor();
  await screen.getByRole("button", /^Agent Compare/).waitFor();
  await screen.getByText("Syncing messages…").waitFor({ state: "hidden" });
  await screen.getByRole("button", "Toggle thread details panel").tap();
  const folded = await screen.getByRole("button", /^Agent Compare/).textContent();
  await screen.getByRole("button", /^Agent Compare/).tap();
  const link = browser.locator('[data-user-message-body]').getByRole("link", /txt/).first();
  await link.waitFor();
  await app.screenshot("02-message-file-link-chips");
  const chip = await link.textContent();
  await link.tap();
  await expect.poll(() => browser.evaluate(() => {
    for (const host of document.querySelectorAll("*")) {
      const content = host.shadowRoot?.querySelector("[data-content]");
      if (content?.textContent?.includes("ORIGINAL NFD PATH OPENED")) return true;
    }
    return false;
  })).toBe(true);
  await app.screenshot("03-original-nfd-file-opened");
  // Return to the thread to expose its composer after closing the file panel.
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", /^Agent Compare/).tap();
  await screen.getByText("Syncing messages…").waitFor({ state: "hidden" });
  await app.screenshot("01-folded-message-summary");
  await screen.getByRole("button", /^Agent Compare/).tap();
  const input = screen.getByTestId("composer-editor");
  await input.fill("@");
  await screen.getByRole("option", /보고서/).first().waitFor();
  await app.screenshot("04-at-menu-folders-and-parent-paths");
  const menuText = await browser.evaluate(() => [...document.querySelectorAll('[role="option"]')].map(e => e.textContent).join("\n"));
  await screen.getByRole("option", /시안.txt|시안.txt/).first().tap();
  await browser.locator('[data-composer-mention-chip]').waitFor();
  await app.screenshot("05-selected-mention-chip");
  const selected = await browser.locator('[data-composer-mention-chip]').textContent();
  await browser.setViewport({ width: 540, height: 900 });
  await browser.evaluate(() => (document.activeElement as HTMLElement)?.blur());
  await screen.getByRole("button", "Expand composer").waitFor();
  await app.screenshot("06-collapsed-composer-preview");
  const collapsed = await screen.getByRole("button", "Expand composer").textContent();
  expect.soft(folded).toContain("시안");
  expect.soft(chip).toContain("시안.txt");
  expect.soft(menuText).toContain("시안");
  expect.soft(selected).toContain("시안.txt");
  expect.soft(collapsed).toContain("시안.txt");
});
