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

test("Terminal shortcut preserves open menu and dialog focus", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo-repo", { "README.md": "Demo project\n" });
  let thread;
  for (let attempt = 0; attempt < 4; attempt++) {
    try {
      thread = await t3.thread({ project, title: "Update README", messages: ["Start"] });
      break;
    } catch (error) {
      if (!String(error).includes("Advertised models: default") || attempt === 3) throw error;
    }
  }
  if (!thread) throw new Error("Replay catalog did not load");
  await runPromise(Effect.flatMap(FileSystem.FileSystem, fs => fs.writeFileString(`${project}/README.md`, "Demo project\nUpdated documentation\n")));
  await t3.open(thread);
  const toggle = screen.getByRole("button", "Toggle terminal drawer");
  const composer = screen.getByTestId("composer-editor");
  await composer.tap();
  await screen.getByRole("button", "Git action options").tap();
  await screen.getByRole("menuitem", { name: "Commit", exact: true }).waitFor();
  const focusBeforeMenu = await browser.evaluate(() => document.activeElement?.outerHTML);
  await browser.keyboard.press("Control+j");
  await app.screenshot("menu-after-shortcut");
  const menuState = await browser.evaluate(() => ({
    closed: document.querySelector('[aria-label="Toggle terminal drawer"]')?.getAttribute("aria-pressed") === "false",
    focus: document.activeElement?.outerHTML,
  }));
  await browser.keyboard.press("Escape");
  if (!menuState.closed) await toggle.tap();
  await screen.getByRole("button", "Git action options").tap();
  await screen.getByRole("menuitem", { name: "Commit", exact: true }).tap();
  const message = screen.getByPlaceholder("Leave empty to auto-generate");
  await message.fill("Update README");
  await browser.keyboard.press("Control+j");
  await app.screenshot("dialog-after-shortcut");
  const dialogState = await browser.evaluate(() => ({
    closed: document.querySelector('[aria-label="Toggle terminal drawer"]')?.getAttribute("aria-pressed") === "false",
    focused: document.activeElement?.getAttribute("placeholder") === "Leave empty to auto-generate",
  }));
  await screen.getByRole("button", { name: "Cancel", exact: true }).tap();
  if (!dialogState.closed) await toggle.tap();
  await composer.tap();
  await browser.keyboard.press("Control+j");
  await app.screenshot("normal-toggle-open");
  const normalOpen = await browser.evaluate(() => document.querySelector('[aria-label="Toggle terminal drawer"]')?.getAttribute("aria-pressed"));
  await browser.keyboard.press("Control+j");
  await app.screenshot("normal-toggle-closed");
  const normalClosed = await browser.evaluate(() => document.querySelector('[aria-label="Toggle terminal drawer"]')?.getAttribute("aria-pressed"));
  await screen.getByRole("button", "Git action options").tap();
  await screen.getByRole("menuitem", { name: "Commit", exact: true }).tap();
  await message.fill("Update README");
  await browser.keyboard.press("Control+Alt+b");
  await app.screenshot("right-panel-control");
  const controlKeptFocus = await browser.evaluate(() => document.activeElement?.getAttribute("placeholder") === "Leave empty to auto-generate");
  await screen.getByRole("button", { name: "Cancel", exact: true }).tap();
  expect({ normalClosed, controlKeptFocus, menuClosed: menuState.closed, menuFocusKept: menuState.focus === focusBeforeMenu, dialogClosed: dialogState.closed, dialogFocusKept: dialogState.focused, normalOpen }).toEqual({ normalClosed: "false", controlKeptFocus: true, menuClosed: true, menuFocusKept: true, dialogClosed: true, dialogFocusKept: true, normalOpen: "true" });
});
