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

async function ods(display: boolean) {
  return new JSZip()
    .file("mimetype", "application/vnd.oasis.opendocument.spreadsheet", { compression: "STORE" })
    .file("content.xml", `<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.3"><office:body><office:spreadsheet><table:table table:name="Time"><table:table-row><table:table-cell office:value-type="time" office:time-value="PT03H30M00S">${display ? '<text:p>03:30:00</text:p>' : ''}</table:table-cell></table:table-row></table:table></office:spreadsheet></office:body></office:document-content>`)
    .generateAsync({ type: "uint8array" });
}

test("ODS stored time is visible and display text takes precedence", WEB, async ({t3, app, screen, browser}) => {
  const project = await t3.project("Time preview", {"stored-time.ods": await ods(false), "display-time.ods": await ods(true)});
  const thread = await t3.thread({project, title: "Review time cells", messages: ["Start"]});
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Add panel surface").tap();
  await screen.getByRole("menuitem", "Files F").tap();
  await screen.getByRole("treeitem", "display-time.ods").tap();
  await expect(screen.getByRole("status").filter({hasText: "Sheet 1 of 1"})).toBeVisible({timeout: 120000});
  await app.screenshot("display-text");
  await expect(screen.getByText("03:30:00", {exact: true})).toBeVisible();
  await browser.keyboard.press("Control+p");
  await screen.getByPlaceholder("Search files…").fill("stored-time.ods");
  await screen.getByRole("option", "stored-time.ods", {exact: false}).tap();
  await expect(screen.getByRole("status").filter({hasText: "Sheet 1 of 1"})).toBeVisible({timeout: 120000});
  await app.screenshot("stored-time");
  await expect(screen.getByText("PT03H30M00S", {exact: true})).toBeVisible();
});
