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

async function formulas(percent: boolean) {
  return new JSZip()
    .file("mimetype", "application/vnd.oasis.opendocument.text", { compression: "STORE" })
    .file("content.xml", `<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" office:version="1.3">
<office:automatic-styles>
<style:style style:name="T1" style:family="text"><style:text-properties style:text-position="${percent ? '33%' : 'super'} 58%"/></style:style>
<style:style style:name="T2" style:family="text"><style:text-properties style:text-position="${percent ? '-33%' : 'sub'} 58%"/></style:style>
<style:style style:name="T3" style:family="text"><style:text-properties fo:font-weight="bold"/></style:style>
</office:automatic-styles><office:body><office:text>
<text:p>E=mc<text:span text:style-name="T1">2</text:span></text:p>
<text:p>H<text:span text:style-name="T2">2</text:span>O</text:p>
<text:p>Control: <text:span text:style-name="T3">bold</text:span> works</text:p>
</office:text></office:body></office:document-content>`)
    .generateAsync({ type: "uint8array" });
}

test("ODT previews preserve Writer superscript and subscript in both position formats", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("formulas", {
    "formulas.odt": await formulas(false),
    "formulas-percent.odt": await formulas(true),
  });
  const thread = await t3.thread({project, title: "Review formulas", messages: ["Start"], model: "default"});
  await t3.open(thread);
  await screen.getByTestId("composer-editor").waitFor({state: "visible"});
  const positions: Array<{file: string; super: string | null; sub: string | null}> = [];
  for (const file of ["formulas.odt", "formulas-percent.odt"]) {
    await browser.keyboard.press("Control+p");
    await screen.getByPlaceholder("Search files…").fill(file);
    await browser.locator('[role="option"] strong').first().waitFor({state: "visible"});
    await screen.getByRole("option", `${file} ${file}`, {exact: true}).tap();
    const preview = browser.frameLocator(`iframe[title^="${file}"]`);
    await preview.getByText("Control:", {exact: false}).waitFor({timeout: 120_000});
    await app.screenshot(file === "formulas.odt" ? "writer-keyword-positions" : "writer-percent-positions");
    // Read the converter's semantic tags after capturing both documents, so a failing base
    // still produces the same two visual comparisons as the fixed app.
    const html = await browser.locator(`iframe[title^="${file}"]`).getAttribute("srcdoc");
    positions.push({file, super: html?.match(/<sup>2<\/sup>/)?.[0] ?? null, sub: html?.match(/<sub>2<\/sub>/)?.[0] ?? null});
  }
  expect(positions).toEqual(["formulas.odt", "formulas-percent.odt"].map(file => ({file, super: "<sup>2</sup>", sub: "<sub>2</sub>"})));
});
