"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stopTrack = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const zod_1 = require("zod");
const addEventLog_1 = require("../../../../../src/lib/awell/addEventLog");
const config_1 = require("./config");
exports.stopTrack = {
    key: 'stopTrack',
    category: extensions_core_1.Category.WORKFLOW,
    title: 'Stop track',
    description: 'Stop a track in a patient care flow (pathway).',
    fields: config_1.fields,
    previewable: false, // We don't have pathways in Preview, only cases.
    onEvent: async ({ payload, onComplete, helpers }) => {
        var _a, _b;
        const { fields: { trackDefinitionId }, pathway: { id: careFlowId }, } = (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 elementsResponse = await awellSdk.orchestration.query({
            pathwayElements: {
                __args: {
                    pathway_id: careFlowId,
                },
                elements: {
                    id: true,
                    status: true,
                    definition_id: true,
                    name: true,
                },
            },
        });
        const activeTracks = (_b = (_a = elementsResponse.pathwayElements) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.filter((element) => element.definition_id === trackDefinitionId &&
            (element.status === 'ACTIVE' ||
                element.status === 'SCHEDULED' ||
                element.status === 'POSTPONED'));
        if (activeTracks.length > 0) {
            const events = [];
            for (const activeTrack of activeTracks) {
                await awellSdk.orchestration.mutation({
                    stopTrack: {
                        __args: {
                            input: {
                                track_id: activeTrack.id,
                                pathway_id: careFlowId,
                            },
                        },
                        code: true,
                        success: true,
                    },
                });
                events.push((0, addEventLog_1.addActivityEventLog)({
                    message: `Track ${activeTrack.name} with ID ${activeTrack.id} successfully stopped.`,
                }));
            }
            await onComplete({
                events,
            });
        }
        else {
            await onComplete({
                events: [
                    (0, addEventLog_1.addActivityEventLog)({
                        message: `No active track found with definition ID ${trackDefinitionId} in care flow ${careFlowId}.`,
                    }),
                ],
            });
        }
    },
};
//# sourceMappingURL=stopTrack.js.map