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

async function workbook() {
  const zip = new JSZip();
  zip.file("mimetype", "application/vnd.oasis.opendocument.spreadsheet", { compression: "STORE" });
  zip.file("META-INF/manifest.xml", `<?xml version="1.0"?><manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2"><manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/><manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/></manifest:manifest>`);
  zip.file("content.xml", `<?xml version="1.0"?><office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.2"><office:body><office:spreadsheet><table:table table:name="Empty"><table:table-column/><table:table-row><table:table-cell/></table:table-row></table:table><table:table table:name="Data"><table:table-column table:number-columns-repeated="3"/><table:table-row><table:table-cell office:value-type="string"><text:p>Name</text:p></table:table-cell><table:table-cell office:value-type="string"><text:p>Team</text:p></table:table-cell><table:table-cell office:value-type="string"><text:p>Score</text:p></table:table-cell></table:table-row><table:table-row><table:table-cell office:value-type="string"><text:p>Ada</text:p></table:table-cell><table:table-cell office:value-type="string"><text:p>Core</text:p></table:table-cell><table:table-cell office:value-type="float" office:value="42"><text:p>42</text:p></table:table-cell></table:table-row></table:table></office:spreadsheet></office:body></office:document-content>`);
  return zip.generateAsync({ type: "uint8array", compression: "DEFLATE" });
}

test("Empty ODS sheet reports zero columns", WEB, async ({ t3, app, screen }) => {
  const project = await t3.project("demo", { "workbook.ods": await workbook() });
  const thread = await t3.thread({ project, title: "Review workbook", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByText("fixture turn complete", { exact: false }).waitFor({ timeout: 120_000 });
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("treeitem", "workbook.ods").tap();
  await screen.getByText("This sheet is empty.").waitFor({ timeout: 60_000 });
  await app.screenshot("empty-sheet");
  await screen.getByRole("button", "Next sheet").tap();
  await screen.getByText("Sheet 2 of 2", { exact: false }).waitFor();
  await screen.getByText("Ada", { exact: true }).waitFor();
  await app.screenshot("populated-sheet");
  await screen.getByRole("button", "Previous sheet").tap();
  await screen.getByText("This sheet is empty.").waitFor();
  await expect(screen.getByText("0 rows · 0 columns", { exact: false })).toBeVisible();
});
