"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.goalStateSupport = void 0;
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const cluster = require("cluster");
const _ = require("lodash");
const extensionPack_1 = require("../../api-helper/misc/extensionPack");
const modes_1 = require("../../core/machine/modes");
const cancelGoals_1 = require("./cancelGoals");
const manageGoalSets_1 = require("./manageGoalSets");
const resetGoals_1 = require("./resetGoals");
const setGoalState_1 = require("./setGoalState");
/**
 * Allow goal setting
 */
function goalStateSupport(options) {
    return Object.assign(Object.assign({}, extensionPack_1.metadata("goal-state")), { configure: sdm => {
            if (modes_1.isInLocalMode()) {
                logger_1.logger.warn("Setting goal state is not available in local mode.");
                logger_1.logger.warn("Resetting goals does not work in local mode. Use `atomist trigger post-commit` instead.");
            }
            else {
                sdm.addCommand(setGoalState_1.setGoalStateCommand(sdm));
                sdm.addCommand(resetGoals_1.resetGoalsCommand(sdm));
                sdm.addCommand(cancelGoals_1.cancelGoalSetsCommand(sdm));
                sdm.addCommand(cancelGoals_1.listPendingGoalSetsCommand(sdm));
                if ((cluster.isMaster || !_.get(sdm.configuration, "cluster.enabled")) &&
                    !process.env.ATOMIST_ISOLATED_GOAL &&
                    !!options &&
                    !!options.cancellation &&
                    !!options.cancellation.enabled) {
                    logger_1.logger.debug(`Timeout based goal cancellation enabled for this SDM`);
                    sdm.addTriggeredListener({
                        trigger: { interval: 1000 * 30 },
                        listener: manageGoalSets_1.manageGoalSetsTrigger(options.cancellation),
                    });
                }
            }
        } });
}
exports.goalStateSupport = goalStateSupport;
//# sourceMappingURL=goalState.js.map