"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stopCareFlow = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const zod_1 = require("zod");
const addEventLog_1 = require("../../../../../src/lib/awell/addEventLog");
exports.stopCareFlow = {
    key: 'stopCareFlow',
    category: extensions_core_1.Category.WORKFLOW,
    title: 'Stop care flow',
    description: 'Stop the care flow the patient is currently enrolled.',
    fields: config_1.fields,
    previewable: false,
    onEvent: async ({ payload, onComplete, helpers }) => {
        const { fields: { careFlowIds, reason }, pathway: { id: currentCareFlowId }, } = (0, extensions_core_1.validate)({
            schema: zod_1.z.object({
                fields: config_1.FieldsValidationSchema,
                pathway: config_1.PathwayValidationSchema,
            }),
            payload,
        });
        const awellSdk = await helpers.awellSdk();
        const events = [];
        if (careFlowIds.length > 0) {
            for (const careFlowId of careFlowIds) {
                await awellSdk.orchestration.mutation({
                    stopPathway: {
                        __args: {
                            input: {
                                pathway_id: careFlowId,
                                reason,
                            },
                        },
                        code: true,
                        success: true,
                    },
                });
                events.push((0, addEventLog_1.addActivityEventLog)({
                    message: `Care flow ${careFlowId} successfully stopped.`,
                }));
                // wait 1 second for rate limiting
                await new Promise((resolve) => setTimeout(resolve, 1000));
            }
        }
        else {
            await awellSdk.orchestration.mutation({
                stopPathway: {
                    __args: {
                        input: {
                            pathway_id: currentCareFlowId,
                            reason,
                        },
                    },
                    code: true,
                    success: true,
                },
            });
            events.push((0, addEventLog_1.addActivityEventLog)({
                message: `Care flow ${currentCareFlowId} successfully stopped.`,
            }));
        }
        await onComplete({
            events,
        });
    },
};
//# sourceMappingURL=stopCareFlow.js.map