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

test("Settings finds the displayed dictionary title in English and French", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("demo");
  await t3.open("/settings/voice");
  await screen.getByText("Your dictionary", { exact: true }).waitFor();
  await browser.evaluate(() => document.getElementById("speech-dictionary")?.scrollIntoView({ block: "center", behavior: "instant" }));
  await screen.getByRole("combobox", "Search settings").fill("Your dictionary");
  await app.screenshot("english-displayed-title");
  let englishFailure: unknown;
  try {
    await t3.open("/settings/general");
    await screen.getByRole("combobox", "Search settings").fill("Your dictionary");
    await expect(screen.getByRole("option", "Dictionary Voice")).toBeVisible();
    await browser.keyboard.press("Enter");
    await expect(browser.locator('[id="speech-dictionary"]')).toBeVisible();
    await browser.evaluate(() => new Promise<boolean>((resolve) => {
      const row = document.getElementById("speech-dictionary")!;
      const observer = new IntersectionObserver(([entry]) => {
        if (entry.intersectionRatio === 1) { observer.disconnect(); resolve(true); }
      }, { threshold: 1 });
      observer.observe(row);
    }));
    await app.screenshot("english-activated-row");
  } catch (error) { englishFailure = error; }
  await screen.getByRole("combobox", "Search settings").fill("");
  await t3.open("/settings/general");
  await screen.getByRole("combobox", "Language", { exact: true }).tap();
  await screen.getByRole("option", "Français", { exact: true }).tap();
  await t3.open("/settings/voice");
  await screen.getByText("Votre dictionnaire", { exact: true }).waitFor();
  await browser.evaluate(() => document.getElementById("speech-dictionary")?.scrollIntoView({ block: "center", behavior: "instant" }));
  await screen.getByRole("combobox", "Rechercher dans les paramètres").fill("Votre dictionnaire");
  await app.screenshot("french-displayed-title");
  let frenchFailure: unknown;
  try {
  await t3.open("/settings/general");
  await screen.getByRole("combobox", "Rechercher dans les paramètres").fill("Votre dictionnaire");
  await expect(screen.getByRole("option", "Dictionnaire Voix")).toBeVisible();
  await browser.keyboard.press("Enter");
  await expect(browser.locator('[id="speech-dictionary"]')).toBeVisible();
    await browser.evaluate(() => new Promise<boolean>((resolve) => {
      const row = document.getElementById("speech-dictionary")!;
      const observer = new IntersectionObserver(([entry]) => {
        if (entry.intersectionRatio === 1) { observer.disconnect(); resolve(true); }
      }, { threshold: 1 });
      observer.observe(row);
    }));
  await app.screenshot("french-activated-row");
  } catch (error) { frenchFailure = error; }
  await screen.getByRole("combobox", "Rechercher dans les paramètres").fill("Dictionnaire");
  await screen.getByRole("option", "Dictionnaire Voix").waitFor();
  await app.screenshot("french-control-results");
  await browser.keyboard.press("Enter");
  await app.screenshot("french-dictionary-control");
  if (englishFailure) throw englishFailure;
  if (frenchFailure) throw frenchFailure;
});
