"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.onAnyPush = exports.whenPushSatisfies = exports.GoalSetterMapping = void 0;
const Goals_1 = require("../goal/Goals");
const goalTest_1 = require("../mapping/goalTest");
const commonPushTests_1 = require("../mapping/support/commonPushTests");
const PredicateMappingTerm_1 = require("../mapping/support/PredicateMappingTerm");
const PushRule_1 = require("../mapping/support/PushRule");
const GoalComponent_1 = require("./GoalComponent");
/**
 * PushRule implementation exposed in DSL. Continues fluent API.
 */
class GoalSetterMapping extends PushRule_1.PushRule {
    constructor(guard1, guards, reason) {
        super(goalTest_1.wrapPredicateMapping(guard1), (guards || []).map(goalTest_1.wrapPredicateMapping), reason);
    }
    get label() {
        return this.goalsName;
    }
    setGoals(goalComponent) {
        if (!!goalComponent && goalComponent.name) {
            this.goalsName = goalComponent.name;
        }
        if (!goalComponent) {
            return this.set(goalComponent);
        }
        return this.set(GoalComponent_1.toGoals(goalComponent));
    }
    /**
     * Set goals dynamically, depending on this push
     * @param {(inv: P) => GoalComponent} f
     * @return {this}
     */
    setGoalsWhen(f) {
        // This is a bit devious. We override the parent mapping
        this.mapping = async (pu) => {
            // Check whether the enclosing test would have let the push through
            const shouldEvaluate = await this.pushTest.mapping(pu);
            if (!shouldEvaluate) {
                return undefined;
            }
            const goalComponent = await f(pu);
            if (!!goalComponent && goalComponent.name) {
                this.goalsName = goalComponent.name;
            }
            if (!goalComponent) {
                return goalComponent;
            }
            return GoalComponent_1.toGoals(goalComponent);
        };
        return this;
    }
    /**
     * Prevent setting any further goals on this push. Ordering matters:
     * goals may previously have been set.
     * @param name name of the empty Goals. Default is "No Goals"
     */
    setNoMoreGoals(name) {
        this.setGoals(Goals_1.goals(name || "No Goals").andLock());
    }
}
exports.GoalSetterMapping = GoalSetterMapping;
/**
 * Simple GoalSetter DSL. Allows use of booleans and functions
 * returning boolean in predicate expressions
 * @param {PushTest} guard1
 * @param {PushTest} guards
 */
function whenPushSatisfies(guard1, ...guards) {
    return new GoalSetterMapping(PredicateMappingTerm_1.toPredicateMapping(guard1), guards.map(PredicateMappingTerm_1.toPredicateMapping));
}
exports.whenPushSatisfies = whenPushSatisfies;
/**
 * PushRule that matches every push
 * @type {GoalSetterMapping}
 */
function onAnyPush() {
    return new GoalSetterMapping(commonPushTests_1.AnyPush, [], "On any push");
}
exports.onAnyPush = onAnyPush;
//# sourceMappingURL=goalDsl.js.map