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

test("Popup search row padding focuses its input", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("demo");
  await t3.open();
  await browser.locator('[data-chat-provider-model-picker="true"]').first().click();
  const search = screen.getByPlaceholder("Search models...");
  await search.tap();
  const focused: boolean[] = [];
  for (const spot of ["right", "left", "top", "icon", "centre"]) {
    const point = await browser.evaluate((spot) => {
      const input = document.querySelector<HTMLInputElement>('input[placeholder="Search models..."]')!;
      input.blur();
      const row = input.closest<HTMLElement>(".shrink-0.px-3")!;
      const r = row.getBoundingClientRect();
      const i = input.getBoundingClientRect();
      const icon = row.querySelector('svg')!.getBoundingClientRect();
      return spot === "right" ? { x: r.right - 4, y: i.top + i.height / 2 }
        : spot === "left" ? { x: r.left + 4, y: i.top + i.height / 2 }
        : spot === "top" ? { x: r.left + r.width / 2, y: r.top + 3 }
        : spot === "icon" ? { x: icon.left + icon.width / 2, y: icon.top + icon.height / 2 }
        : { x: i.left + i.width / 2, y: i.top + i.height / 2 };
    }, spot);
    await browser.mouse.move(point.x, point.y);
    await browser.mouse.down();
    await browser.mouse.up();
    focused.push(await browser.evaluate(() => document.activeElement?.getAttribute("placeholder") === "Search models..."));
  }
  const right = await browser.evaluate(() => {
    const input = document.querySelector<HTMLInputElement>('input[placeholder="Search models..."]')!;
    input.blur();
    const r = input.closest<HTMLElement>(".shrink-0.px-3")!.getBoundingClientRect();
    const i = input.getBoundingClientRect();
    return { x: r.right - 4, y: i.top + i.height / 2 };
  });
  await browser.mouse.move(right.x, right.y);
  await browser.mouse.down();
  await browser.mouse.up();
  await browser.keyboard.type("gpt");
  await app.screenshot("right-padding-then-type-gpt");
  expect(focused).toEqual([true, true, true, true, true]);
  await expect(search).toHaveValue("gpt");
});
