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

test("A second distinct Escape closes only the theme editor", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("theme-demo");
  await t3.open("/settings/appearance");
  await screen.getByRole("button", "Create theme", { exact: true }).tap();
  const editor = screen.getByRole("dialog", "Create theme", { exact: true });
  await editor.getByRole("button", "Custom CSS", { exact: true }).tap();
  await editor.getByRole("textbox", "Custom CSS", { exact: true }).fill(".sidebar { border-radius: 12px; }");
  await browser.evaluate(() => {
    document.querySelector('textarea[aria-label="Custom CSS"]')?.parentElement?.parentElement?.querySelector("p")?.scrollIntoView({ block: "nearest" });
    return null;
  });
  await app.screenshot("css-before-escape");
  await browser.keyboard.down("Escape");
  await app.screenshot("first-escape-releases-tab");
  await expect(editor).toBeVisible();
  await expect(editor.getByText("Tab moves to the next control.", { exact: true })).toBeVisible();
  await browser.keyboard.down("Escape");
  await browser.keyboard.down("Escape");
  await app.screenshot("held-escape-keeps-editor-open");
  await expect(editor).toBeVisible();
  await browser.keyboard.up("Escape");
  await browser.keyboard.press("Escape");
  await app.screenshot("second-distinct-escape");
  await expect(editor).not.toBeVisible();
  await expect(screen.getByRole("button", "Create theme", { exact: true })).toBeVisible();
});
