"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFormsInTrack = void 0;
const lodash_1 = require("lodash");
const getFormsInTrack = async ({ awellSdk, pathwayId, activityId, }) => {
    var _a, _b;
    // First get the current activity to find the track_id and cutoff date
    const activityResponse = await awellSdk.orchestration.query({
        activity: {
            __args: {
                id: activityId,
            },
            success: true,
            activity: {
                id: true,
                date: true,
                context: {
                    track_id: true,
                },
            },
        },
    });
    const currentActivity = (_a = activityResponse.activity) === null || _a === void 0 ? void 0 : _a.activity;
    if ((0, lodash_1.isNil)(currentActivity)) {
        throw new Error('Cannot find the current activity');
    }
    const trackId = (_b = currentActivity.context) === null || _b === void 0 ? void 0 : _b.track_id;
    if ((0, lodash_1.isNil)(trackId) || trackId.trim() === '') {
        throw new Error('Could not find track ID for the current activity');
    }
    // Get all activities in the track
    const activitiesResponse = await awellSdk.orchestration.query({
        careflowActivities: {
            __args: {
                pathway_id: pathwayId,
                filters: { track_id: trackId },
                pagination: { offset: 0, count: 500 },
            },
            success: true,
            activities: {
                id: true,
                status: true,
                date: true,
                object: {
                    id: true,
                    name: true,
                    type: true,
                },
            },
        },
    });
    // Filter and sort by date in ascending order (chronological)
    const formActivitiesInTrack = activitiesResponse.careflowActivities.activities
        .filter((a) => a.object.type === 'FORM' &&
        a.status === 'DONE' &&
        a.date <= currentActivity.date)
        .sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime());
    if (formActivitiesInTrack.length === 0) {
        return [];
    }
    // Fetch form definitions and responses for all forms
    return await Promise.all(formActivitiesInTrack.map(async (formActivity) => {
        const formDefinition = await awellSdk.orchestration.query({
            form: {
                __args: {
                    id: formActivity.object.id,
                    pathway_id: pathwayId,
                },
                form: {
                    __scalar: true,
                    questions: {
                        __scalar: true,
                        options: {
                            __scalar: true,
                        },
                    },
                },
            },
        });
        const formResponse = await awellSdk.orchestration.query({
            formResponse: {
                __args: {
                    pathway_id: pathwayId,
                    activity_id: formActivity.id,
                },
                response: {
                    answers: {
                        __scalar: true,
                    },
                },
            },
        });
        return {
            formActivityId: formActivity.id,
            formId: formActivity.object.id,
            formName: formActivity.object.name,
            formDefinition: formDefinition.form.form,
            formResponse: formResponse.formResponse.response,
        };
    }));
};
exports.getFormsInTrack = getFormsInTrack;
//# sourceMappingURL=getFormsInTrack.js.map