import assert from "node:assert/strict";
import test from "node:test";
import { strToU8, unzipSync, zipSync } from "fflate";
import { compactPartnerBatchWorkbookForUpload } from "../koc-portal/app/batch-workbook-upload.ts";

const imageBytes = (marker, size) => {
  const bytes = new Uint8Array(size);
  bytes.set([0x89, 0x50, 0x4e, 0x47, marker]);
  for (let index = 5; index < bytes.length; index += 1) bytes[index] = marker;
  return bytes;
};

test("slims oversized WPS workbooks without removing backfill screenshots", () => {
  const worksheet = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
      <sheetData>
        <row r="1">
          <c r="A1" t="inlineStr"><is><t>序号（不能改）</t></is></c>
          <c r="D1" t="inlineStr"><is><t>图片1</t></is></c>
          <c r="F1" t="inlineStr"><is><t>笔记截图</t></is></c>
          <c r="G1" t="inlineStr"><is><t>数据分析截图（单篇笔记数据分析截图）</t></is></c>
        </row>
        <row r="2">
          <c r="D2" t="str"><f>_xlfn.DISPIMG(&quot;SOURCE&quot;,1)</f><v>=DISPIMG(&quot;SOURCE&quot;,1)</v></c>
          <c r="F2" t="str"><f>_xlfn.DISPIMG(&quot;PUBLISH&quot;,1)</f><v>=DISPIMG(&quot;PUBLISH&quot;,1)</v></c>
        </row>
      </sheetData>
    </worksheet>`;
  const cellImages = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <etc:cellImages xmlns:etc="http://www.wps.cn/officeDocument/2017/etCustomData" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
      <etc:cellImage><xdr:pic><xdr:nvPicPr><xdr:cNvPr id="1" name="SOURCE"/></xdr:nvPicPr><xdr:blipFill><a:blip r:embed="rId1"/></xdr:blipFill></xdr:pic></etc:cellImage>
      <etc:cellImage><xdr:pic><xdr:nvPicPr><xdr:cNvPr id="2" name="PUBLISH"/></xdr:nvPicPr><xdr:blipFill><a:blip r:embed="rId2"/></xdr:blipFill></xdr:pic></etc:cellImage>
    </etc:cellImages>`;
  const relationships = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
      <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/source.png"/>
      <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/publish.png"/>
    </Relationships>`;
  const workbook = zipSync(
    {
      "xl/worksheets/sheet1.xml": strToU8(worksheet),
      "xl/cellimages.xml": strToU8(cellImages),
      "xl/_rels/cellimages.xml.rels": strToU8(relationships),
      "xl/media/source.png": [imageBytes(1, 2_000_000), { level: 0 }],
      "xl/media/publish.png": [imageBytes(2, 2_000), { level: 0 }],
    },
    { level: 0 },
  );

  const compacted = compactPartnerBatchWorkbookForUpload(workbook);
  const entries = unzipSync(compacted.bytes);
  assert.equal(entries["xl/media/source.png"], undefined);
  assert.deepEqual(entries["xl/media/publish.png"], imageBytes(2, 2_000));
  assert.equal(compacted.removedMediaCount, 1);
  assert.equal(compacted.preservedScreenshotCount, 1);
  assert.ok(compacted.bytes.byteLength < workbook.byteLength / 10);
});
