import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Modified Escape records in both shortcut fields while bare Escape cancels", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("shortcut-demo");
  const thread = await t3.thread({project, title: "Shortcut demonstration", messages: ["Hello"]});
  await t3.open(thread);
  await t3.open("/settings/keybindings");
  const search = screen.getByLabel("Search keybindings");
  await search.fill("Right Panel: Toggle");
  await screen.getByRole("button", "Edit shortcut for Right Panel: Toggle: Ctrl+Alt+B", {exact:true}).tap();
  const existing = screen.getByRole("textbox", "Keybinding for Right Panel: Toggle", {exact:true});
  await existing.press("Shift+Escape");
  await app.screenshot("existing-binding-shift-escape");
  const existingResult = await browser.evaluate(() => ({
    value: document.querySelector<HTMLInputElement>('input[aria-label="Keybinding for Right Panel: Toggle"]')?.value ?? null,
    settings: !!document.querySelector('[role="dialog"]')
  }));
  // Reopen the settings route to discard the unsaved existing-binding edit.
  await t3.open(thread);
  await t3.open("/settings/keybindings");
  await screen.getByRole("button", "Add keybinding", {exact:true}).tap();
  await screen.getByRole("combobox", "Search", {exact:true}).fill("Right Panel: Toggle Maximized");
  await screen.getByRole("option", "Right Panel: Toggle Maximized", {exact:true}).tap();
  const added = screen.getByRole("textbox", "Keybinding for Right Panel: Toggle Maximized", {exact:true});
  await added.tap();
  await added.press("Shift+Escape");
  await app.screenshot("add-binding-shift-escape");
  const addedResult = await browser.evaluate(() => ({
    value: document.querySelector<HTMLInputElement>('input[aria-label="Keybinding for Right Panel: Toggle Maximized"]')?.value ?? null,
    settings: !!document.querySelector('[role="dialog"]'),
    saveEnabled: [...document.querySelectorAll<HTMLButtonElement>('button')].some(b => b.textContent?.trim() === 'Save' && !b.disabled)
  }));
  await added.tap();
  await added.press("Shift+F2");
  await app.screenshot("control-shift-f2");
  await added.tap();
  await added.press("Escape");
  await app.screenshot("bare-escape-cancels");
  const bareResult = await browser.evaluate(() => ({
    value: document.querySelector<HTMLInputElement>('input[aria-label="Keybinding for Right Panel: Toggle Maximized"]')?.value ?? null,
    focused: document.activeElement?.getAttribute('data-keybinding-capture') !== null,
    settings: !!document.querySelector('[role="dialog"]')
  }));
  await browser.keyboard.press("Escape");
  await browser.locator('[role="dialog"]').waitFor({state: "detached"});
  await app.screenshot("second-escape-closes-settings");
  await expect(existingResult).toEqual({value:"shift+esc", settings:true});
  await expect(addedResult).toEqual({value:"shift+esc", settings:true, saveEnabled:true});
  await expect(bareResult).toEqual({value:"", focused:false, settings:true});
});
