/// <reference lib="dom" />
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import { runPromise, test, WEB } from "./t3.ts";

test("Diff leaves Ctrl+F available while Files retains Find", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("demo-project", { "notes.txt": "Original note\n" });
  await runPromise(Effect.gen(function* () {
    const fs = yield* FileSystem.FileSystem;
    yield* fs.writeFileString(`${project}/notes.txt`, "Original note\nUncommitted addition\n");
  }));
  const thread = await t3.thread({ project, title: "go", model: "default", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel", { exact: true }).tap();
  await screen.getByRole("button", "Files F", { exact: true }).tap();
  await screen.getByRole("button", "Add panel surface").tap();
  await screen.getByRole("menuitem", /^Diff/).tap();
  await screen.getByRole("button", /^(Expand|Collapse) notes\.txt$/).waitFor();
  await browser.evaluate(() => {
    const output = document.createElement("output");
    output.id = "shortcut-observation";
    output.style.cssText = "position:fixed;bottom:12px;left:12px;padding:12px;background:#202020;color:white;border:2px solid #888;z-index:99999;font:16px monospace";
    output.textContent = "Shortcut observation: waiting for Ctrl+F";
    document.body.append(output);
    window.addEventListener("keydown", (event) => {
      if (event.key.toLowerCase() !== "f" || !event.ctrlKey) return;
      queueMicrotask(() => {
        output.dataset.prevented = String(event.defaultPrevented);
        output.textContent = `Ctrl+F: defaultPrevented=${event.defaultPrevented}; focus=${document.activeElement?.getAttribute("aria-label") || document.activeElement?.textContent?.slice(0, 30)}`;
      });
    });
    return null;
  });
  await screen.getByRole("button", "Diff", { exact: true }).focus();
  const state = await browser.evaluate(() => ({
    browserSurfaces: document.querySelectorAll("[data-preview-view]").length,
    mode: document.activeElement?.closest("[data-preview-panel-mode]")?.getAttribute("data-preview-panel-mode"),
  }));
  if (state.browserSurfaces !== 0 || state.mode !== "inline") throw new Error("Wrong reproduction state");
  await browser.keyboard.press("Control+f");
  await browser.locator("#shortcut-observation[data-prevented]").waitFor();
  const diffPrevented = await browser.evaluate(() => document.querySelector<HTMLElement>("#shortcut-observation")!.dataset.prevented);
  await app.screenshot("diff-ctrl-f");
  await screen.getByRole("button", "Files", { exact: true }).tap();
  await screen.getByRole("button", "Files", { exact: true }).focus();
  await browser.keyboard.press("Control+f");
  await app.screenshot("files-ctrl-f-control");
  const filesFocused = await browser.evaluate(() => document.activeElement?.getAttribute("aria-label") || document.activeElement?.getAttribute("placeholder"));
  await expect(diffPrevented).toBe("false");
  await expect(filesFocused).toBe("Search demo-project files");
});
