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

test("Shortcuts recorded in Russian and Greek work after switching to English", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("shortcut-demo");
  const thread = await t3.thread({ project, title: "Keyboard shortcut demo", messages: ["hello"] });
  const results: Record<string, boolean> = {};
  for (const [layout, key] of [["Russian", "л"], ["Greek", "κ"]]) {
    await t3.open("/settings/keybindings");
    await screen.getByLabel("Search keybindings").fill("Command Palette");
    await screen.getByRole("button", /Edit shortcut for Command Palette: Toggle:/).tap();
    // Start from F8 so recording mod+k is a change with a Save button on both commits.
    await screen.getByRole("textbox", "Keybinding for Command Palette: Toggle", { exact: true }).press("F8");
    await screen.getByRole("button", "Save", { exact: true }).tap();
    await screen.getByRole("button", "Edit shortcut for Command Palette: Toggle: F8", { exact: true }).tap();
    await screen.getByRole("textbox", "Keybinding for Command Palette: Toggle", { exact: true }).focus();
    // Send the DOM key/code pair produced by Ctrl + physical K on this layout.
    await browser.evaluate((key) => {
      const target = document.activeElement!;
      for (const type of ["keydown", "keyup"]) target.dispatchEvent(new KeyboardEvent(type, {
        key, code: "KeyK", ctrlKey: true, bubbles: true, cancelable: true,
      }));
      return null;
    }, key);
    await app.screenshot(`${layout}-recorded-shortcut`);
    await screen.getByRole("button", "Save", { exact: true }).tap();
    await screen.getByRole("button", /Edit shortcut for Command Palette: Toggle:/).waitFor();
    await t3.open(thread);
    await screen.getByTestId("composer-editor").waitFor();
    await browser.evaluate(() => { (document.activeElement as HTMLElement)?.blur(); return null; });
    await browser.keyboard.press("Control+k");
    const palette = screen.getByPlaceholder("Search commands, projects, and threads...");
    // Capture and collect both layouts before asserting, so base and head show the same flow.
    await app.screenshot(`${layout}-then-English-Ctrl-K`);
    results[layout] = await palette.isVisible();
    await browser.keyboard.press("Escape");
    await browser.evaluate((key) => {
      for (const type of ["keydown", "keyup"]) document.body.dispatchEvent(new KeyboardEvent(type, {
        key, code: "KeyK", ctrlKey: true, bubbles: true, cancelable: true,
      }));
      return null;
    }, key);
    await palette.waitFor();
    await app.screenshot(`${layout}-same-layout-Ctrl-K`);
    await browser.keyboard.press("Escape");
  }
  await t3.open("/settings/keybindings");
  await screen.getByLabel("Search keybindings").fill("Command Palette");
  await screen.getByRole("button", /Edit shortcut for Command Palette: Toggle:/).tap();
  await screen.getByRole("textbox", "Keybinding for Command Palette: Toggle", { exact: true }).focus();
  // AZERTY's Latin a on physical Q must retain the layout character.
  await browser.evaluate(() => {
    document.activeElement!.dispatchEvent(new KeyboardEvent("keydown", {
      key: "a", code: "KeyQ", ctrlKey: true, bubbles: true, cancelable: true,
    }));
    return null;
  });
  await app.screenshot("Latin-AZERTY-a-on-physical-Q");
  const latin = await browser.evaluate(() => (document.querySelector('[aria-label="Keybinding for Command Palette: Toggle"]') as HTMLInputElement)?.value);
  expect(latin).toBe("mod+a");
  expect(results).toEqual({ Russian: true, Greek: true });
});
