"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.htmlToPdf = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const zod_1 = require("zod");
const htmlToPdf_1 = require("../../../../../src/utils/htmlToPdf");
exports.htmlToPdf = {
    key: 'htmlToPdf',
    title: 'HTML to PDF',
    description: 'Convert an HTML string to a PDF base64 string, to be uploaded or sent in a future action',
    category: extensions_core_1.Category.DATA,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: false,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        helpers.log({ fields: payload.fields }, 'Processing htmlToPdf');
        try {
            const { fields: { htmlString, options }, } = (0, extensions_core_1.validate)({
                schema: zod_1.z.object({
                    fields: config_1.FieldsValidationSchema,
                }),
                payload,
            });
            const base64Pdf = await (0, htmlToPdf_1.htmlToBase64Pdf)(htmlString, options);
            helpers.log({
                htmlStringLength: htmlString.length,
                options,
                base64PdfLength: base64Pdf.length,
            }, 'Converted HTML to PDF');
            await onComplete({
                data_points: {
                    base64Pdf,
                },
            });
        }
        catch (err) {
            helpers.log({ err }, 'error', err);
            const error = err;
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: error.message },
                        error: {
                            category: 'SERVER_ERROR',
                            message: error.message,
                        },
                    },
                ],
            });
        }
    },
};
//# sourceMappingURL=htmlToPdf.js.map