"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.triggerFlow = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const helpers_1 = require("../../helpers");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
exports.triggerFlow = {
    key: 'triggerFlow',
    category: extensions_core_1.Category.COMMUNICATION,
    title: 'Trigger flow',
    description: 'Trigger a flow in Talkdesk',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { fields, client } = await (0, helpers_1.validatePayloadAndCreateClient)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        const { activity } = payload;
        const input = {
            flowId: fields.flowId,
            data: { ...fields.data, awell_activity_id: activity.id },
        };
        const res = await client.triggerFlow(input);
        if (fields.autoComplete) {
            await onComplete({
                data_points: {
                    interactionId: res.interaction_id,
                    flowVersionId: res.flow_version_id,
                },
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: `Flow started in TalkDesk. Interaction ID: ${res.interaction_id}, Flow version ID: ${res.flow_version_id}.`,
                    }),
                ],
            });
        }
    },
};
//# sourceMappingURL=triggerFlow.js.map