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

test("Image preview stays open after both cross-edge drags", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo");
  const thread = await t3.thread({ project, title: "Image preview gestures", messages: ["Start"] });
  await t3.open(thread);
  const input = '[data-chat-composer-form] input[type="file"]';
  await browser.locator(input).waitFor({ state: "attached" });
  await browser.evaluate((selector) => {
    const canvas = document.createElement("canvas");
    canvas.width = 800; canvas.height = 500;
    const ctx = canvas.getContext("2d")!;
    ctx.fillStyle = "#245c82"; ctx.fillRect(0, 0, 800, 500);
    ctx.fillStyle = "#f4bc62"; ctx.fillRect(80, 80, 640, 340);
    ctx.fillStyle = "#163c54"; ctx.font = "40px sans-serif";
    ctx.fillText("Image preview drag test", 180, 260);
    const bytes = Uint8Array.from(atob(canvas.toDataURL("image/png").split(",")[1]!), c => c.charCodeAt(0));
    const data = new DataTransfer(); data.items.add(new File([bytes], "shot.png", { type: "image/png" }));
    const picker = document.querySelector<HTMLInputElement>(selector)!;
    picker.files = data.files; picker.dispatchEvent(new Event("change", { bubbles: true }));
    return null;
  }, input);
  const isOpen = () => browser.evaluate(() => !!document.querySelector('[role="dialog"] img'));
  const open = async () => {
    if (!(await isOpen())) await screen.getByRole("button", "Preview shot.png").tap();
    await browser.locator('[role="dialog"] img').waitFor();
  };
  const points = () => browser.evaluate(() => {
    const dialog = document.querySelector('[role="dialog"]')!;
    const d = dialog.getBoundingClientRect();
    const i = dialog.querySelector("img")!.getBoundingClientRect();
    const empty = { x: d.left + 8, y: d.top + d.height / 2 };
    if (document.elementFromPoint(empty.x, empty.y) !== dialog) throw new Error("Empty area must hit dialog");
    return { empty, image: { x: i.left + i.width / 2, y: i.top + i.height / 2 } };
  });
  const results: boolean[] = [];
  for (const direction of ["image-to-empty", "empty-to-image"] as const) {
    await open();
    const p = await points();
    const from = direction === "image-to-empty" ? p.image : p.empty;
    const to = direction === "image-to-empty" ? p.empty : p.image;
    await browser.mouse.move(from.x, from.y); await browser.mouse.down();
    for (let n = 1; n <= 12; n++) await browser.mouse.move(from.x + (to.x - from.x) * n / 12, from.y + (to.y - from.y) * n / 12);
    await app.screenshot(`${direction}-before-release`);
    await browser.mouse.up();
    await app.screenshot(`${direction}-after-release`);
    results.push(await isOpen());
  }
  await open();
  let p = await points();
  await browser.mouse.move(p.empty.x, p.empty.y); await browser.mouse.down(); await browser.mouse.up();
  await app.screenshot("plain-empty-click-closes");
  const emptyClosed = !(await isOpen());
  await open(); p = await points();
  await browser.mouse.move(p.image.x, p.image.y); await browser.mouse.down(); await browser.mouse.up();
  await app.screenshot("plain-image-click-stays-open");
  await expect({ crossEdgeDragsStayOpen: results, emptyClickCloses: emptyClosed, imageClickStaysOpen: await isOpen() }).toEqual({ crossEdgeDragsStayOpen: [true, true], emptyClickCloses: true, imageClickStaysOpen: true });
});
