"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.callWithGrace = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const settings_1 = require("../../settings");
const lib_1 = require("../../lib");
const zod_1 = require("zod");
const lodash_1 = require("lodash");
exports.callWithGrace = {
    key: 'callWithGrace',
    title: 'Call with Grace',
    description: 'Use AI phone conversations to communicate with your patients.',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: false,
    onEvent: async ({ payload, onComplete, onError }) => {
        const { pathway, patient } = payload;
        const { fields: { flowId, data, phoneNumber }, settings: { accountId, clientSecret }, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({ fields: config_1.FieldsSchema, settings: settings_1.SettingsSchema }),
            payload,
        });
        const client = new lib_1.GridspaceClient({ accountId, clientSecret });
        const allData = {
            ...data,
            phone_number: phoneNumber,
            patient_id: patient.id,
            ...(!(0, lodash_1.isNil)(patient.profile) && { ...patient.profile }),
            pathway_id: pathway.id,
            pathway_definition_id: pathway.definition_id,
            activity_id: payload.activity.id,
        };
        const resp = await client.callWithGrace(flowId, allData);
        await onComplete({
            events: [
                {
                    date: new Date().toISOString(),
                    text: { en: `Call with Grace. Response: ${JSON.stringify(resp)}` },
                },
            ],
            data_points: {},
        });
    },
};
//# sourceMappingURL=callWithGrace.js.map