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

async function book() {
  const svg1 = `<svg xmlns="http://www.w3.org/2000/svg" width="620" height="220" viewBox="0 0 620 220"><defs><filter id="merged" x="-50%" y="-50%" width="200%" height="200%"><feGaussianBlur in="SourceAlpha" stdDeviation="3" result="blur"/><feOffset in="blur" dx="6" dy="6" result="offsetBlur"/><feMerge><feMergeNode in="offsetBlur"/><feMergeNode in="SourceGraphic"/></feMerge></filter><filter id="drop" x="-50%" y="-50%" width="200%" height="200%"><feDropShadow dx="6" dy="6" stdDeviation="3" flood-color="black" flood-opacity="0.6"/></filter></defs><circle cx="150" cy="100" r="60" fill="orange" filter="url(#merged)"/><circle cx="450" cy="100" r="60" fill="dodgerblue" filter="url(#drop)"/></svg>`;
  const svg2 = `<svg xmlns="http://www.w3.org/2000/svg" width="620" height="220" viewBox="0 0 620 220"><defs><filter id="d2" x="-50%" y="-50%" width="200%" height="200%"><feDropShadow dx="6" dy="6" stdDeviation="3" flood-color="black" flood-opacity="0.6"/></filter></defs><circle cx="150" cy="100" r="60" fill="seagreen" filter="url(#d2)"/></svg>`;
  return new JSZip().file("mimetype", "application/epub+zip")
    .file("META-INF/container.xml", '<container><rootfile full-path="OPS/book.opf"/></container>')
    .file("OPS/book.opf", `<package xmlns="http://www.idpf.org/2007/opf" version="3.0"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>SVG shadows</dc:title></metadata><manifest><item id="c1" href="c1.xhtml" media-type="application/xhtml+xml"/><item id="c2" href="c2.xhtml" media-type="application/xhtml+xml"/></manifest><spine><itemref idref="c1"/><itemref idref="c2"/></spine></package>`)
    .file("OPS/c1.xhtml", `<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Shadow artwork</title></head><body><h1>Chapter 1: SVG shadows</h1>${svg1}<p>Orange: merged shadow · Blue: drop shadow</p></body></html>`)
    .file("OPS/c2.xhtml", `<html><head><title>Fallback artwork</title></head><body><h1>Chapter 2: malformed XHTML</h1><p>Unclosed break<br>HTML fallback parser</p>${svg2}<p>Green: drop shadow in malformed XHTML</p></body></html>`)
    .generateAsync({ type: "uint8array" });
}

test("EPUB SVG artwork retains merged and drop shadows in both parsers", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("SVG artwork", {"shadows.epub": await book()});
  const thread = await t3.thread({project, title:"Review SVG shadows", messages:["Start"]});
  await browser.setViewport({width:1440, height:1000});
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("treeitem", "shadows.epub").tap();
  await screen.getByRole("button", "Maximize panel", {exact:true}).tap();
  const frame = browser.frameLocator('iframe[title^="shadows.epub"]');
  await frame.getByRole("heading", "Chapter 1: SVG shadows").waitFor({timeout:120_000});
  await app.screenshot("chapter-1-shadows");
  const first = await browser.evaluate(() => {
    const doc = document.querySelector<HTMLIFrameElement>('iframe[title^="shadows.epub"]')!.contentDocument!;
    return [...doc.querySelectorAll("filter")].map(f => [...f.children].map(e => e.localName));
  });
  await screen.getByRole("button", "Next chapter", {exact:true}).tap();
  await frame.getByRole("heading", "Chapter 2: malformed XHTML").waitFor();
  await app.screenshot("chapter-2-fallback-shadow");
  const second = await browser.evaluate(() => {
    const doc = document.querySelector<HTMLIFrameElement>('iframe[title^="shadows.epub"]')!.contentDocument!;
    return [...doc.querySelectorAll("filter")].map(f => [...f.children].map(e => e.localName));
  });
  expect({first,second}).toEqual({first:[["feGaussianBlur","feOffset","feMerge"],["feDropShadow"]],second:[["feDropShadow"]]});
});
