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

test("Terminal close preference persists, resets, and guards bulk closes", WEB, async ({ t3, app, screen, browser }) => {
  const capture = async (label: string) => {
    await screen.getByText("Syncing messages...", { exact: true }).waitFor({ state: "hidden" });
    await screen.getByText("Loading messages...", { exact: true }).waitFor({ state: "hidden" });
    await browser.evaluate(async () => {
      await Promise.all(document.getAnimations().filter((animation) => animation.effect?.getComputedTiming().iterations !== Infinity).map((animation) => animation.finished.catch(() => {})));
    });
    await app.screenshot(label);
  };
  const showConfirmations = async () => {
    await screen.getByText("Confirmations", { exact: true }).scrollIntoView();
    await browser.evaluate(() => {
      Array.from(document.querySelectorAll("h2,h3")).find((el) => el.textContent === "Confirmations")?.scrollIntoView({ block: "start" });
    });
  };
  const project = await t3.project("terminal-demo");
  const thread = await t3.thread({ project, title: "Disposable terminals", messages: ["hello"] });
  await t3.open(thread);
  const threadUrl = await browser.url();
  await screen.getByRole("button", "Toggle right panel", { exact: true }).tap();
  await screen.getByRole("button", /^Terminal/).tap();
  const addTerminal = async () => {
    await screen.getByRole("button", "Add panel surface", { exact: true }).tap();
    await screen.getByRole("menuitem", /^Terminal/).tap();
  };
  await addTerminal();
  const tabs = screen.getByRole("button", /^Close Terminal \d+$/);
  const close = () => tabs.first().tap();
  await close();
  await capture("individual-close-default");
  await screen.getByRole("button", "Cancel", { exact: true }).tap();
  await expect(screen.getByRole("alertdialog")).toBeHidden();
  await expect(tabs).toHaveCount(2);
  const bulkClose = async () => {
    await tabs.first().secondaryTap();
    await screen.getByRole("menuitem", "Close all", { exact: true }).tap();
  };
  await bulkClose();
  // Both outcomes settle: a dialog on head, no remaining terminal tab on base.
  await expect.poll(async () => (await screen.getByRole("alertdialog").isVisible()) || (await tabs.count()) === 0).toBe(true);
  await capture("bulk-close-default");
  const bulkPrompted = await screen.getByRole("alertdialog").isVisible();
  if (bulkPrompted) {
    await screen.getByRole("button", "Cancel", { exact: true }).tap();
  await expect(screen.getByRole("alertdialog")).toBeHidden();
    await expect(tabs).toHaveCount(2);
    await capture("bulk-cancel-preserves-tabs");
  }
  await app.open("/settings/general");
  await showConfirmations();
  const preference = screen.getByRole("switch", "Confirm terminal closing", { exact: true });
  await capture("terminal-confirmation-setting");
  await expect(preference).toBeVisible();
  await preference.tap();
  await expect(preference).not.toBeChecked();
  await browser.reload();
  await expect(preference).not.toBeChecked();
  await showConfirmations();
  await capture("disabled-preference-after-reload");
  await app.open(threadUrl);
  await close();
  await expect(tabs).toHaveCount(1);
  await expect(screen.getByRole("alertdialog")).toBeHidden();
  await capture("individual-close-disabled");
  await addTerminal();
  await bulkClose();
  await expect(tabs).toHaveCount(0);
  await expect(screen.getByRole("alertdialog")).toBeHidden();
  await capture("bulk-close-disabled");
  await app.open("/settings/general");
  await screen.getByRole("button", "Reset terminal close confirmation to default", { exact: true }).tap();
  await expect(preference).toBeChecked();
  await showConfirmations();
  await capture("reset-restores-preference");
  await app.open(threadUrl);
  await screen.getByRole("button", "Toggle right panel", { exact: true }).tap();
  await screen.getByRole("button", /^Terminal/).tap();
  await addTerminal();
  await close();
  await expect(screen.getByRole("alertdialog")).toBeVisible();
  await capture("individual-close-after-reset");
  await screen.getByRole("button", "Cancel", { exact: true }).tap();
  await expect(screen.getByRole("alertdialog")).toBeHidden();
  await bulkClose();
  await expect(screen.getByRole("alertdialog")).toBeVisible();
  await capture("bulk-close-after-reset");
  await screen.getByRole("button", "Cancel", { exact: true }).tap();
  await expect(screen.getByRole("alertdialog")).toBeHidden();
  await expect(tabs).toHaveCount(2);
  await expect(bulkPrompted).toBe(true);
});
