"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllFormsInCurrentStep = void 0;
const lodash_1 = require("lodash");
const getAllFormsInCurrentStep = async ({ awellSdk, pathwayId, activityId, }) => {
    var _a, _b;
    const activity_response = await awellSdk.orchestration
        .query({
        activity: {
            __args: {
                id: activityId,
            },
            success: true,
            activity: {
                id: true,
                status: true,
                date: true,
                object: {
                    id: true,
                    type: true,
                },
                context: {
                    step_id: true,
                },
            },
        },
    })
        .catch((error) => {
        console.error(`Failed to fetch activity ${activityId}`, error);
        throw new Error(`Failed to fetch activity ${activityId}`);
    });
    const currentActivity = (_a = activity_response === null || activity_response === void 0 ? void 0 : activity_response.activity) === null || _a === void 0 ? void 0 : _a.activity;
    if ((0, lodash_1.isNil)(currentActivity) || !activity_response.activity.success)
        throw new Error(`Failed to fetch activity ${activityId}`);
    const currentStepId = (_b = currentActivity.context) === null || _b === void 0 ? void 0 : _b.step_id;
    if ((0, lodash_1.isNil)(currentStepId))
        throw new Error('Could not find step ID of the current activity');
    const activities = await awellSdk.orchestration.query({
        pathwayStepActivities: {
            __args: {
                pathway_id: pathwayId,
                step_id: currentStepId,
            },
            success: true,
            activities: {
                id: true,
                status: true,
                date: true,
                object: {
                    id: true,
                    type: true,
                },
                context: {
                    step_id: true,
                },
            },
        },
    });
    // Filter and sort by date in ascending order (chronological)
    const formActivitiesInCurrentStep = activities.pathwayStepActivities.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 ((0, lodash_1.isEmpty)(formActivitiesInCurrentStep))
        return [];
    const getFormDefinitionAndFormResponse = async (formActivity) => {
        const formDefinition = await awellSdk.orchestration.query({
            form: {
                __args: {
                    id: formActivity.object.id,
                },
                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 {
            formDefinition: formDefinition.form.form,
            formResponse: formResponse.formResponse.response,
        };
    };
    return await Promise.all(formActivitiesInCurrentStep.map(async (formActivity) => {
        const { formDefinition, formResponse } = await getFormDefinitionAndFormResponse(formActivity);
        return {
            formActivityId: formActivity.id,
            formId: formActivity.object.id,
            formDefinition,
            formResponse,
        };
    }));
};
exports.getAllFormsInCurrentStep = getAllFormsInCurrentStep;
//# sourceMappingURL=getAllFormsInCurrentStep.js.map