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

test("Webhook URL copying and image attachment sizing retain existing behavior", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("helper-demo");
  const thread = await t3.thread({ project, title: "Helper behavior", messages: ["first"] });
  await t3.open("/settings/scheduled-tasks");
  await screen.getByRole("button", "New task").tap();
  await screen.getByRole("textbox", "Name").fill("Webhook demonstration");
  await screen.getByRole("combobox", "Destination").tap();
  await screen.getByRole("option", "One thread, continuing the conversation").tap();
  await screen.getByRole("combobox", "Destination thread").tap();
  await screen.getByRole("option", "Helper behavior").tap();
  await screen.getByRole("button", "On webhook").tap();
  await screen.getByRole("button", "Create task").tap();
  await screen.getByRole("button", "Actions for Webhook demonstration").tap();
  await screen.getByRole("menuitem", "Edit", { exact: true }).tap();
  await expect(screen.getByRole("textbox", "Webhook URL")).toBeVisible();
  await screen.getByRole("button", "Copy", { exact: true }).tap();
  await expect(screen.getByRole("button", "Copied", { exact: true })).toBeVisible();
  await app.screenshot("loopback-webhook-copied");
  await browser.keyboard.press("Escape");
  await t3.open(thread);
  await screen.getByRole("textbox").fill("Image sizing examples: 800×600 PNG, 2000×2000 PNG, 4000×2000 PNG, 1×10000 PNG, 4000×2000 JPEG");
  for (const [width, height, mime] of [[800,600,"image/png"],[2000,2000,"image/png"],[4000,2000,"image/png"],[1,10000,"image/png"],[4000,2000,"image/jpeg"]] as const) {
    await browser.evaluate(async ({width,height,mime}) => {
      const canvas = new OffscreenCanvas(width,height);
      const ctx = canvas.getContext("2d")!;
      ctx.fillStyle = "#0d9488"; ctx.fillRect(0,0,width,height);
      ctx.fillStyle = "#ffffff"; ctx.font="bold 100px sans-serif";
      ctx.fillText(`${width} × ${height}`,100,160);
      const blob = await canvas.convertToBlob({type:mime});
      const data = new DataTransfer();
      data.items.add(new File([blob],`${width}x${height}.${mime === "image/png" ? "png" : "jpg"}`,{type:mime}));
      document.querySelector('[contenteditable="true"]')!.dispatchEvent(new ClipboardEvent("paste",{clipboardData:data,bubbles:true,cancelable:true}));
    },{width,height,mime});
  }
  await expect(screen.getByRole("button", "Image attachment", {exact:false})).toHaveCount(5);
  await app.screenshot("image-attachment-sizing");
  for (const [index, width, height] of [[0,800,600],[1,2000,2000],[2,2000,1000],[3,1,2000],[4,2000,1000]]) {
    await screen.getByRole("button", "Image attachment", {exact:false}).nth(index).tap();
    await expect.poll(() => browser.evaluate(({width,height}) => Array.from(document.querySelectorAll('[role="dialog"] img')).some((img) => (img as HTMLImageElement).naturalWidth === width && (img as HTMLImageElement).naturalHeight === height), {width,height})).toBe(true);
    await app.screenshot(`image-${index}-processed-${width}x${height}`);
    await browser.keyboard.press("Escape");
  }
});
