"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLatestFormInCurrentStep = void 0;
const lodash_1 = require("lodash");
const getLatestFormInCurrentStep = async ({ awellSdk, pathwayId, activityId }) => {
    var _a, _b, _c, _d;
    const activityResponse = await awellSdk.orchestration.query({
        activity: {
            __args: {
                id: activityId,
            },
            success: true,
            activity: {
                date: true,
                context: {
                    step_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 currentStepId = (_d = (_c = (_b = activityResponse.activity) === null || _b === void 0 ? void 0 : _b.activity) === null || _c === void 0 ? void 0 : _c.context) === null || _d === void 0 ? void 0 : _d.step_id;
    if ((0, lodash_1.isNil)(currentStepId))
        throw new Error('Could not find step ID of the current activity');
    const activitiesInCurrentStepResponse = await awellSdk.orchestration.query({
        pathwayStepActivities: {
            __args: {
                pathway_id: pathwayId,
                step_id: currentStepId,
            },
            success: true,
            activities: {
                id: true,
                status: true,
                date: true,
                object: {
                    id: true,
                    name: true,
                    type: true,
                },
                context: {
                    step_id: true,
                },
            },
        },
    });
    // Capture completed form activities and sort by date in descending order
    const formActivitiesInCurrentStep = activitiesInCurrentStepResponse.pathwayStepActivities.activities
        .filter((a) => a.object.type === 'FORM' &&
        a.status === 'DONE' &&
        a.date <= currentActivity.date)
        .sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
    // Grab the most recent form activity
    const formActivity = formActivitiesInCurrentStep[0];
    if ((0, lodash_1.isNil)(formActivity))
        throw new Error('No (completed) form action found in the current step');
    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,
        formDefinition: formDefinition.form.form,
        formResponse: formResponse.formResponse.response,
    };
};
exports.getLatestFormInCurrentStep = getLatestFormInCurrentStep;
//# sourceMappingURL=getLatestFormInCurrentStep.js.map