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

test("Pointer highlights clipped suggestions without moving the list; keyboard still scrolls", WEB, async ({ t3, app, browser, screen }) => {
  const files = Object.fromEntries(Array.from({length: 100}, (_, i) => [`entry-${String(i).padStart(3, "0")}.ts`, "export {};\n"]));
  const project = await t3.project("Suggestion demo", files);
  const thread = await t3.thread({project, title: "Suggestion hover", messages: ["hello"]});
  await t3.open(thread);
  await screen.getByRole("textbox").fill("@");
  await expect.poll(() => browser.evaluate(() => document.querySelectorAll('[data-composer-command-drawer] [role="option"]').length)).toBeGreaterThan(20);
  const state = () => browser.evaluate(() => {
    const vp = document.querySelector<HTMLElement>('[data-composer-command-drawer] [data-slot="scroll-area-viewport"]')!;
    const rect = vp.getBoundingClientRect();
    const rows = [...vp.querySelectorAll<HTMLElement>('[role="option"]')];
    const clipped = rows.find(row => { const r = row.getBoundingClientRect(); return r.top < rect.bottom - 3 && r.bottom > rect.bottom + 0.1; });
    const row = clipped ?? rows.filter(row => { const r = row.getBoundingClientRect(); return r.top < rect.bottom - 3 && r.bottom > rect.top; }).at(-1)!;
    const r = row.getBoundingClientRect();
    return {top: vp.scrollTop, x: r.left + r.width / 2, y: Math.min(r.bottom, rect.bottom) - 5, id: row.id, label: row.textContent};
  });
  const top = () => browser.evaluate(() => document.querySelector<HTMLElement>('[data-composer-command-drawer] [data-slot="scroll-area-viewport"]')!.scrollTop);
  await app.screenshot("01-before-pointer");
  const initial = await state();
  await browser.mouse.move(initial.x, initial.y);
  await browser.mouse.move(initial.x + 1, initial.y + 1);
  await browser.mouse.move(initial.x + 2, initial.y + 2);
  await app.screenshot("02-after-pointer-moves");
  const hoverTop = await top();
  const selectedAtPointer = await browser.evaluate(({x,y}) => document.elementFromPoint(x,y)?.closest('[role="option"]')?.getAttribute('aria-selected'), {x:initial.x+2,y:initial.y+2});
  await browser.keyboard.press("ArrowDown");
  await app.screenshot("03-keyboard-navigation");
  const keyboardVisible = await browser.evaluate(() => {
    const row = document.querySelector<HTMLElement>('[data-composer-command-drawer] [aria-selected="true"]')!;
    const vp = row.closest('[data-slot="scroll-area-viewport"]')!;
    const r = row.getBoundingClientRect(), v = vp.getBoundingClientRect();
    return r.top >= v.top && r.bottom <= v.bottom;
  });
  await browser.mouse.move(initial.x, initial.y - 100);
  await browser.mouse.wheel(0,214);
  await expect.poll(top).toBeGreaterThan(100);
  await app.screenshot("04-wheel-scrolled");
  const wheeled = await state();
  await browser.mouse.move(wheeled.x,wheeled.y);
  await browser.mouse.move(wheeled.x+1,wheeled.y+1);
  await app.screenshot("05-hover-after-wheel");
  const wheelHoverTop = await top();
  expect({hoverTop, selectedAtPointer, keyboardVisible, wheelHoverTop}).toEqual({hoverTop:initial.top, selectedAtPointer:"true", keyboardVisible:true, wheelHoverTop:wheeled.top});
});
