/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Imported flag theme name keeps whole grapheme clusters", WEB, async ({ t3, app, browser, screen }) => {
  await t3.project("theme-demo");
  await t3.open("/settings/appearance");
  await screen.getByRole("button", "Add theme", { exact: true }).tap();
  await browser.evaluate(() => {
    const input = document.querySelector<HTMLInputElement>('input[type="file"]');
    if (!input) throw new Error("Theme file input missing");
    const files = new DataTransfer();
    files.items.add(new File([JSON.stringify({name: "a".repeat(46) + "🇺🇸", type: "dark", colors: {"editor.background": "#101010"}})], "flag-theme.json", {type: "application/json"}));
    input.files = files.files;
    input.dispatchEvent(new Event("change", { bubbles: true }));
    return null;
  });
  await expect(browser.locator('#theme-json-editor')).toHaveValue(/🇺🇸/);
  await screen.getByRole("button", "Add theme", { exact: true }).tap();
  const edit = browser.locator('button[aria-label^="Edit aaaa"]');
  await edit.tap();
  await screen.getByRole("textbox", "Theme name", { exact: true }).tap();
  await app.screenshot("imported-theme-name");
  const name = await browser.evaluate(() => document.querySelector<HTMLInputElement>('input[placeholder="Theme name"]')?.value);
  expect(name).toBe("a".repeat(46));
});
