"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addTestToPlan = addTestToPlan;
const mablApi_1 = require("../../mablApi");
async function addTestToPlan(apiClient, planId, testInvariantId) {
    const plan = await apiClient.getPlan(planId);
    if (!plan.execution_stages?.[0]?.journeys?.[0]) {
        plan.journeys = [];
        plan.execution_stages = [
            { journeys: [], concurrency: mablApi_1.ExecutionStage.ConcurrencyEnum.Parallel },
        ];
    }
    const alreadyInPlan = plan.execution_stages.some((stage) => stage.journeys?.some((journey) => journey.journey_id === testInvariantId));
    if (alreadyInPlan) {
        return;
    }
    const newJourney = { journey_id: testInvariantId };
    const lastStage = plan.execution_stages[plan.execution_stages.length - 1];
    lastStage.journeys = lastStage.journeys ?? [];
    lastStage.journeys.push(newJourney);
    plan.journeys = plan.journeys ?? [];
    plan.journeys.push(newJourney);
    await apiClient.updatePlan(planId, plan);
}
