"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.trackPersonEvent = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const lib_1 = require("../../lib");
const config_1 = require("./config");
const lodash_1 = require("lodash");
exports.trackPersonEvent = {
    key: 'trackPersonEvent',
    category: extensions_core_1.Category.CUSTOMER_SUPPORT,
    title: 'Track person event',
    description: 'Track a person event in Customer.io',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        var _a;
        const { customerIoTrackClient, fields, pathway, patient, activity } = await (0, lib_1.validatePayloadAndCreateSdks)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const patientIdentifiers = (_a = patient.profile) === null || _a === void 0 ? void 0 : _a.identifier;
        const patientIdentifiersAttributes = !(0, lodash_1.isNil)(patientIdentifiers) && !(0, lodash_1.isEmpty)(patientIdentifiers)
            ? patientIdentifiers.reduce((acc, identifier) => {
                acc[`_awell_identifier_${identifier.system}`] = identifier.value;
                return acc;
            }, {})
            : {};
        await customerIoTrackClient.trackPersonEvent({
            type: 'person',
            action: 'event',
            name: fields.eventName,
            identifiers: {
                [fields.personIdentifierType]: fields.identifierValue,
            },
            attributes: {
                _awell_careflow_id: pathway.id,
                _awell_careflow_definition_id: pathway.definition_id,
                _awell_patient_id: patient.id,
                _awell_activity_id: activity.id,
                ...patientIdentifiersAttributes,
                ...fields.attributes,
            },
        });
        await customerIoTrackClient.trackPersonEvent({
            type: 'person',
            action: 'event',
            name: fields.eventName,
            identifiers: {
                [fields.personIdentifierType]: fields.identifierValue,
            },
            attributes: {
                _awell_careflow_id: pathway.id,
                _awell_careflow_definition_id: pathway.definition_id,
                _awell_patient_id: patient.id,
                _awell_activity_id: activity.id,
                ...fields.attributes,
            },
        });
        await onComplete();
    },
};
//# sourceMappingURL=trackPersonEvent.js.map