/// <reference lib="dom" />
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import { CommandId, ThreadId } from "@t3tools/contracts";
import { expect } from "e2e";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as Effect from "effect/Effect";
import { T3_HOME } from "../e2e.config.ts";
import { rpcClient, runPromise, test, WEB } from "./t3.ts";

const link = Effect.fn("scenario.link")(function* (origin: string, threadId: ThreadId) {
  const client = yield* rpcClient(T3_HOME, origin);
  const uuid = Effect.flatMap(SecureRandom.SecureRandom, r => r.uuidV4());
  const key = { threadId, host: "github.com", repository: "pingdotgg/t3code", number: 46290 };
  yield* client["orchestration.dispatchCommand"]({ type: "thread.pull-request.link", commandId: CommandId.make(yield* uuid), ...key, url: "https://github.com/pingdotgg/t3code/pull/46290", source: "manual" });
  yield* client["orchestration.dispatchCommand"]({ type: "thread.pull-request-link.sync", commandId: CommandId.make(yield* uuid), ...key, snapshot: { state: "open", title: "Keep shortcut hints active until both modifier keys are released", headBranch: "fix/shift-hints", baseBranch: "main", isDraft: false, updatedAt: null, syncedAt: "2026-11-30T00:00:00.000Z" }, stack: null });
}, Effect.scoped, Effect.provide(NodeCrypto.layer));

test("PR quick actions remain until both Shift keys are released", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("shortcut-demo");
  const thread = await t3.thread({project, title: "Shift shortcut hints", messages: ["hello"], model: "default"});
  await runPromise(link(app.baseUrl ?? "", ThreadId.make(thread.threadId)));
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel", {exact:true}).tap();
  await screen.getByRole("button", "Linked items L", {exact:true}).tap();
  await screen.getByRole("button", "Close Linked items", {exact:true}).focus();
  const hints = screen.getByRole("group", "Quick actions for pull request #46290", {exact:true});
  await browser.keyboard.down("ShiftLeft");
  await app.screenshot("left-shift-held");
  await browser.keyboard.down("ShiftRight");
  await app.screenshot("both-shifts-held");
  await browser.keyboard.up("ShiftLeft");
  await app.screenshot("first-shift-released");
  try { await expect(hints).toBeVisible(); }
  finally {
    await browser.keyboard.up("ShiftRight");
    await app.screenshot("both-shifts-released");
  }
  await expect(hints).not.toBeVisible();
});
