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

test("Inheritance tooltips translate singular and plural project override counts", WEB, async ({ t3, app, screen, browser }) => {
  await browser.emulateMedia({ colorScheme: "dark" });
  await t3.project("proj-a");
  await t3.project("proj-b");
  await t3.open("/settings/worktrees");
  await screen.getByRole("button", "Project scope: All projects").tap();
  await screen.getByRole("menuitemradio").filter({ hasText: "proj-a" }).tap();
  await screen.getByRole("switch", "Start new worktrees from remote by default").tap();
  await screen.getByRole("textbox", "Branch prefix", { exact: true }).fill("pa/");
  await screen.getByRole("textbox", "Branch prefix", { exact: true }).press("Tab");
  await screen.getByRole("button", "Project scope: proj-a").tap();
  await screen.getByRole("menuitemradio").filter({ hasText: "proj-b" }).tap();
  await screen.getByRole("textbox", "Branch prefix", { exact: true }).fill("pb/");
  await screen.getByRole("textbox", "Branch prefix", { exact: true }).press("Tab");
  await screen.getByRole("button", "Project scope: proj-b").tap();
  await screen.getByRole("menuitemradio").filter({ hasText: "All projects" }).tap();
  await t3.open("/settings/general");
  await screen.getByRole("combobox", "Language", { exact: true }).tap();
  await screen.getByRole("option", "Deutsch", { exact: true }).tap();
  await t3.open("/settings/worktrees");
  const badges = screen.getByRole("button", /Integrierter Standard · [12] .*Herkunft dieses Werts anzeigen/);
  const single = badges.first();
  const plural = badges.last();
  await single.hover();
  await browser.locator('[data-slot="tooltip-popup"]').waitFor();
  await browser.evaluate(async () => {
    for (const animation of document.getAnimations()) {
      if (Number.isFinite(animation.effect?.getComputedTiming().endTime)) animation.finish();
    }
    await Promise.all(document.getAnimations().filter((animation) => Number.isFinite(animation.effect?.getComputedTiming().endTime)).map((animation) => animation.finished));
    return null;
  });
  await app.screenshot("german-singular-tooltip");
  const singularTooltip = await browser.locator('[data-slot="tooltip-popup"]').textContent();
  await plural.hover();
  await browser.locator('[data-slot="tooltip-popup"]').filter({ hasText: /2 / }).waitFor();
  await browser.evaluate(async () => {
    for (const animation of document.getAnimations()) {
      if (Number.isFinite(animation.effect?.getComputedTiming().endTime)) animation.finish();
    }
    await Promise.all(document.getAnimations().filter((animation) => Number.isFinite(animation.effect?.getComputedTiming().endTime)).map((animation) => animation.finished));
    return null;
  });
  await app.screenshot("german-plural-tooltip");
  await expect(single).toHaveAttribute("aria-label", "Integrierter Standard · 1 Projektüberschreibung. Herkunft dieses Werts anzeigen");
  await expect(plural).toHaveAttribute("aria-label", "Integrierter Standard · 2 Projektüberschreibungen. Herkunft dieses Werts anzeigen");
  expect(singularTooltip).toBe("Integrierter Standard · 1 Projektüberschreibung");
  await expect(browser.locator('[data-slot="tooltip-popup"]')).toHaveText("Integrierter Standard · 2 Projektüberschreibungen");
});
