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

test("Enter saves after Undo and Erase followed by canvas input", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("annotation-demo");
  const thread = await t3.thread({ project, title: "Annotation keyboard save", 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 = 450;
    const ctx = canvas.getContext("2d")!;
    ctx.fillStyle = "#f1f5f9"; ctx.fillRect(0, 0, 800, 450);
    ctx.fillStyle = "#0f172a"; ctx.font = "28px sans-serif";
    ctx.fillText("Annotation Enter save demo", 40, 60);
    const bytes = Uint8Array.from(atob(canvas.toDataURL().split(",")[1]!), c => c.charCodeAt(0));
    const files = new DataTransfer();
    files.items.add(new File([bytes], "shot.png", { type: "image/png" }));
    const picker = document.querySelector<HTMLInputElement>(selector)!;
    picker.files = files.files; picker.dispatchEvent(new Event("change", { bubbles: true }));
    return null;
  }, input);
  await screen.getByRole("button", "Preview shot.png").tap();
  const drawing = 'svg[aria-label="Draw on shot.png"]';
  const outcomes: boolean[] = [];
  for (const variant of ["Undo", "Erase"]) {
    await screen.getByRole("button", "Draw on image").tap();
    await browser.locator(drawing).waitFor();
    const rect = await browser.evaluate(selector => {
      const r = document.querySelector(selector)!.getBoundingClientRect();
      return { x: r.x, y: r.y, w: r.width, h: r.height };
    }, drawing);
    const stroke = async (row: number) => {
      await browser.mouse.move(rect.x + rect.w * .25, rect.y + rect.h * row);
      await browser.mouse.down();
      for (let i = 0; i < 8; i++) await browser.mouse.move(rect.x + rect.w * (.25 + i * .05), rect.y + rect.h * row);
      await browser.mouse.up();
    };
    await stroke(.35); await stroke(.55);
    if (variant === "Undo") {
      await screen.getByRole("button", "Undo", { exact: true }).tap();
      await stroke(.75);
    } else {
      await screen.getByRole("button", "Erase a stroke (E)").tap();
      await browser.mouse.move(rect.x + rect.w * .4, rect.y + rect.h * .55);
      await browser.mouse.down(); await browser.mouse.up();
    }
    await app.screenshot(variant + "-before-Enter");
    await browser.keyboard.press("Enter");
    // Saving flattens and uploads the image before the drawing overlay disappears.
    await browser.locator(drawing).waitFor({ state: "hidden", timeout: 5000 }).catch(() => undefined);
    await app.screenshot(variant + "-after-Enter");
    const saved = await browser.evaluate(selector => !document.querySelector(selector), drawing);
    outcomes.push(saved);
    if (!saved) await screen.getByRole("button", "Cancel drawing").tap();
  }
  await expect(outcomes).toEqual([true, true]);
});
