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

const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200"><defs><filter id="bevel"><feGaussianBlur in="SourceAlpha" stdDeviation="3" result="blur"/><feDiffuseLighting in="blur" surfaceScale="6" diffuseConstant="1" lighting-color="white" result="light"><fePointLight x="-50" y="-50" z="120"/></feDiffuseLighting><feComposite in="SourceGraphic" in2="light" operator="arithmetic" k1="0" k2="1" k3="1" k4="0"/></filter></defs><rect x="20" y="20" width="160" height="160" fill="#ed8619" filter="url(#bevel)"/></svg>`;

test("EPUB inline and standalone SVG pages keep their bevel lighting", WEB, async ({ t3, app, screen, browser }) => {
  const epub = await new JSZip()
    .file("mimetype", "application/epub+zip", { compression: "STORE" })
    .file("META-INF/container.xml", `<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles><rootfile full-path="book.opf" media-type="application/oebps-package+xml"/></rootfiles></container>`)
    .file("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">bevel</dc:identifier><dc:title>Lit bevel</dc:title><dc:language>en</dc:language></metadata><manifest><item id="inline" href="inline.xhtml" media-type="application/xhtml+xml"/><item id="standalone" href="standalone.svg" media-type="image/svg+xml"/></manifest><spine><itemref idref="inline"/><itemref idref="standalone"/></spine></package>`)
    .file("inline.xhtml", `<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Inline SVG</title></head><body><h1>Lit bevel (inline SVG)</h1>${svg}</body></html>`)
    .file("standalone.svg", svg).generateAsync({ type: "uint8array" });
  const project = await t3.project("EPUB artwork", { "bevel.epub": epub, "original.svg": svg });
  const thread = await t3.thread({ project, title: "Review EPUB artwork", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByText("fixture turn complete", { exact: false }).waitFor();
  await browser.keyboard.press("Control+p");
  await screen.getByPlaceholder("Search files…").fill("bevel.epub");
  await screen.getByRole("option", "bevel.epub", { exact: false }).tap();
  const preview = browser.frameLocator('iframe[title^="bevel.epub:"]');
  await preview.getByText("Lit bevel (inline SVG)").waitFor({ timeout: 120_000 });
  await app.screenshot("inline-svg-bevel");
  const inline = await preview.locator("filter").getAttribute("id");
  const inlineLight = await preview.locator("feDiffuseLighting").count();
  const inlinePoint = await preview.locator("fePointLight").count();
  const inlineK2 = await preview.locator("feComposite").getAttribute("k2");
  await screen.getByRole("button", "Next chapter", { exact: true }).tap();
  await preview.locator("rect").waitFor({ timeout: 120_000 });
  await app.screenshot("standalone-svg-bevel");
  const standaloneLight = await preview.locator("feDiffuseLighting").count();
  const standalonePoint = await preview.locator("fePointLight").count();
  const standaloneK2 = await preview.locator("feComposite").getAttribute("k2");
  await browser.keyboard.press("Control+p");
  await screen.getByPlaceholder("Search files…").fill("original.svg");
  await screen.getByRole("option", "original.svg", { exact: false }).tap();
  await screen.getByRole("img", "original.svg", { exact: true }).waitFor({ timeout: 120_000 });
  await app.screenshot("original-svg-control");
  expect({ inline: Boolean(inline), inlineLight, inlinePoint, inlineK2, standaloneLight, standalonePoint, standaloneK2 }).toEqual({ inline: true, inlineLight: 1, inlinePoint: 1, inlineK2: "1", standaloneLight: 1, standalonePoint: 1, standaloneK2: "1" });
});
