import { PDFDocument } from 'pdf-lib';
import _sanitizeHtml from 'sanitize-html';
import path from 'path';
import { BuildCommandToConvertDocumentWithCalibreModel, BuildCommandToConvertDocumentWithJupyterModel, BuildCommandToConvertDocumentWithLibreOfficeModel, BuildCommandToConvertDocumentWithPandocModel, BuildCommandToGenerateLatexPdfModel, CheckIfPdfIsValidModel, SanitizeHtmlFromContentModel, SlicePdfUsingBytesModel, } from './type/index.js';
import { getCommand } from './command.js';
export const DOC = 1;
export async function checkIfPdfIsValid(source) {
    const { input } = CheckIfPdfIsValidModel.parse(source);
    try {
        await PDFDocument.load(input.file.buffer);
        return true;
    }
    catch (e) {
        return false;
    }
}
export function buildCommandToConvertDocumentWithLibreOffice(source) {
    const input = BuildCommandToConvertDocumentWithLibreOfficeModel.parse(source);
    const cmd = getCommand('soffice');
    cmd.list.push('--headless');
    cmd.list.push('--convert-to');
    cmd.list.push(input.output.file.format);
    cmd.list.push('--outdir');
    cmd.list.push(input.output.directory.path);
    cmd.list.push(input.input.file.path);
    return [cmd];
}
export function buildCommandToConvertDocumentWithPandoc(source) {
    const { input, output } = BuildCommandToConvertDocumentWithPandocModel.parse(source);
    const inputFormat = path.extname(input.file.path).slice(1);
    const outputFormat = output.format || path.extname(output.file.path).slice(1);
    const cmd = getCommand(`pandoc`);
    cmd.list.push(`--sandbox`, `-f`, `${inputFormat}`, `-t`, `${outputFormat}`, `-o`, `${output.file.path}`, `${input.file.path}`);
    return [cmd];
}
// https://www.reddit.com/r/hacking/comments/108sp8f/how_to_know_if_a_pdf_contains_malware/
export function buildCommandToGenerateLatexPdf(source) {
    const input = BuildCommandToGenerateLatexPdfModel.parse(source);
    const cmd = getCommand(`pdflatex`);
    cmd.list.push(`-interaction=nonstopmode`, `-halt-on-error`, `-output-directory`, `${input.output.directory}`, `-jobname=${input.output.file.name}`, `${input.input.file.path}`);
    return [cmd];
}
export function buildCommandToConvertDocumentWithCalibre(source) {
    const input = BuildCommandToConvertDocumentWithCalibreModel.parse(source);
    const cmd = getCommand(`ebook-convert`);
    cmd.list.push(input.input.file.path, input.output.file.path);
    return [cmd];
}
export function buildCommandToConvertDocumentWithJupyter(source) {
    const input = BuildCommandToConvertDocumentWithJupyterModel.parse(source);
    const cmd = getCommand(`jupyter`);
    cmd.list.push(`nbconvert`, `--to`, input.output.format, input.input.file.path);
    return [cmd];
}
// https://github.com/Wandmalfarbe/pandoc-latex-template
// We found the best open source solution is LibreOffice (which was forked from OpenOffice.org, which itself was called Star Office before it was open sourced). It is much more actively developed than AbiWord, as another answer suggested.
// https://stackoverflow.com/a/21204653/169992
// https://github.com/AlJohri/docx2pdf
// https://askubuntu.com/questions/396825/convert-docx-to-pdf
// sudo apt-get install unoconv
// doc2pdf respondus-docx-sample-file.docx
export async function slicePdfUsingBytes(source) {
    const { input, startPage, endPage } = SlicePdfUsingBytesModel.parse(source);
    const readPdf = await PDFDocument.load(input.file.buffer);
    const writePdf = await PDFDocument.create();
    let indices = [];
    let i = startPage;
    while (i < endPage) {
        indices.push(i++);
    }
    let pages = await writePdf.copyPages(readPdf, indices);
    for (let i = 0, n = pages.length; i < n; i++) {
        let page = pages[i];
        writePdf.addPage(page);
    }
    let bytes = await writePdf.save();
    return bytes;
}
export function sanitizeHTMLFromContent(source) {
    const input = SanitizeHtmlFromContentModel.parse(source);
    return _sanitizeHtml(input.input.content);
}
//# sourceMappingURL=document.js.map