"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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.HandlerBasedSoftwareDeliveryMachine = void 0;
const _ = require("lodash");
const AbstractSoftwareDeliveryMachine_1 = require("../../api-helper/machine/AbstractSoftwareDeliveryMachine");
const FulfillGoalOnRequested_1 = require("../handlers/events/delivery/goals/FulfillGoalOnRequested");
const RequestDownstreamGoalsOnGoalSuccess_1 = require("../handlers/events/delivery/goals/RequestDownstreamGoalsOnGoalSuccess");
const RespondOnGoalCompletion_1 = require("../handlers/events/delivery/goals/RespondOnGoalCompletion");
const SetGoalsOnGoal_1 = require("../handlers/events/delivery/goals/SetGoalsOnGoal");
const SetGoalsOnPush_1 = require("../handlers/events/delivery/goals/SetGoalsOnPush");
const SkipDownstreamGoalsOnGoalFailure_1 = require("../handlers/events/delivery/goals/SkipDownstreamGoalsOnGoalFailure");
const VoteOnGoalApprovalRequest_1 = require("../handlers/events/delivery/goals/VoteOnGoalApprovalRequest");
const ClosedIssueHandler_1 = require("../handlers/events/issue/ClosedIssueHandler");
const NewIssueHandler_1 = require("../handlers/events/issue/NewIssueHandler");
const UpdatedIssueHandler_1 = require("../handlers/events/issue/UpdatedIssueHandler");
const OnChannelLink_1 = require("../handlers/events/repo/OnChannelLink");
const OnFirstPushToRepo_1 = require("../handlers/events/repo/OnFirstPushToRepo");
const OnPullRequest_1 = require("../handlers/events/repo/OnPullRequest");
const OnRepoCreation_1 = require("../handlers/events/repo/OnRepoCreation");
const OnRepoOnboarded_1 = require("../handlers/events/repo/OnRepoOnboarded");
const OnTag_1 = require("../handlers/events/repo/OnTag");
const OnUserJoiningChannel_1 = require("../handlers/events/repo/OnUserJoiningChannel");
/**
 * Implementation of SoftwareDeliveryMachine based on Atomist event handlers.
 * Not intended for direct user instantiation. See machineFactory.ts
 */
class HandlerBasedSoftwareDeliveryMachine extends AbstractSoftwareDeliveryMachine_1.AbstractSoftwareDeliveryMachine {
    get onRepoCreation() {
        return this.repoCreationListeners.length > 0 ?
            () => new OnRepoCreation_1.OnRepoCreation(this.repoCreationListeners, this.configuration.sdm.repoRefResolver, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory) :
            undefined;
    }
    get onFirstPush() {
        return this.firstPushListeners.length > 0 ?
            () => new OnFirstPushToRepo_1.OnFirstPushToRepo(this.firstPushListeners, this.configuration.sdm.repoRefResolver, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory) :
            undefined;
    }
    get goalSetting() {
        if (this.pushMapping) {
            return {
                eventHandlers: [
                    () => new SetGoalsOnPush_1.SetGoalsOnPush(this.configuration.sdm.projectLoader, this.configuration.sdm.repoRefResolver, this.pushMapping, this.goalsSetListeners, this.goalFulfillmentMapper, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory, this.configuration.sdm.enrichGoal, this.configuration.sdm.tagGoalSet),
                    () => new SetGoalsOnGoal_1.SetGoalsOnGoal(this.configuration.sdm.projectLoader, this.configuration.sdm.repoRefResolver, this.pushMapping, this.goalsSetListeners, this.goalFulfillmentMapper, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory, this.configuration.sdm.enrichGoal, this.configuration.sdm.tagGoalSet),
                ],
                commandHandlers: [],
                ingesters: [],
            };
        }
        else {
            return {
                eventHandlers: [],
                commandHandlers: [],
                ingesters: [],
            };
        }
    }
    get goalConsequences() {
        if (this.pushMapping) {
            return {
                eventHandlers: [
                    () => new SkipDownstreamGoalsOnGoalFailure_1.SkipDownstreamGoalsOnGoalFailure(),
                    () => new RequestDownstreamGoalsOnGoalSuccess_1.RequestDownstreamGoalsOnGoalSuccess(this.configuration.name, this.goalFulfillmentMapper, this.configuration.sdm.repoRefResolver, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory),
                    () => new RespondOnGoalCompletion_1.RespondOnGoalCompletion(this.configuration.sdm.repoRefResolver, this.configuration.sdm.credentialsResolver, this.goalCompletionListeners, this.configuration.sdm.preferenceStoreFactory),
                    () => new VoteOnGoalApprovalRequest_1.VoteOnGoalApprovalRequest(this.configuration.sdm.repoRefResolver, this.configuration.sdm.credentialsResolver, this.goalApprovalRequestVoters, this.goalApprovalRequestVoteDecisionManager, this.goalFulfillmentMapper, this.configuration.sdm.preferenceStoreFactory)
                ],
                commandHandlers: [],
                ingesters: [],
            };
        }
        else {
            return {
                eventHandlers: [],
                commandHandlers: [],
                ingesters: [],
            };
        }
    }
    get allFunctionalUnits() {
        return []
            .concat([
            this.goalSetting,
            this.goalConsequences,
        ]);
    }
    get eventHandlers() {
        return this.registrationManager.eventHandlers
            .concat(this.goalFulfillmentMapper.hasImplementation() ? () => new FulfillGoalOnRequested_1.FulfillGoalOnRequested(this.goalFulfillmentMapper, this.goalExecutionListeners) : undefined)
            .concat(_.flatten(this.allFunctionalUnits.map(fu => fu.eventHandlers)))
            .concat([
            this.userJoiningChannelListeners.length > 0 ?
                () => new OnUserJoiningChannel_1.OnUserJoiningChannel(this.userJoiningChannelListeners, this.configuration.sdm.repoRefResolver, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory) :
                undefined,
            this.tagListeners.length > 0 ?
                () => new OnTag_1.OnTag(this.tagListeners, this.configuration.sdm.repoRefResolver, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory) :
                undefined,
            this.newIssueListeners.length > 0 ?
                () => new NewIssueHandler_1.NewIssueHandler(this.newIssueListeners, this.configuration.sdm.repoRefResolver, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory) :
                undefined,
            this.updatedIssueListeners.length > 0 ?
                () => new UpdatedIssueHandler_1.UpdatedIssueHandler(this.updatedIssueListeners, this.configuration.sdm.repoRefResolver, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory) :
                undefined,
            this.closedIssueListeners.length > 0 ?
                () => new ClosedIssueHandler_1.ClosedIssueHandler(this.closedIssueListeners, this.configuration.sdm.repoRefResolver, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory) :
                undefined,
            this.channelLinkListeners.length > 0 ?
                () => new OnChannelLink_1.OnChannelLink(this.configuration.sdm.projectLoader, this.configuration.sdm.repoRefResolver, this.channelLinkListeners, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory) :
                undefined,
            this.pullRequestListeners.length > 0 ?
                () => new OnPullRequest_1.OnPullRequest(this.configuration.sdm.projectLoader, this.configuration.sdm.repoRefResolver, this.pullRequestListeners, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory) : undefined,
            this.repoOnboardingListeners.length > 0 ?
                () => new OnRepoOnboarded_1.OnRepoOnboarded(this.repoOnboardingListeners, this.configuration.sdm.repoRefResolver, this.configuration.sdm.credentialsResolver, this.configuration.sdm.preferenceStoreFactory) :
                undefined,
            this.onRepoCreation,
            this.onFirstPush,
        ])
            .filter(m => !!m);
    }
    get commandHandlers() {
        return this.registrationManager.commandHandlers
            .concat(_.flatten(this.allFunctionalUnits.map(fu => fu.commandHandlers)))
            .filter(m => !!m);
    }
    get ingesters() {
        return this.registrationManager.ingesters
            .concat(_.flatten(this.allFunctionalUnits.map(fu => fu.ingesters)))
            .filter(m => !!m);
    }
    /**
     * Construct a new software delivery machine, with zero or
     * more goal setters.
     * @param {string} name
     * @param configuration automation client configuration we're running in
     * @param {GoalSetter} goalSetters tell me what to do on a push. Hint: start with "whenPushSatisfies(...)"
     */
    constructor(name, configuration, goalSetters) {
        super(name, configuration, goalSetters);
    }
}
exports.HandlerBasedSoftwareDeliveryMachine = HandlerBasedSoftwareDeliveryMachine;
//# sourceMappingURL=HandlerBasedSoftwareDeliveryMachine.js.map