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

test("Keep both preserves numbered copies of a long Unicode theme", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("demo");
  await t3.open("/settings/appearance");
  const name = "İstanbul İncisi İzmir İskenderun İnegöl İpsala";
  const add = async (accent: string) => {
    await screen.getByRole("button", "Add theme", { exact: true }).tap();
    const dialog = screen.getByRole("dialog", "Add a theme");
    await dialog.getByRole("textbox", "Theme JSON").fill(JSON.stringify({ version: 1, name, appearance: "dark", colors: { accent } }));
    await dialog.getByRole("button", "Add theme", { exact: true }).tap();
    return dialog;
  };
  const first = await add("#3b82f6");
  await first.waitFor({ state: "hidden" });
  const dialog = await add("#f97316");
  await dialog.getByRole("button", "Keep both").tap();
  try {
    await expect(dialog).toBeHidden();
    await expect.poll(() => browser.evaluate(() => {
      const themes = JSON.parse(localStorage.getItem("t3code:themes:v1") ?? "[]");
      return { count: themes.length, ids: new Set(themes.map((t: any) => t.id)).size, accents: themes.map((t: any) => t.colors?.accent ?? t.dark?.colors?.accent).sort(), numbered: themes.some((t: any) => / \(1\)$/.test(t.label)) };
    })).toEqual({ count: 2, ids: 2, accents: ["oklch(0.623083 0.188015 259.815)", "oklch(0.704871 0.186721 47.604)"], numbered: true });
    await browser.evaluate(() => {
      const copy = [...document.querySelectorAll("button")].find((b) => /Use İstanbul.*\(1\) theme/.test(b.getAttribute("aria-label") ?? ""));
      copy?.scrollIntoView({ block: "center" });
      return null;
    });
  } finally {
    await app.screenshot("keep-both-long-unicode-name");
  }
});
