"use strict";
/*
 * Copyright © 2019 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.GoalSetGoalCompletionListener = void 0;
const GraphClient_1 = require("@atomist/automation-client/lib/spi/graph/GraphClient");
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const _ = require("lodash");
const storeGoals_1 = require("../goal/storeGoals");
/**
 * Update the state of the SdmGoalSet as the goals progress
 * @param gcl
 */
const GoalSetGoalCompletionListener = async (gcl) => {
    const state = storeGoals_1.goalSetState(gcl.allGoals || []);
    logger_1.logger.debug(`GoalSet '${gcl.completedGoal.goalSetId}' now in state '${state}' because goal '${gcl.completedGoal.uniqueName}' was '${gcl.completedGoal.state}'`);
    const result = await gcl.context.graphClient.query({
        name: "SdmGoalSetForId",
        variables: {
            goalSetId: [gcl.completedGoal.goalSetId],
        },
        options: GraphClient_1.QueryNoCacheOptions,
    });
    const goalSet = _.get(result, "SdmGoalSet[0]");
    if (!!goalSet) {
        if (goalSet.state !== state) {
            const newGoalSet = Object.assign(Object.assign({}, goalSet), { state });
            await storeGoals_1.storeGoalSet(gcl.context, newGoalSet);
        }
    }
};
exports.GoalSetGoalCompletionListener = GoalSetGoalCompletionListener;
//# sourceMappingURL=goalSetListener.js.map