"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.analyzeDocumentToolDefinition = void 0;
exports.analyzeDocument = analyzeDocument;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const pdfParse = require("pdf-parse");
// @ts-ignore
const mammoth_1 = __importDefault(require("mammoth"));
async function analyzeDocument(filePath) {
    try {
        const absolutePath = path_1.default.resolve(filePath);
        if (!fs_1.default.existsSync(absolutePath)) {
            return `Error: File not found at ${absolutePath}`;
        }
        const ext = path_1.default.extname(absolutePath).toLowerCase();
        if (ext === '.pdf') {
            const dataBuffer = fs_1.default.readFileSync(absolutePath);
            // @ts-ignore
            const data = await pdfParse(dataBuffer);
            let text = data.text.trim();
            if (text.length > 20000) {
                text = text.substring(0, 20000) + "... [Content Truncated]";
            }
            return text;
        }
        if (ext === '.docx') {
            const result = await mammoth_1.default.extractRawText({ path: absolutePath });
            let text = result.value.trim();
            if (text.length > 20000) {
                text = text.substring(0, 20000) + "... [Content Truncated]";
            }
            return text;
        }
        // Fallback for TXT, MD, CSV, etc.
        let content = fs_1.default.readFileSync(absolutePath, 'utf8');
        if (content.length > 20000) {
            content = content.substring(0, 20000) + "... [Content Truncated]";
        }
        return content;
    }
    catch (error) {
        return `Failed to analyze document: ${error.message}`;
    }
}
exports.analyzeDocumentToolDefinition = {
    type: "function",
    function: {
        name: "analyze_document",
        description: "Extracts textual content from documents (PDF, DOCX) and plain text files (TXT, MD, CSV). Useful for reading reports, contracts, and data dumps.",
        parameters: {
            type: "object",
            properties: {
                filePath: {
                    type: "string",
                    description: "The absolute or relative path to the document file.",
                }
            },
            required: ["filePath"],
        },
    },
};
