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

test("Sidebar search results retain their thread actions", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("Search actions demo");
  const alpha = await t3.thread({ project, title: "Alpha release notes", messages: ["Alpha release notes"] });
  await t3.thread({ project, title: "Zebra settled cleanup", messages: ["Zebra settled cleanup"] });
  await t3.thread({ project, title: "Zebra migration plan", messages: ["Zebra migration plan"] });
  await t3.open(alpha);
  await screen.getByText(/^Syncing messages/).waitFor({ state: "hidden" });
  await screen.getByRole("button", /^Alpha release notes,/).secondaryTap();
  await screen.getByText(/^Syncing messages/).waitFor({ state: "hidden" });
  await app.screenshot("regular-row-control");
  await browser.keyboard.press("Escape");
  await screen.getByRole("button", /^Zebra settled cleanup,/).secondaryTap();
  await screen.getByRole("menuitem", "Settle thread", { exact: true }).tap();
  await screen.getByRole("combobox", "Search threads").fill("Zebra");
  await screen.getByRole("option", /Zebra migration plan/).secondaryTap();
  await app.screenshot("active-search-result-actions");
  const activeMenu = await screen.getByRole("menuitem", "Pin thread", { exact: true }).count();
  await browser.keyboard.press("Escape");
  await screen.getByRole("combobox", "Search threads").fill("Zebra");
  await screen.getByRole("option", /Zebra settled cleanup/).secondaryTap();
  await app.screenshot("settled-search-result-actions");
  await expect(screen.getByRole("menuitem", "Un-settle thread", { exact: true })).toBeVisible();
  await expect(activeMenu).toBe(1);
  await expect(screen.getByRole("menuitem", "Rename thread", { exact: true })).toBeDisabled();
  await screen.getByRole("menuitem", "Copy", { exact: true }).hover();
  await app.screenshot("copy-thread-submenu");
  await expect(screen.getByRole("menuitem", "Thread", { exact: true })).toBeVisible();
  await screen.getByRole("menuitem", "Thread", { exact: true }).tap();
  await screen.getByRole("textbox", "Message Alpha release notes").tap();
  await browser.keyboard.press("Control+v");
  await expect(screen.getByRole("textbox", "Message Alpha release notes")).toContainText("Zebra settled cleanup");
  await app.screenshot("copied-thread-pasted");
  await screen.getByRole("combobox", "Search threads").fill("");
  await screen.getByRole("button", /^Alpha release notes,/).tap({ modifiers: ["Control"] });
  await screen.getByRole("button", /^Zebra migration plan,/).tap({ modifiers: ["Control"] });
  await app.screenshot("multiple-regular-rows-selected");
  await screen.getByRole("combobox", "Search threads").fill("Zebra");
  await screen.getByRole("option", /Zebra migration plan/).secondaryTap();
  await app.screenshot("single-thread-menu-with-multiple-selected");
  await expect(screen.getByRole("menuitem", "Pin thread", { exact: true })).toBeVisible();
  await expect(screen.getByRole("menuitem", "Settle thread", { exact: true })).toBeVisible();
  await expect(screen.getByRole("menuitem", "Snooze", { exact: true })).toBeVisible();
  await expect(screen.getByRole("menuitem", "Rename thread", { exact: true })).toBeDisabled();
  await screen.getByRole("menuitem", "Pin thread", { exact: true }).tap();
  await screen.getByRole("option", /Zebra migration plan/).secondaryTap();
  await app.screenshot("pinned-search-result-actions");
  await expect(screen.getByRole("menuitem", "Unpin thread", { exact: true })).toBeVisible();
  await screen.getByRole("menuitem", "Snooze", { exact: true }).hover();
  await app.screenshot("search-result-snooze-presets");
  await browser.keyboard.press("Escape");
  await screen.getByRole("combobox", "Search threads").fill("Zebra");
  await screen.getByRole("option", /Zebra migration plan/).secondaryTap();
  await screen.getByRole("menuitem", "Settle thread", { exact: true }).tap();
  await screen.getByRole("option", /Zebra migration plan/).secondaryTap();
  await app.screenshot("search-result-settlement-applied");
  await expect(screen.getByRole("menuitem", "Un-settle thread", { exact: true })).toBeVisible();
});
