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

test("Add keybinding searches actions and shows existing shortcuts", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("Keybinding demo");
  await t3.open("/settings/keybindings");
  await screen.getByRole("button", "Add keybinding", { exact: true }).tap();
  const search = screen.getByRole("combobox", "Search", { exact: true });
  if (!(await search.isVisible())) {
    await screen.getByRole("combobox").filter({ hasText: "Command" }).tap();
  }
  await app.screenshot("add-keybinding-picker");
  await expect(search).toBeFocused();
  await search.fill("sidebar.toggle");
  const sidebar = screen.getByRole("option", /Sidebar: Toggle/);
  await expect(sidebar).toBeVisible();
  await expect(sidebar).toContainText("Ctrl+B");
  await app.screenshot("search-command-id-existing-shortcut");
  await sidebar.tap();
  const command = screen.getByRole("combobox", "Command", { exact: true });
  await expect(command).toHaveText("Sidebar: Toggle");
  await app.screenshot("assigned-action-selected");
  await screen.getByRole("textbox", "Keybinding for Sidebar: Toggle", { exact: true }).tap();
  await browser.keyboard.press("Control+Alt+Shift+Y");
  await screen.getByRole("button", "Save", { exact: true }).tap();
  await screen.getByRole("button", "Add keybinding", { exact: true }).tap();
  await search.fill("sidebar.toggle");
  await expect(sidebar).toBeVisible();
  await app.screenshot("additional-shortcut-saved");
  await search.fill("Opposite Queue");
  await expect(screen.getByRole("option", /Composer: Opposite Queue or Steer Action/)).toBeVisible();
  await app.screenshot("search-action-label");
  await search.fill("no-such-action");
  await expect(screen.getByText("No results found.", { exact: true })).toBeVisible();
  await app.screenshot("no-actions-found");
  await browser.keyboard.press("Escape");
});
