"use strict";
/*
 * Copyright © 2020 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.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestDownstreamGoalsOnGoalSuccess = void 0;
const decorators_1 = require("@atomist/automation-client/lib/decorators");
const globals_1 = require("@atomist/automation-client/lib/globals");
const graphQL_1 = require("@atomist/automation-client/lib/graph/graphQL");
const HandlerResult_1 = require("@atomist/automation-client/lib/HandlerResult");
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const fetchGoalsOnCommit_1 = require("../../../../../api-helper/goal/fetchGoalsOnCommit");
const goalPreconditions_1 = require("../../../../../api-helper/goal/goalPreconditions");
const sdmGoal_1 = require("../../../../../api-helper/goal/sdmGoal");
const storeGoals_1 = require("../../../../../api-helper/goal/storeGoals");
const handlerRegistrations_1 = require("../../../../../api-helper/machine/handlerRegistrations");
const skillContext_1 = require("../../../../../api/context/skillContext");
const SdmGoalMessage_1 = require("../../../../../api/goal/SdmGoalMessage");
const types_1 = require("../../../../../typings/types");
const validateGoal_1 = require("../../../../delivery/goals/support/validateGoal");
const goalSigning_1 = require("../../../../signing/goalSigning");
/**
 * Move downstream goals from 'planned' to 'requested' when preconditions are met.
 */
let RequestDownstreamGoalsOnGoalSuccess = class RequestDownstreamGoalsOnGoalSuccess {
    constructor(name, implementationMapper, repoRefResolver, credentialsResolver, preferenceStoreFactory) {
        this.name = name;
        this.implementationMapper = implementationMapper;
        this.repoRefResolver = repoRefResolver;
        this.credentialsResolver = credentialsResolver;
        this.preferenceStoreFactory = preferenceStoreFactory;
    }
    async handle(event, context) {
        const sdmGoal = event.data.SdmGoal[0];
        if (!validateGoal_1.shouldHandle(sdmGoal)) {
            logger_1.logger.debug(`Goal ${sdmGoal.uniqueName} skipped because not managed by this SDM`);
            return HandlerResult_1.Success;
        }
        await goalSigning_1.verifyGoal(sdmGoal, this.configuration.sdm.goalSigning, context);
        const id = this.repoRefResolver.repoRefFromPush(sdmGoal.push);
        const credentials = await handlerRegistrations_1.resolveCredentialsPromise(this.credentialsResolver.eventHandlerCredentials(context, id));
        const preferences = this.preferenceStoreFactory(context);
        const goals = fetchGoalsOnCommit_1.fetchGoalsFromPush(sdmGoal);
        const goalsToRequest = goals.filter(g => isDirectlyDependentOn(sdmGoal, g))
            .filter(g => expectToBeFulfilledAfterRequest(g, this.name))
            .filter(shouldBePlannedOrSkipped)
            .filter(g => goalPreconditions_1.preconditionsAreMet(g, { goalsForCommit: goals }));
        if (goalsToRequest.length > 0) {
            logger_1.logger.debug("because %s is successful, these goals are now ready: %s", sdmGoal_1.goalKeyString(sdmGoal), goalsToRequest.map(sdmGoal_1.goalKeyString).join(", "));
        }
        await Promise.all(goalsToRequest.map(async (sdmG) => {
            if (sdmG.preApprovalRequired) {
                return storeGoals_1.updateGoal(context, sdmG, {
                    state: types_1.SdmGoalState.waiting_for_pre_approval,
                    description: !!sdmGoal.descriptions && !!sdmGoal.descriptions.waitingForPreApproval
                        ? sdmGoal.descriptions.waitingForPreApproval : `Start required: ${sdmG.name}`,
                });
            }
            else {
                let g = sdmG;
                const cbs = this.implementationMapper.findFulfillmentCallbackForGoal(sdmG);
                for (const cb of cbs) {
                    g = await cb.callback(g, {
                        id,
                        addressChannels: undefined,
                        preferences,
                        configuration: this.configuration,
                        credentials,
                        context,
                        skill: skillContext_1.createSkillContext(context),
                    });
                }
                return storeGoals_1.updateGoal(context, g, {
                    state: types_1.SdmGoalState.requested,
                    description: !!sdmGoal.descriptions && !!sdmGoal.descriptions.requested
                        ? sdmGoal.descriptions.requested : `Ready: ${g.name}`,
                    data: g.data,
                });
            }
        }));
        return HandlerResult_1.Success;
    }
};
__decorate([
    decorators_1.Value(""),
    __metadata("design:type", Object)
], RequestDownstreamGoalsOnGoalSuccess.prototype, "configuration", void 0);
RequestDownstreamGoalsOnGoalSuccess = __decorate([
    decorators_1.EventHandler("Move downstream goals from 'planned' to 'requested' when preconditions are met", () => graphQL_1.subscription({
        name: "OnAnySuccessfulSdmGoal",
        variables: { registration: () => { var _a, _b; return [(_b = (_a = globals_1.automationClientInstance()) === null || _a === void 0 ? void 0 : _a.configuration) === null || _b === void 0 ? void 0 : _b.name]; } },
    })),
    __metadata("design:paramtypes", [String, Object, Object, Object, Function])
], RequestDownstreamGoalsOnGoalSuccess);
exports.RequestDownstreamGoalsOnGoalSuccess = RequestDownstreamGoalsOnGoalSuccess;
function shouldBePlannedOrSkipped(dependentGoal) {
    if (dependentGoal.state === types_1.SdmGoalState.planned) {
        return true;
    }
    if (dependentGoal.state === types_1.SdmGoalState.skipped) {
        logger_1.logger.debug("Goal %s was skipped, but now maybe it can go", dependentGoal.uniqueName);
        return true;
    }
    if (dependentGoal.state === types_1.SdmGoalState.failure && dependentGoal.retryFeasible) {
        logger_1.logger.debug("Goal %s failed, but maybe we will retry it", dependentGoal.uniqueName);
        return true;
    }
    logger_1.logger.debug("Goal '%s' in state '%s' will not be requested", dependentGoal.uniqueName, dependentGoal.state);
    return false;
}
function expectToBeFulfilledAfterRequest(dependentGoal, name) {
    switch (dependentGoal.fulfillment.method) {
        case SdmGoalMessage_1.SdmGoalFulfillmentMethod.Sdm:
            return true;
        case SdmGoalMessage_1.SdmGoalFulfillmentMethod.SideEffect:
            return dependentGoal.fulfillment.name !== name;
        case SdmGoalMessage_1.SdmGoalFulfillmentMethod.Other:
            // legacy behavior
            return true;
        default:
            return false;
    }
}
function isDirectlyDependentOn(successfulGoal, goal) {
    if (!goal) {
        return false;
    }
    if (!goal.preConditions || goal.preConditions.length === 0) {
        return false; // no preconditions? not dependent
    }
    if (sdmGoal_1.mapKeyToGoal(goal.preConditions)(successfulGoal)) {
        logger_1.logger.debug("%s depends on %s", goal.uniqueName, successfulGoal.uniqueName);
        return true; // the failed goal is one of my preconditions? dependent
    }
    return false;
}
//# sourceMappingURL=RequestDownstreamGoalsOnGoalSuccess.js.map