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

test("An issue panel with no addable surfaces leaves New Tab alone", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Panel demo");
  const thread = await t3.thread({ project, title: "Available surfaces control", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Add panel surface", { exact: true }).tap();
  await screen.getByRole("menu").waitFor();
  await app.screenshot("chat-click-launcher-control");
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("ControlOrMeta+t");
  await screen.getByRole("menu").waitFor();
  await app.screenshot("chat-shortcut-launcher-control");
  await browser.keyboard.press("Escape");

  await browser.addInitScript(() => {
    document.addEventListener("keydown", (event) => {
      if (event.key.toLowerCase() === "t" && (event.ctrlKey || event.metaKey)) {
        Object.assign(window, { panelNewTabEvent: event });
      }
    }, true);
    const send = WebSocket.prototype.send;
    WebSocket.prototype.send = function(data) {
      const request = typeof data === "string" ? JSON.parse(data) : null;
      if (request?._tag !== "Request" || request.tag !== "issues.detail") return send.call(this, data);
      const { projectId, tracker, scope, key } = request.payload;
      const value = {
        projectId, tracker, scope, key, host: "github.com", displayKey: "#42", projectTitle: "Panel demo",
        title: "Review the empty panel launcher", body: "This issue detail panel has no surfaces available to add.",
        url: "https://github.com/example/demo/issues/42", author: null,
        state: { category: "open", name: "Open" }, createdAt: "2026-10-01T00:00:00.000Z",
        updatedAt: "2026-10-01T00:00:00.000Z", closedAt: null, assignees: [], labels: [], commentCount: 0, writable: false,
        capabilities: { create: false, edit: false, comment: false, labels: false, assignees: false, reactions: false, templates: false, linkedPullRequests: false, events: false, search: true },
        viewerPermissions: { setState: false, comment: false, edit: false, labels: false, assignees: false },
        states: [], viewer: null, comments: [], commentsCursor: null, events: [], linkedPullRequests: [],
      };
      queueMicrotask(() => this.dispatchEvent(new MessageEvent("message", { data: JSON.stringify({ _tag: "Exit", requestId: request.id, exit: { _tag: "Success", value } }) })));
    };
  });
  await t3.open("/issues?selectedProjectId=demo&tracker=github&scope=example/demo&key=42");
  await screen.getByText("This issue detail panel has no surfaces available to add.", { exact: true }).waitFor();
  await app.screenshot("issue-panel-before-interaction");
  const add = browser.locator('button[aria-label="Add panel surface"]');
  const present = await browser.evaluate(() => document.querySelector('button[aria-label="Add panel surface"]') !== null);
  if (present) {
    await add.tap();
    await screen.getByRole("menu").waitFor();
  }
  await app.screenshot("issue-click-launcher");
  await browser.keyboard.press("Escape");
  await browser.keyboard.press("ControlOrMeta+t");
  await app.screenshot("issue-shortcut-launcher");
  const prevented = await browser.evaluate(() => (window as unknown as { panelNewTabEvent: KeyboardEvent }).panelNewTabEvent.defaultPrevented);
  await expect(prevented).toBe(false);
  await expect(add).toHaveCount(0);
  await expect(screen.getByRole("menu")).toHaveCount(0);
});
