"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFlowExecution = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const lodash_1 = require("lodash");
const createSdkClient_1 = require("../../../common/createSdkClient");
exports.createFlowExecution = {
    key: 'createFlowExecution',
    title: 'Start a new execution of a pre-existing flow',
    description: 'Send a text message from a given telephone number to a recipient of your choice.',
    category: extensions_core_1.Category.COMMUNICATION,
    fields: config_1.fields,
    dataPoints: config_1.dataPoints,
    previewable: false,
    onActivityCreated: async (payload, onComplete, onError) => {
        const { fields: { recipient, parameters, from, flow_id }, } = (0, extensions_core_1.validate)({
            schema: config_1.CreateFlowExecutionSchema,
            payload,
        });
        // TODO: get rid of this assertion
        if ((0, lodash_1.isNil)(from)) {
            throw new Error('`from` should never be invalid');
        }
        const { client } = await (0, createSdkClient_1.createSdkClient)({ payload, skipRegion: true });
        const execution = await client.studio.v2.flows(flow_id).executions.create({
            to: recipient,
            from,
            parameters,
        });
        await onComplete({
            data_points: {
                executionId: execution.sid,
            },
        });
    },
};
//# sourceMappingURL=createFlowExecution.js.map