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

async function book() {
  const zip = new JSZip();
  zip.file("mimetype", "application/epub+zip", { compression: "STORE" });
  zip.file("META-INF/container.xml", '<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles><rootfile full-path="OEBPS/book.opf" media-type="application/oebps-package+xml"/></rootfiles></container>');
  zip.file("OEBPS/book.opf", '<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="id"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:identifier id="id">tag-proof</dc:identifier><dc:title>EPUB tag proof</dc:title><dc:language>en</dc:language></metadata><manifest><item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/><item id="c1" href="one.xhtml" media-type="application/xhtml+xml"/><item id="c2" href="two.xhtml" media-type="application/xhtml+xml"/><item id="css" href="style.css" media-type="text/css"/></manifest><spine><itemref idref="c1"/><itemref idref="c2"/></spine></package>');
  zip.file("OEBPS/nav.xhtml", '<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"><head><title>Contents</title></head><body><nav epub:type="toc"><ol><li><a href="one.xhtml">Valid XHTML</a></li><li><a href="two.xhtml">Fallback HTML</a></li></ol></nav></body></html>');
  zip.file("OEBPS/style.css", 'tt { color: rgb(200,0,0); background: rgb(255,240,160); } acronym { color: rgb(0,0,200); }');
  const lines = '<p id="tt">TT: run <tt>make install</tt> now.</p><p>CODE: run <code>make install</code> now.</p><p id="acr">ACRONYM: built with <acronym title="GNU Compiler Collection">GCC</acronym> today.</p><p>ABBR: built with <abbr title="GNU Compiler Collection">GCC</abbr> today.</p>';
  zip.file("OEBPS/one.xhtml", '<?xml version="1.0"?><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Valid XHTML</title><link rel="stylesheet" href="style.css"/></head><body><h1>Valid XHTML</h1>'+lines+'</body></html>');
  zip.file("OEBPS/two.xhtml", '<html><head><title>Fallback HTML</title><link rel="stylesheet" href="style.css"></head><body><h1>Fallback HTML</h1>'+lines.replaceAll('</p>', '')+'</body></html>');
  return zip.generateAsync({ type: "uint8array" });
}

test("EPUB preserves tt and acronym in XML and fallback HTML", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("epub-demo", { "book.epub": await book() });
  const thread = await t3.thread({ project, title: "EPUB formatting", 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", "book.epub").tap();
  const frame = browser.frameLocator('iframe[title^="book.epub"]');
  const results = [];
  for (const chapter of ["Valid XHTML", "Fallback HTML"]) {
    await frame.getByRole("heading", chapter).waitFor({state: "visible", timeout:120000});
    await app.screenshot(chapter === "Valid XHTML" ? "valid-xhtml" : "fallback-html");
    results.push(await browser.evaluate(() => {
      const doc = document.querySelector<HTMLIFrameElement>('iframe[title^="book.epub"]')!.contentDocument!;
      const tt = doc.querySelector('tt');
      const acr = doc.querySelector('acronym');
      return { tt: tt?.textContent ?? null, ttColor: tt ? doc.defaultView!.getComputedStyle(tt).color : null, ttBackground: tt ? doc.defaultView!.getComputedStyle(tt).backgroundColor : null, ttFont: tt ? doc.defaultView!.getComputedStyle(tt).fontFamily : null, acronym: acr?.textContent ?? null, title: acr?.getAttribute('title') ?? null, acronymColor: acr ? doc.defaultView!.getComputedStyle(acr).color : null, acronymDecoration: acr ? doc.defaultView!.getComputedStyle(acr).textDecorationLine : null };
    }));
    if (chapter === "Valid XHTML") await screen.getByRole("button", "Next chapter").tap();
  }
  for (const result of results) {
    expect(result.tt).toBe("make install");
    expect(result.ttColor).toBe("rgb(200, 0, 0)");
    expect(result.ttBackground).toBe("rgb(255, 240, 160)");
    expect(result.ttFont).toContain("monospace");
    expect(result.acronym).toBe("GCC");
    expect(result.title).toBe("GNU Compiler Collection");
    expect(result.acronymColor).toBe("rgb(0, 0, 200)");
    expect(result.acronymDecoration).toContain("underline");
  }
});
