"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LandingAiApiClient = void 0;
const axios_1 = __importDefault(require("axios"));
const form_data_1 = __importDefault(require("form-data"));
const lodash_1 = require("lodash");
class LandingAiApiClient {
    constructor({ baseUrl, apiKey }) {
        this.client = axios_1.default.create({
            baseURL: baseUrl,
            headers: {
                Authorization: `Bearer ${apiKey}`,
                'Content-Type': 'application/json',
            },
        });
    }
    async agenticDocumentAnalysis({ input, mode, }) {
        var _a, _b, _c, _d, _e, _f;
        const form = new form_data_1.default();
        /**
         * Their API currently doesn't support a reference to a file URL,
         * so we need to download the file and append it to the form
         */
        if (mode === 'remoteFile') {
            if (!(0, lodash_1.isNil)(input.body.pdf)) {
                const fileRes = await axios_1.default.get(input.body.pdf, {
                    responseType: 'stream',
                });
                form.append('pdf', fileRes.data, {
                    filename: 'document.pdf',
                    contentType: 'application/pdf',
                });
            }
            else if (!(0, lodash_1.isNil)(input.body.image)) {
                const fileRes = await axios_1.default.get(input.body.image, {
                    responseType: 'stream',
                });
                form.append('image', fileRes.data, {
                    filename: 'document.png',
                    contentType: 'image/png',
                });
            }
            else {
                throw new Error('Must provide either image or pdf file URL.');
            }
        }
        else if (mode === 'base64EncodedFile') {
            if (!(0, lodash_1.isNil)(input.body.pdf)) {
                const base64PdfToBuffer = Buffer.from(input.body.pdf.replace(/^data:.*;base64,/, ''), 'base64');
                form.append('pdf', base64PdfToBuffer, {
                    filename: 'document.pdf',
                    contentType: 'application/pdf',
                });
            }
            else if (!(0, lodash_1.isNil)(input.body.image)) {
                const base64ImageToBuffer = Buffer.from(input.body.image.replace(/^data:.*;base64,/, ''), 'base64');
                form.append('image', base64ImageToBuffer, {
                    filename: 'document.png',
                    contentType: 'image/png',
                });
            }
            else {
                throw new Error('Must provide either base64 image or pdf.');
            }
        }
        // Add remaining fields
        form.append('include_marginalia', (_b = (_a = input.body.include_marginalia) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : 'false');
        form.append('include_metadata_in_markdown', (_d = (_c = input.body.include_metadata_in_markdown) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : 'false');
        if (!(0, lodash_1.isNil)(input.body.fields_schema)) {
            form.append('fields_schema', input.body.fields_schema);
        }
        const queryParams = new URLSearchParams();
        if (!(0, lodash_1.isNil)((_e = input.query) === null || _e === void 0 ? void 0 : _e.pages)) {
            queryParams.set('pages', input.query.pages);
        }
        if (!(0, lodash_1.isNil)((_f = input.query) === null || _f === void 0 ? void 0 : _f.timeout)) {
            queryParams.set('timeout', input.query.timeout.toString());
        }
        const response = await this.client.post(`/v1/tools/agentic-document-analysis?${queryParams.toString()}`, form, {
            headers: {
                ...form.getHeaders(),
            },
        });
        return response;
    }
}
exports.LandingAiApiClient = LandingAiApiClient;
//# sourceMappingURL=client.js.map