"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.
    supports_automated_retries: true,
    onEvent: async ({ payload, onComplete, helpers }) => {
        var _a, _b, _c;
        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 stoppableTrackStatuses = ['active'];
        const tracksResponse = await awellSdk.orchestration.query({
            careflowTracks: {
                __args: {
                    careflow_id: careFlowId,
                    statuses: stoppableTrackStatuses,
                },
                tracks: {
                    id: true,
                    status: true,
                    definition_id: true,
                    title: true,
                },
            },
        });
        const activeTracks = (_c = (_b = (_a = tracksResponse.careflowTracks) === null || _a === void 0 ? void 0 : _a.tracks) === null || _b === void 0 ? void 0 : _b.filter((track) => track.definition_id === trackDefinitionId &&
            stoppableTrackStatuses.includes(track.status))) !== null && _c !== void 0 ? _c : [];
        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.title} 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