"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.documentFieldValueAssigned = void 0;
const dataPoints = {
    webhookData: {
        key: 'webhookData',
        valueType: 'json',
    },
    patientFirstName: {
        key: 'patientFirstName',
        valueType: 'string',
    },
    patientLastName: {
        key: 'patientLastName',
        valueType: 'string',
    },
    patientZipCode: {
        key: 'patientZipCode',
        valueType: 'string',
    },
    patientIdentifiers: {
        key: 'patientIdentifiers',
        valueType: 'json',
    },
    patientMobilePhone: {
        key: 'patientMobilePhone',
        valueType: 'string',
    },
    receivingProviderFullName: {
        key: 'receivingProviderFullName',
        valueType: 'string',
    },
};
/**
 * Known field name mappings to data points
 * Keys are the field names as they appear in the Documo webhook
 * Values are the corresponding data point names
 */
const FIELD_NAME_MAPPINGS = {
    'Patient First Name': 'patientFirstName',
    'Patient Last Name': 'patientLastName',
    'Patient Zip Code': 'patientZipCode',
    'Patient Identifiers': 'patientIdentifiers',
    'Patient Phone Number': 'patientMobilePhone',
    'Receiving Provider Full Name': 'receivingProviderFullName',
};
/**
 * Extracts a field value by name from the assignments array
 */
function getFieldValueByName(assignments, fieldName) {
    const assignment = assignments.find((a) => a.name === fieldName);
    if (assignment == null) {
        return null;
    }
    // Return the appropriate value based on type
    if (assignment.valueText != null) {
        return assignment.valueText;
    }
    if (assignment.valueDate != null) {
        return assignment.valueDate;
    }
    if (assignment.valueArray != null) {
        return JSON.stringify(assignment.valueArray);
    }
    if (assignment.valueObject != null) {
        return JSON.stringify(assignment.valueObject);
    }
    return null;
}
/**
 * Extracts all mapped patient fields from assignments
 */
function extractPatientFields(assignments) {
    const result = {};
    for (const [documoFieldName, patientFieldName] of Object.entries(FIELD_NAME_MAPPINGS)) {
        result[patientFieldName] = getFieldValueByName(assignments, documoFieldName);
    }
    return result;
}
exports.documentFieldValueAssigned = {
    key: 'documentFieldValueAssigned',
    description: 'This webhook is triggered when a field value is assigned to a document.',
    dataPoints,
    onEvent: async ({ payload: { payload }, onSuccess }) => {
        var _a, _b, _c, _d, _e, _f;
        const { assignments } = payload;
        // Extract mapped patient fields
        const patientFields = extractPatientFields(assignments);
        await onSuccess({
            data_points: {
                webhookData: JSON.stringify(payload),
                patientFirstName: (_a = patientFields.patientFirstName) !== null && _a !== void 0 ? _a : '',
                patientLastName: (_b = patientFields.patientLastName) !== null && _b !== void 0 ? _b : '',
                patientZipCode: (_c = patientFields.patientZipCode) !== null && _c !== void 0 ? _c : '',
                patientIdentifiers: (_d = patientFields.patientIdentifiers) !== null && _d !== void 0 ? _d : '',
                patientMobilePhone: (_e = patientFields.patientMobilePhone) !== null && _e !== void 0 ? _e : '',
                receivingProviderFullName: (_f = patientFields.receivingProviderFullName) !== null && _f !== void 0 ? _f : '',
            },
        });
    },
};
//# sourceMappingURL=documentFieldValueAssigned.js.map