/// <reference lib="dom" />
import { expect } from "e2e";
import JSZip from "jszip";
import { test, WEB } from "./t3.ts";
const namespaces =
  'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"';

function relationships(type: string, target: string) {
  return `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/${type}" Target="${target}"/></Relationships>`;
}

/** A one-slide deck whose text box has one paragraph per run, each in its own family. */
async function deck(runs: readonly { text: string; family: string }[]) {
  const paragraphs = runs
    .map(
      (run) =>
        `<a:p><a:r><a:rPr lang="en-US" sz="2400"><a:latin typeface="${run.family}"/></a:rPr><a:t>${run.text}</a:t></a:r></a:p>`,
    )
    .join("");

  const zip = new JSZip()
    .file(
      "[Content_Types].xml",
      '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/ppt/presentation.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"/><Override PartName="/ppt/slides/slide1.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml"/></Types>',
    )
    .file("_rels/.rels", relationships("officeDocument", "ppt/presentation.xml"))
    .file(
      "ppt/presentation.xml",
      `<p:presentation ${namespaces}><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst><p:sldSz cx="9144000" cy="5143500"/></p:presentation>`,
    )
    .file("ppt/_rels/presentation.xml.rels", relationships("slide", "slides/slide1.xml"))
    .file(
      "ppt/slides/slide1.xml",
      `<p:sld ${namespaces}><p:cSld><p:spTree><p:nvGrpSpPr><p:cNvPr id="1" name=""/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr><p:grpSpPr/><p:sp><p:nvSpPr><p:cNvPr id="2" name="Text"/><p:cNvSpPr txBox="1"/><p:nvPr/></p:nvSpPr><p:spPr><a:xfrm><a:off x="457200" y="457200"/><a:ext cx="8229600" cy="2000000"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></p:spPr><p:txBody><a:bodyPr/>${paragraphs}</p:txBody></p:sp></p:spTree></p:cSld></p:sld>`,
    )
    ;
  const slide = await zip.file("ppt/slides/slide1.xml")!.async("string");
  for (let i = 1; i <= 6; i++) zip.file(`ppt/slides/slide${i}.xml`, slide.replace("Roadmap", `Roadmap — Slide ${i}`));
  zip.file("ppt/presentation.xml", `<p:presentation ${namespaces}><p:sldIdLst>${Array.from({length:6},(_,i)=>`<p:sldId id="${256+i}" r:id="rId${i+1}"/>`).join("")}</p:sldIdLst><p:sldSz cx="9144000" cy="5143500"/></p:presentation>`);
  zip.file("ppt/_rels/presentation.xml.rels", `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">${Array.from({length:6},(_,i)=>`<Relationship Id="rId${i+1}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide${i+1}.xml"/>`).join("")}</Relationships>`);
  return zip.generateAsync({type:"uint8array"});
}

test("Next advances exactly one slide at the clamped end", WEB, async ({t3, app, screen, browser}) => {
 const project = await t3.project("deck-project", {"roadmap.pptx": await deck([{text:"Roadmap",family:"Arial"}])});
 const thread = await t3.thread({project,title:"Review roadmap",messages:["Start"]});
 await t3.open(thread);
 await screen.getByRole("textbox", "Message Review roadmap").waitFor();
 await browser.keyboard.press("Control+p");
 await screen.getByPlaceholder("Search files…").fill("roadmap.pptx");
 await screen.getByRole("option", "roadmap.pptx", {exact:false}).tap();
 await screen.getByText("6 slides", {exact:false}).waitFor({timeout:120000});
 await screen.getByRole("button", "Hide file explorer", {exact:true}).tap();
 const next = screen.getByRole("button", "Next slide", {exact:true});
 for(let i=0;i<3;i++) { await next.tap(); await screen.getByText(`Slide ${i+2} of 6`, {exact:true}).waitFor(); }
 await app.screenshot("slide-4-before-next");
 await next.tap();
 try { await expect(screen.getByText("Slide 5 of 6", {exact:true})).toBeVisible(); await expect(next).toBeEnabled(); }
 finally { await app.screenshot("after-fourth-next"); }
 await next.tap();
 await expect(screen.getByText("Slide 6 of 6", {exact:true})).toBeVisible();
 await app.screenshot("after-fifth-next");
 await screen.getByRole("button", "Previous slide", {exact:true}).tap();
 await expect(screen.getByText("Slide 5 of 6", {exact:true})).toBeVisible();
 const point = await browser.evaluate(() => { const r=document.querySelector("[data-office-pages]")!.getBoundingClientRect(); return {x:r.x+r.width/2,y:r.y+r.height/2}; });
 await browser.mouse.move(point.x,point.y);
 await browser.mouse.wheel(0,-600);
 await screen.getByText("Slide 2 of 6", {exact:true}).waitFor();
 await browser.mouse.wheel(0,2000);
 await expect(screen.getByText("Slide 6 of 6", {exact:true})).toBeVisible();
 await expect(next).toBeDisabled();
 await app.screenshot("manual-scroll-bottom");
});
