"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.callCompleted = void 0;
const lodash_1 = require("lodash");
const dataPoints = {
    callId: {
        key: 'callId',
        valueType: 'string',
    },
    completed: {
        key: 'completed',
        valueType: 'boolean',
    },
    status: {
        key: 'status',
        valueType: 'string',
    },
    answeredBy: {
        key: 'answeredBy',
        valueType: 'string',
    },
    errorMessage: {
        key: 'errorMessage',
        valueType: 'string',
    },
    callObject: {
        key: 'callObject',
        valueType: 'json',
    },
};
exports.callCompleted = {
    key: 'callCompleted',
    dataPoints,
    onEvent: async ({ payload: { payload, rawBody, headers, settings }, onSuccess, onError, }) => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j;
        const callId = payload === null || payload === void 0 ? void 0 : payload.call_id;
        const awellPatientId = (_c = (_b = (_a = payload === null || payload === void 0 ? void 0 : payload.variables) === null || _a === void 0 ? void 0 : _a.metadata) === null || _b === void 0 ? void 0 : _b.awell_patient_id) !== null && _c !== void 0 ? _c : (_d = payload === null || payload === void 0 ? void 0 : payload.metadata) === null || _d === void 0 ? void 0 : _d.awell_patient_id;
        if ((0, lodash_1.isNil)(callId)) {
            await onError({
                response: {
                    statusCode: 400,
                    message: 'Missing call_id in payload',
                },
            });
        }
        await onSuccess({
            data_points: {
                callId,
                callObject: JSON.stringify(payload),
                /**
                 * Whether the call has been completed.
                 * This doesn't tell anything about the outcome of the call. That's what status is for
                 */
                completed: (_f = (_e = payload === null || payload === void 0 ? void 0 : payload.completed) === null || _e === void 0 ? void 0 : _e.toString()) !== null && _f !== void 0 ? _f : '',
                /**
                 * The status of the call
                 * completed - Call was successfully completed, this can be both human or voicemail answered (see answered_by for details on who the call was answered by)
                 * failed - Call failed to connect or complete (see error_message for details)
                 * busy - Called number was busy
                 * no-answer - Call was not answered
                 * canceled - Call was canceled before completion
                 * unknown - Status could not be determined
                 */
                status: (_g = payload === null || payload === void 0 ? void 0 : payload.status) !== null && _g !== void 0 ? _g : '',
                /**
                 * human: The call was answered by a human.
                 * voicemail: The call was answered by an answering machine or voicemail.
                 * unknown: There was not enough audio at the start of the call to make a determination.
                 * no-answer: The call was not answered.
                 * null: Not enabled, or still processing the result.
                 */
                answeredBy: (_h = payload === null || payload === void 0 ? void 0 : payload.answered_by) !== null && _h !== void 0 ? _h : '',
                /**
                 * If an error occurs, this will contain a description of the error. Otherwise, it will be null.
                 */
                errorMessage: (_j = payload === null || payload === void 0 ? void 0 : payload.error_message) !== null && _j !== void 0 ? _j : '',
            },
            ...(awellPatientId !== undefined && { patient_id: awellPatientId }),
        });
    },
};
//# sourceMappingURL=callCompleted.js.map