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

 test("Large text counts follow German app language in an English browser", WEB, async ({ t3, app, browser, screen }) => {
  const csv = ["Name,Status,Details", ...Array.from({length:1234}, (_,i)=>`Record ${i+1},Complete,${"x".repeat(990)}`)].join("\n");
  const huge = Array.from({length:1234}, (_,i)=>`Line ${i+1}: ${"synthetic text ".repeat(3930)}`).join("\n");
  const wide = [Array.from({length:1024}, (_,i)=>`Column ${i+1}`).join(","), ...Array.from({length:3}, ()=>Array.from({length:1024}, ()=>"sample ".repeat(50)).join(","))].join("\n");
  const project = await t3.project("Count formatting", {"big.csv": csv, "huge.txt": huge, "wide.csv": wide});
  const thread = await t3.thread({project, title:"Inspect file counts", messages:["Start"]});
  await t3.open("/settings/general");
  await screen.getByLabel("Language", {exact:true}).tap();
  await screen.getByText("Deutsch", {exact:true}).tap();
  await screen.getByLabel("Sprache", {exact:true}).waitFor();
  await t3.open(thread);
  await screen.getByRole("textbox", "Nachricht an Inspect file counts").waitFor();
  const locale = await browser.evaluate(() => ({ browser: navigator.language, app: document.documentElement.lang }));
  if (!locale.browser.startsWith("en") || locale.app !== "de") throw new Error(`Wrong locale setup: ${JSON.stringify(locale)}`);
  const counts: string[] = [];
  for (const name of ["big.csv", "huge.txt", "wide.csv"]) {
    await browser.keyboard.press("Control+p");
    await screen.getByPlaceholder("Dateien durchsuchen…").fill(name);
    await screen.getByRole("option", name, {exact:false}).tap();
    const header = screen.getByText(name === "big.csv" ? /1[,.]234 Zeilen · 3 Spalten/ : name === "huge.txt" ? /1[,.]234 Zeilen · [0-9.]+ MB/ : /3 Zeilen · 1[,.]024 Spalten/);
    await header.waitFor({timeout:120000});
    counts.push((await header.textContent())!);
    await app.screenshot(name === "big.csv" ? "german-csv-counts" : name === "huge.txt" ? "german-text-counts" : "german-column-counts");
  }
  expect(counts.every((text, index) => text.includes(index === 2 ? "1.024 Spalten" : "1.234 Zeilen"))).toBe(true);
});
