"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendCall = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const validatePayloadAndCreateSdk_1 = require("../../lib/validatePayloadAndCreateSdk");
const config_1 = require("./config");
const schema_1 = require("../../api/schema");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
exports.sendCall = {
    key: 'sendCall',
    category: extensions_core_1.Category.COMMUNICATION,
    title: 'Send call',
    description: 'Send an AI phone call with a custom objective and actions.',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    supports_automated_retries: true,
    onEvent: async ({ payload, onComplete, helpers: { log } }) => {
        const { fields: allFields, blandSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const getWebhookUrl = () => {
            if (allFields.completeExtensionActivityAsync &&
                allFields.webhook !== undefined) {
                return `${allFields.webhook}?activity_id=${payload.activity.id}`;
            }
            return undefined;
        };
        const { otherData, ...fields } = allFields;
        // otherData helps us to pass in fields that are not part of the SendCallInputSchema,
        // given bland's schema is updating quickly
        const sendCallInput = schema_1.SendCallInputSchema.parse({
            ...fields,
            webhook: getWebhookUrl(),
            ...otherData, // there can be a 'webhook' field in this otherData object, it needs to be able to override the webhook from the fields object
            phone_number: fields.phoneNumber,
            request_data: fields.requestData,
            metadata: {
                ...fields.metadata,
                awell_patient_id: payload.patient.id,
                awell_care_flow_definition_id: payload.pathway.definition_id,
                awell_care_flow_id: payload.pathway.id,
                awell_activity_id: payload.activity.id,
            },
            analysis_schema: fields.analysisSchema,
        });
        try {
            log({ sendCallInput }, 'Sending call to Bland');
        }
        catch (err) {
            console.error('unable to use new helpers.log');
            console.error(JSON.stringify(err));
        }
        const { data } = await blandSdk.sendCall(sendCallInput);
        /**
         * If a webhook is provided, we don't need to complete the action
         * as the webhook will handle the completion
         */
        if (allFields.completeExtensionActivityAsync) {
            return;
        }
        await onComplete({
            data_points: {
                call_id: data.call_id,
            },
            events: [
                (0, addEventLog_1.addActivityEventLog)({
                    message: `Request for call sent to Bland.\nStatus: ${data.status}\nCall ID: ${data.call_id}\nMessage: ${data.message}`,
                }),
            ],
        });
    },
};
//# sourceMappingURL=sendCall.js.map