"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLastCalculationActivityInCurrentStep = void 0;
const lodash_1 = require("lodash");
const getLastCalculationActivityInCurrentStep = async ({ awellSdk, pathwayId, currentActivityId }) => {
    var _a, _b, _c, _d;
    const activityResponse = await awellSdk.orchestration.query({
        activity: {
            __args: {
                id: currentActivityId,
            },
            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: {
                    name: true,
                    type: true,
                },
                context: {
                    step_id: true,
                },
            },
        },
    });
    const calculationActivitiesInCurrentStep = activitiesInCurrentStepResponse.pathwayStepActivities.activities.filter((a) => a.object.type === 'CALCULATION' &&
        a.status === 'DONE' &&
        a.date <= currentActivity.date);
    // Grab the most recent calculation activity
    const calculationActivity = calculationActivitiesInCurrentStep[0];
    if ((0, lodash_1.isNil)(calculationActivity))
        return null;
    return calculationActivity;
};
exports.getLastCalculationActivityInCurrentStep = getLastCalculationActivityInCurrentStep;
//# sourceMappingURL=getLastCalculationActivityInCurrentStep.js.map