/// <reference lib="dom" />
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import { T3_HOME } from "../e2e.config.ts";
import { makeState, rpcClient, runPromise, test, WEB } from "./t3.ts";

test("Mod+S moves an edited queued message to stash and restores the separate draft", WEB, async ({t3, app, browser, screen}) => {
  const setMode = (mode: "approval-required" | "full-access") => runPromise(Effect.gen(function* () {
    const client = yield* rpcClient(T3_HOME, app.baseUrl ?? "");
    yield* client["server.updateSettings"]({patch: {defaultRuntimeMode: mode}});
  }).pipe(Effect.scoped));
  await setMode("approval-required");
  const project = await t3.project("demo");
  const thread = await t3.thread({project, title: "Queued edit stash", messages: ["Change the file"], model: "approval"});
  const {cli} = makeState(T3_HOME);
  const attachmentThread = await t3.thread({project, title: "Attachment guard", messages: ["Change the file"], model: "approval"});
  await runPromise(cli(["thread", "send", attachmentThread.threadId, "Queued with existing image"]));
  await t3.open(attachmentThread);
  await screen.getByRole("button", "Edit queued message").tap();
  await browser.evaluate(() => {
    const bytes = Uint8Array.from(atob("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAD0lEQVR4AQEEAPv/AP8AAAMBAQCNHeWCAAAAAElFTkSuQmCC"), c => c.charCodeAt(0));
    const data = new DataTransfer();
    data.items.add(new File([bytes], "sample.png", {type: "image/png"}));
    document.querySelector('[contenteditable="true"]')!.dispatchEvent(new ClipboardEvent("paste", {clipboardData: data, bubbles: true, cancelable: true}));
  });
  await screen.getByRole("button", "Image attachment, sample.png", {exact: false}).waitFor({state: "visible"});
  await screen.getByRole("button", "Update queued message", {exact: false}).tap();
  await screen.getByRole("button", "Edit queued message").tap();
  await screen.getByRole("button", "Remove sample.png").waitFor({state: "visible"});
  await screen.getByRole("textbox").fill("Edited message with existing image");
  await app.screenshot("attachment-before-shortcut");
  await screen.getByRole("textbox").press("Control+s");
  await app.screenshot("attachment-after-shortcut");
  const attachmentState = await browser.evaluate(() => ({
    text: document.querySelector('[role="textbox"]')?.textContent,
    image: !!document.querySelector('button[aria-label="Remove sample.png"]'),
    editing: !!document.querySelector('button[aria-label="Cancel editing queued message"]'),
    stash: !!document.querySelector('[data-prompt-stash-badge]'),
  }));
  await runPromise(cli(["thread", "send", thread.threadId, "Original queued follow-up"]));
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.fill("Separate draft to restore");
  await screen.getByRole("button", "Edit queued message").tap();
  await composer.fill("Edited follow-up for the stash");
  await app.screenshot("before-shortcut");
  await composer.press("Control+s");
  await screen.getByRole("button", "Open stash.", {exact: false}).tap();
  await app.screenshot("after-shortcut-stash-open");
  expect(attachmentState).toEqual({text: "Edited message with existing image", image: true, editing: true, stash: false});
  await expect(composer).toContainText("Separate draft to restore");
  await expect(screen.getByRole("button", "Cancel editing queued message")).not.toBeVisible();
  await expect(screen.getByRole("button", "Edit queued message")).not.toBeVisible();
  await expect(screen.getByRole("button", "Restore stashed prompt: Edited follow-up for the stash")).toBeVisible();
});
