"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.completedBlandText = void 0;
const lodash_1 = require("lodash");
const dataPoints = {
    conversationId: {
        key: 'conversationId',
        valueType: 'string',
    },
    status: {
        key: 'status',
        valueType: 'string',
    },
    reason: {
        key: 'reason',
        valueType: 'string',
    },
    userNumber: {
        key: 'userNumber',
        valueType: 'string',
    },
    agentNumber: {
        key: 'agentNumber',
        valueType: 'string',
    },
    pathwayId: {
        key: 'pathwayId',
        valueType: 'string',
    },
    messageCount: {
        key: 'messageCount',
        valueType: 'number',
    },
    transcript: {
        key: 'transcript',
        valueType: 'string',
    },
    variables: {
        key: 'variables',
        valueType: 'json',
    },
    conversationObject: {
        key: 'conversationObject',
        valueType: 'json',
    },
};
exports.completedBlandText = {
    key: 'completedBlandText',
    description: 'Triggered when a Bland SMS conversation ends (status webhook with channel "sms").',
    dataPoints,
    onEvent: async ({ payload: { payload }, onSuccess, onError }) => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
        const conversationId = payload === null || payload === void 0 ? void 0 : payload.conversation_id;
        const awellPatientId = (_b = (_a = payload === null || payload === void 0 ? void 0 : payload.metadata) === null || _a === void 0 ? void 0 : _a.awell_patient_id) !== null && _b !== void 0 ? _b : (_d = (_c = payload === null || payload === void 0 ? void 0 : payload.variables) === null || _c === void 0 ? void 0 : _c.metadata) === null || _d === void 0 ? void 0 : _d.awell_patient_id;
        if ((0, lodash_1.isNil)(conversationId)) {
            await onError({
                response: {
                    statusCode: 400,
                    message: 'Missing conversation_id in payload',
                },
            });
            return;
        }
        await onSuccess({
            data_points: {
                conversationId,
                /**
                 * The status of the conversation, e.g. "ended".
                 */
                status: (_e = payload === null || payload === void 0 ? void 0 : payload.status) !== null && _e !== void 0 ? _e : '',
                /**
                 * Why the conversation ended,
                 * e.g. "Conversation ended at End Call or Transfer Call node".
                 */
                reason: (_f = payload === null || payload === void 0 ? void 0 : payload.reason) !== null && _f !== void 0 ? _f : '',
                userNumber: (_g = payload === null || payload === void 0 ? void 0 : payload.phone_number) !== null && _g !== void 0 ? _g : '',
                agentNumber: (_h = payload === null || payload === void 0 ? void 0 : payload.agent_number) !== null && _h !== void 0 ? _h : '',
                pathwayId: (_j = payload === null || payload === void 0 ? void 0 : payload.pathway_id) !== null && _j !== void 0 ? _j : '',
                messageCount: (_l = (_k = payload === null || payload === void 0 ? void 0 : payload.message_count) === null || _k === void 0 ? void 0 : _k.toString()) !== null && _l !== void 0 ? _l : '',
                /**
                 * The full conversation as a single string
                 * ("AGENT: ...\nUSER: ...").
                 */
                transcript: (_m = payload === null || payload === void 0 ? void 0 : payload.concatenated_transcript) !== null && _m !== void 0 ? _m : '',
                /**
                 * Pathway variables captured during the conversation
                 * (e.g. opted_out, patient_reply, or custom extraction variables).
                 */
                variables: (0, lodash_1.isNil)(payload === null || payload === void 0 ? void 0 : payload.variables)
                    ? ''
                    : JSON.stringify(payload.variables),
                conversationObject: JSON.stringify(payload),
            },
            ...(awellPatientId !== undefined && { patient_id: awellPatientId }),
        });
    },
};
//# sourceMappingURL=completedBlandText.js.map