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

test("Styled ODT footnotes keep the citation inline", WEB, async ({ t3, app, browser, screen }) => {
  const content = `<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" office:version="1.2">
<office:automatic-styles><style:style style:name="P1" style:family="paragraph" style:parent-style-name="Footnote"><style:text-properties fo:font-weight="normal" fo:font-style="normal"/></style:style></office:automatic-styles>
<office:body><office:text><text:p>Body paragraph with styled footnote<text:note text:id="ftn1" text:note-class="footnote"><text:note-citation>1</text:note-citation><text:note-body><text:p text:style-name="P1">Styled note text.</text:p></text:note-body></text:note> and plain control<text:note text:id="ftn2" text:note-class="footnote"><text:note-citation>2</text:note-citation><text:note-body><text:p>Plain note text.</text:p></text:note-body></text:note>.</text:p></office:text></office:body></office:document-content>`;
  const odt = await new JSZip().file("mimetype", "application/vnd.oasis.opendocument.text").file("content.xml", content).file("META-INF/manifest.xml", '<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"><manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.oasis.opendocument.text"/><manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/></manifest:manifest>').generateAsync({type:"uint8array"});
  const project = await t3.project("project", { "footnotes.odt": odt });
  const thread = await t3.thread({ project, title: "Footnote preview", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("treeitem", "footnotes.odt").tap();
  const preview = browser.frameLocator('iframe[title^="footnotes.odt"]');
  await preview.getByText("Styled note text.", { exact: false }).waitFor({ timeout: 60000 });
  await app.screenshot("odt-footnotes");
  const styled = preview.locator('p[data-odf-normal-weight][data-odf-normal-style]');
  await expect(styled).toHaveText("1Styled note text.");
  await expect(preview.locator('p[data-odf-normal-weight][data-odf-normal-style] > sup')).toHaveText("1");
});
