/// <reference lib="dom" />
import { expect } from "e2e";
import JSZip from "jszip";
import { test, WEB } from "./t3.ts";
async function ods(mode: string) {
 const z = new JSZip();
 z.file('mimetype','application/vnd.oasis.opendocument.spreadsheet');
 z.file('META-INF/manifest.xml','<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"><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>');
 const table = (name: string, style: string, rows: string[][], source = '') => `<table:table table:name="${name}" table:style-name="${style}">${source}${rows.map(r=>`<table:table-row>${r.map(c=>`<table:table-cell office:value-type="string"><text:p>${c}</text:p></table:table-cell>`).join('')}</table:table-row>`).join('')}</table:table>`;
 const visible = table('Summary','ta1',[['Item','Amount'],['Revenue','1200']])+table('Private Notes','ta2',[['Secret','salary 99000']])+table('Totals','ta1',[['Total','1200']]);
 z.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" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:xlink="http://www.w3.org/1999/xlink"><office:automatic-styles><style:style style:name="ta1" style:family="table"><style:table-properties table:display="true"/></style:style><style:style style:name="ta2" style:family="table"><style:table-properties table:display="false"/></style:style></office:automatic-styles><office:body><office:spreadsheet>${mode==='allhidden'?table('Only','ta2',[['Hidden','data']]):visible}${mode==='extlink'?table("'file:///rates.ods'#Rates",'ta2',[['USD','1.2']],'<table:table-source xlink:href="rates.ods" table:table-name="Rates" table:mode="copy-results-only"/>'):''}</office:spreadsheet></office:body></office:document-content>`);
 return z.generateAsync({type:'uint8array'});
}
test('ODS preview includes only visible sheets', WEB, async ({t3,app,screen,browser})=>{
 const project=await t3.project('demo',{'sheets/budget.ods':await ods('budget'),'sheets/extlink.ods':await ods('extlink'),'sheets/allhidden.ods':await ods('allhidden')});
 const thread=await t3.thread({project,title:'Review spreadsheets',messages:['Start']});
 await t3.open(thread);
 await screen.getByRole('button','Toggle right panel').tap();
 await screen.getByRole('button','Files F').tap();
 await screen.getByRole('treeitem','sheets',{exact:true}).tap();
 const results=[];
 for(const file of ['budget.ods','extlink.ods','allhidden.ods']){
  await screen.getByRole('treeitem',file,{exact:true}).tap();
  if(file==='allhidden.ods'){
   await screen.getByText(/Sheet 1 of 1|No preview for this file/).waitFor({timeout:60000});
   await app.screenshot('all-hidden');
   results.push(await browser.evaluate(()=>document.body.innerText.includes('No preview for this file')));
  }else{
   await screen.getByText('Sheet 1 of',{exact:false}).waitFor({timeout:60000});
   const status=await browser.evaluate(()=>document.body.innerText.match(/Sheet 1 of \d+/)?.[0]);
   await screen.getByRole('combobox','Sheet',{exact:true}).tap();
   await app.screenshot(file==='budget.ods'?'budget-picker':'external-cache-picker');
   const options=await browser.evaluate(()=>Array.from(document.querySelectorAll('[role="option"]')).map(e=>e.textContent));
   results.push(status==='Sheet 1 of 2' && JSON.stringify(options)===JSON.stringify(['Summary','Totals']));
   if(file==='budget.ods' && options.includes('Private Notes')) {
    await screen.getByRole('option','Private Notes',{exact:true}).tap();
    await screen.getByText('salary 99000',{exact:true}).waitFor();
    await app.screenshot('hidden-notes-content');
   } else {
    await browser.keyboard.press('Escape');
   }
   await app.screenshot(file==='budget.ods'?'budget-sheet-count':'external-sheet-count');
  }
 }
 expect(results).toEqual([true,true,true]);
});
