/**
 * Convert Service — pure document→content converters.
 *
 * These are buffer/text in → content out: deterministic, no LLM, no storage,
 * no graph mutation. They seed the bodies of typed entities at import time
 * (`asset-import-service.ts`, the asset-as-embed flow).
 *
 *   - `convertXlsxToDatasets` (here) — structural xlsx → one CSV per sheet.
 *   - `convertDocxToMarkdown` / `convertPdfToNoteText` — text-extraction
 *     heuristics that live in `./extract/` and are re-exported below.
 *
 * The deferred asset→entry "convert" orchestrator (and its HTTP endpoint +
 * `convert_asset` agent tool) were removed in Phase E: import now creates a
 * typed entity that embeds the asset directly, so there is no post-hoc
 * conversion step.
 */
import * as XLSX from 'xlsx';
// Per-format text-extraction heuristics live in ./extract/ (kept apart from the
// structural xlsx converter below); re-exported so existing importers are stable.
import { convertDocxToMarkdown, convertPdfToNoteText } from './extract/index.js';
export { convertDocxToMarkdown, convertPdfToNoteText };
/** Above this many sheets, the caller falls a workbook back to a single
 *  `material` rather than fanning it out into that many dataset entities (no
 *  unbounded entity/commit fan-out — Codex). Gated on the RAW tab count so we
 *  never run the per-sheet CSV conversion just to count. */
export const XLSX_SHEET_CAP = 20;
/**
 * Upper bound (bytes) on a doc fed to the synchronous, request-thread parsers
 * (mammoth for docx, SheetJS for xlsx). The upload route already caps storage
 * at 100MB, but a 100MB workbook can decompress to billions of cells and stall
 * the event loop / exhaust memory before any sheet cap applies, so import
 * refuses oversized docs up front (degrading them to a download-only material).
 * NOTE: this bounds the INPUT but does not sandbox the parse — moving
 * SheetJS/mammoth to a worker with hard CPU/row limits, and upgrading SheetJS
 * past its npm-pinned 0.18.5 advisories, is the deeper follow-up.
 */
export const CONVERT_MAX_BYTES = 25 * 1024 * 1024;
/**
 * Excel (.xlsx) → one dataset per NON-EMPTY sheet (D5). v1: header is row 1,
 * cells are their computed values (SheetJS `sheet_to_csv` resolves formulas to
 * their cached results). Empty sheets are skipped. Dataset names are
 * `<baseName> — <sheetName>`.
 *
 * `opts.maxSheets` gates on the RAW tab count BEFORE any per-sheet conversion:
 * a workbook with more tabs than the cap returns `{ exceededCap: true }`
 * without running `sheet_to_csv` N times, so the caller can fall back cheaply.
 */
export function convertXlsxToDatasets(buffer, baseName, opts) {
    const workbook = XLSX.read(buffer, { type: 'buffer' });
    const names = workbook.SheetNames;
    if (opts?.maxSheets != null && names.length > opts.maxSheets) {
        return { sheets: [], totalSheets: names.length, exceededCap: true };
    }
    const sheets = [];
    for (const sheetName of names) {
        const sheet = workbook.Sheets[sheetName];
        // A sheet with no `!ref` has no cells at all; sheet_to_csv on it yields
        // ''. Skip those so an empty tab doesn't become an empty dataset.
        if (!sheet || !sheet['!ref'])
            continue;
        const csv = XLSX.utils.sheet_to_csv(sheet, { blankrows: false }).trim();
        if (!csv)
            continue;
        sheets.push({ sheetName, name: `${baseName} — ${sheetName}`, csv });
    }
    return { sheets, totalSheets: sheets.length };
}
//# sourceMappingURL=convert-service.js.map