"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.preconditionsAreMet = void 0;
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const sprintf_js_1 = require("sprintf-js");
const types_1 = require("../../typings/types");
const sdmGoal_1 = require("./sdmGoal");
/*
 * Right now the only preconditions supported are other goals.
 * The intention is that others will be expressed, such as requiring an image.
 */
function preconditionsAreMet(goal, info) {
    if (!goal.preConditions || goal.preConditions.length === 0) {
        return true;
    }
    const falsification = goal.preConditions.find(p => !satisfied(p, info.goalsForCommit));
    if (falsification) {
        logger_1.logger.debug("Precondition not met for %s: %s", sdmGoal_1.goalKeyString(goal), sdmGoal_1.goalKeyString(falsification));
        return false;
    }
    logger_1.logger.debug("All %d preconditions satisfied for %s", goal.preConditions.length, sdmGoal_1.goalKeyString(goal));
    return true;
}
exports.preconditionsAreMet = preconditionsAreMet;
function satisfied(preconditionKey, goalsForCommit) {
    const preconditionGoal = sdmGoal_1.mapKeyToGoal(goalsForCommit)(preconditionKey);
    if (!preconditionGoal) {
        logger_1.logger.error("Precondition %s not found on commit", sdmGoal_1.goalKeyString(preconditionKey));
        return true;
    }
    switch (preconditionGoal.state) {
        case types_1.SdmGoalState.failure:
        case types_1.SdmGoalState.skipped:
        case types_1.SdmGoalState.canceled:
        case types_1.SdmGoalState.stopped:
            logger_1.logger.debug("Precondition %s in state %s, won't be met", sdmGoal_1.goalKeyString(preconditionKey), preconditionGoal.state);
            return false;
        case types_1.SdmGoalState.planned:
        case types_1.SdmGoalState.requested:
        case types_1.SdmGoalState.waiting_for_approval:
        case types_1.SdmGoalState.approved:
        case types_1.SdmGoalState.waiting_for_pre_approval:
        case types_1.SdmGoalState.pre_approved:
        case types_1.SdmGoalState.in_process:
            logger_1.logger.debug("Not yet. %s in state %s", sdmGoal_1.goalKeyString(preconditionKey), preconditionGoal.state);
            return false;
        case "success":
            return true;
        default:
            throw new Error(sprintf_js_1.sprintf("Unhandled state: %s on %s", preconditionGoal.state, sdmGoal_1.goalKeyString(preconditionKey)));
    }
}
//# sourceMappingURL=goalPreconditions.js.map