/// <reference lib="dom" />
import { execFileSync } from "node:child_process";
import { mkdirSync, writeFileSync } from "node:fs";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Phone drawer closes when branch draft or Add project starts", WEB, async ({ t3, app, browser, screen }) => {
  await browser.setViewport({ width: 390, height: 844 });
  await browser.emulateMedia({ colorScheme: "dark" });
  const project = await t3.project("demo-repo");
  execFileSync("git", ["checkout", "-b", "feature/login"], { cwd: project });
  const thread = await t3.thread({ project, title: "Branch thread", messages: ["Start"] });
  const second = `${project}/../second-repo`;
  mkdirSync(second, { recursive: true });
  writeFileSync(`${second}/README.md`, "Second synthetic repository\n");
  execFileSync("git", ["init", "--initial-branch=main", second]);
  await t3.open(thread);
  await screen.getByRole("button", "Toggle main sidebar", { exact: true }).tap();
  await screen.getByRole("dialog", "Threads").getByText("Branch thread", { exact: true }).waitFor();
  await browser.evaluate(() => {
    const row = [...document.querySelectorAll('[data-sidebar-row]')].find(el => el.textContent?.includes('Branch thread'))!;
    row.dispatchEvent(new MouseEvent('contextmenu', { bubbles: true, cancelable: true, button: 2, clientX: 150, clientY: 200 }));
    return null;
  });
  await screen.getByRole("menuitem", "New thread on feature/login", { exact: true }).tap();
  await browser.evaluate(() => {
    for (const animation of document.getAnimations()) {
      if (Number.isFinite(animation.effect?.getComputedTiming().endTime)) animation.finish();
    }
    return null;
  });
  await browser.evaluate(async () => { await new Promise(resolve => setTimeout(resolve, 700)); return null; });
  await app.screenshot("branch-draft-started");
  const branchDrawerOpen = await browser.evaluate(() => !!document.querySelector('[role="dialog"][data-mobile="true"][data-open]'));
  // Navigate to the same seeded thread to reset the draft before the second action.
  await t3.open(thread);
  await screen.getByRole("button", "Toggle main sidebar", { exact: true }).tap();
  await screen.getByRole("combobox", "Projects", { exact: true }).tap();
  await screen.getByRole("option", "Add project", { exact: true }).tap();
  await screen.getByText("Local folder", { exact: true }).waitFor();
  await browser.evaluate(() => {
    for (const animation of document.getAnimations()) {
      if (Number.isFinite(animation.effect?.getComputedTiming().endTime)) animation.finish();
    }
    return null;
  });
  await browser.evaluate(async () => { await new Promise(resolve => setTimeout(resolve, 700)); return null; });
  await app.screenshot("add-project-started");
  const addDrawerOpen = await browser.evaluate(() => !!document.querySelector('[role="dialog"][data-mobile="true"][data-open]'));
  await screen.getByText("Local folder", { exact: true }).tap();
  await screen.getByRole("combobox").fill(second);
  await screen.getByRole("combobox").press("Enter");
  await screen.getByRole("dialog", "Command palette", { exact: true }).waitFor({ state: "hidden" });
  await browser.evaluate(() => {
    for (const animation of document.getAnimations()) {
      if (Number.isFinite(animation.effect?.getComputedTiming().endTime)) animation.finish();
    }
    return null;
  });
  await browser.evaluate(async () => { await new Promise(resolve => setTimeout(resolve, 700)); return null; });
  await app.screenshot("project-added-draft");
  expect({ branchDrawerOpen, addDrawerOpen }).toEqual({ branchDrawerOpen: false, addDrawerOpen: false });
});
