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

test("Chat maximize shortcut respects open menus and dialogs", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("shortcut-demo");
  await t3.project("other-project");
  const thread = await t3.thread({ project, title: "Shortcut proof", messages: ["Hello"] });
  await browser.setViewport({ width: 1280, height: 900 });
  await t3.open(thread);
  await screen.getByRole("button", "Thread actions for Shortcut proof").waitFor();
  await browser.keyboard.press("Control+Alt+b");
  await screen.getByRole("button", "Maximize panel", { exact: true }).waitFor();
  await app.screenshot("panel-before-shortcuts");
  const state = () => browser.evaluate(() => ({
    normal: !!document.querySelector('button[aria-label="Maximize panel"]'),
    menu: !!document.querySelector('[role="menu"]'),
    dialog: !!document.querySelector('[role="dialog"]'),
  }));
  await screen.getByRole("button", "Thread actions for Shortcut proof").click();
  await screen.getByRole("menuitem", "Move to project…", { exact: true }).waitFor();
  await browser.keyboard.press("Control+Alt+b");
  await browser.keyboard.press("Control+Alt+e");
  await app.screenshot("maximize-with-thread-menu");
  const menuState = await state();
  await browser.keyboard.press("Escape");
  if (!(await state()).normal) await browser.keyboard.press("Control+Alt+e");
  await screen.getByRole("button", "Thread actions for Shortcut proof").click();
  await screen.getByRole("menuitem", "Move to project…", { exact: true }).click();
  await screen.getByRole("dialog", "Move thread to project").waitFor();
  await browser.keyboard.press("Control+Alt+b");
  await browser.keyboard.press("Control+Alt+e");
  await app.screenshot("maximize-with-move-dialog");
  const dialogState = await state();
  await screen.getByRole("button", "Cancel", { exact: true }).click();
  if (!(await state()).normal) await browser.keyboard.press("Control+Alt+e");
  await browser.keyboard.press("Control+Alt+e");
  await screen.getByRole("button", "Restore panel size", { exact: true }).waitFor();
  await app.screenshot("maximize-after-layer-closes");
  await browser.keyboard.press("Control+Alt+e");
  await screen.getByRole("button", "Maximize panel", { exact: true }).waitFor();
  expect([menuState.normal, menuState.menu, dialogState.normal, dialogState.dialog]).toEqual([true, true, true, true]);
});
