import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Sidebar rename preserves title alignment and dismisses thread details", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Rename demo");
  await t3.thread({ project, title: "Update dependencies", messages: ["fourth"] });
  await t3.thread({ project, title: "Improve search results", messages: ["third"] });
  const other = await t3.thread({ project, title: "Fix login redirect", messages: ["second"] });
  const thread = await t3.thread({ project, title: "Add dark mode toggle", messages: ["first"] });
  await t3.open(thread);
  const threadUrl = await browser.url();
  const pause = (ms: number) => browser.evaluate(ms => new Promise(resolve => setTimeout(() => resolve(null), ms)), ms);
  const measure = () => browser.evaluate(() => {
    const rows = [...document.querySelectorAll('[data-testid^="sidebar-row-"]')];
    const row = rows.find(el => el.getAttribute("aria-label")?.includes("Add dark mode toggle"))!;
    const input = row.querySelector('input[aria-label="Thread title"]') as HTMLInputElement | null;
    const title = input ?? [...row.querySelectorAll("span")].find(el => el.textContent === "Add dark mode toggle")!;
    const rect = title.getBoundingClientRect();
    const style = getComputedStyle(title);
    const range = document.createRange();
    if (!input) range.selectNodeContents(title);
    const textLeft = input ? rect.x + parseFloat(style.borderLeftWidth) + parseFloat(style.paddingLeft) : range.getBoundingClientRect().x;
    const branch = [...row.querySelectorAll("*")].find(el => el.textContent === "main");
    const tooltip = [...document.querySelectorAll('[data-slot="tooltip-popup"]')].find(el => el.textContent?.includes("Add dark mode toggle"));
    const tooltipRect = tooltip?.getBoundingClientRect();
    return { textLeft, titleHeight: rect.height, fontWeight: style.fontWeight, rowHeight: row.getBoundingClientRect().height, branchTop: branch?.getBoundingClientRect().top ?? null, nextRowTop: rows[rows.indexOf(row) + 1]?.getBoundingClientRect().top ?? null, inputLeft: input ? rect.x : null, inputRight: input ? rect.right : null, tooltipVisible: !!tooltipRect?.height, tooltipLeft: tooltipRect?.x ?? null, rowRight: row.getBoundingClientRect().right };
  });
  const observations = [];
  async function rename(label: string, variant: string, keyboard = false) {
    const row = browser.locator(`[data-testid="sidebar-row-${variant}"][aria-label*="Add dark mode toggle"]`);
    await row.hover();
    await pause(1500);
    const before = await measure();
    await app.screenshot(`${label}-hovered`);
    if (keyboard) await row.press("F2");
    else await row.doubleTap();
    const input = screen.getByRole("textbox", "Thread title");
    await pause(500);
    await app.screenshot(`${label}-entered-rename`);
    await input.press("End");
    await browser.keyboard.type(" X");
    await pause(1500);
    const after = await measure();
    observations.push({ label, before, after });
    console.log("RENAME_METRICS", JSON.stringify({ label, before, after }));
    await app.screenshot(`${label}-renaming`);
    await input.press("Escape");
    await browser.mouse.move(650, 400);
    await pause(500);
  }
  await rename("01-card", "card");
  await t3.open(other);
  await rename("02-receded-card-F2", "card", true);
  await app.open("/settings/labs");
  await screen.getByRole("switch", "Boring sidebar (group by project)").check();
  await screen.getByRole("switch", "Two-line folder rows").uncheck();
  await app.open(threadUrl);
  await rename("03-folder", "folder");
  await app.open("/settings/labs");
  await screen.getByRole("switch", "Two-line folder rows").check();
  await app.open(threadUrl);
  await rename("04-folder-multiline", "folder");
  await app.open("/settings/labs");
  await screen.getByRole("switch", "Boring sidebar (group by project)").uncheck();
  await app.open(threadUrl);
  const card = browser.locator('[data-testid="sidebar-row-card"][aria-label*="Add dark mode toggle"]');
  await card.hover();
  await card.getByRole("button", "Settle thread", { exact: true }).tap();
  await app.open(threadUrl);
  await rename("05-slim", "slim");
  for (const { before, after } of observations) {
    expect.soft(after.tooltipVisible).toBe(false);
    expect.soft(after.textLeft).toBe(before.textLeft);
    expect.soft(after.titleHeight).toBe(before.titleHeight);
    expect.soft(after.branchTop).toBe(before.branchTop);
    expect.soft(after.rowHeight).toBe(before.rowHeight);
  }
});
