"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getComments = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const validatePayloadAndCreateSdk_1 = require("../../lib/validatePayloadAndCreateSdk");
const config_1 = require("./config");
const htmlToEscapedJsString_1 = require("./lib/htmlToEscapedJsString");
exports.getComments = {
    key: 'getComments',
    category: extensions_core_1.Category.COMMUNICATION,
    title: 'Get comments',
    description: 'Retrieves all comments in a care flow.',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    supports_automated_retries: true,
    onEvent: async ({ payload, onComplete }) => {
        const { taskSdk, pathway } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const { data } = await taskSdk.getCareflowComments({
            careflowId: pathway.id,
            /**
             * This endpoint is paginated.
             * We currently use a heuristic and assume that 50 is a good number to retrieve all comments.
             */
            limit: 50,
            offset: 0,
        });
        const commentsAsText = data.comments
            .map((comment) => {
            var _a, _b, _c, _d, _e, _f, _g;
            const author = `Author: ${(_c = (_b = (_a = comment.comment) === null || _a === void 0 ? void 0 : _a.created_by) === null || _b === void 0 ? void 0 : _b.email) !== null && _c !== void 0 ? _c : 'Unknown user'}`;
            const date = `Date: ${new Date((_e = (_d = comment.comment) === null || _d === void 0 ? void 0 : _d.created_at) !== null && _e !== void 0 ? _e : '').toLocaleDateString('en-US', {
                year: 'numeric',
                month: 'long',
                day: 'numeric',
            })}`;
            // Non-visit note text only supports plain text.
            const commentText = (0, htmlToEscapedJsString_1.htmlToEscapedJsString)((_g = (_f = comment.comment) === null || _f === void 0 ? void 0 : _f.text) !== null && _g !== void 0 ? _g : '');
            return `${author}\n${date}\n\n${commentText}\n\n-------------------\n`;
        })
            .join('\n');
        await onComplete({
            data_points: {
                comments: commentsAsText.length > 0 ? commentsAsText : 'No comments found',
            },
        });
    },
};
//# sourceMappingURL=getComments.js.map