"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.isPredicatedStaticValue = exports.PushRule = void 0;
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const pushTestUtils_1 = require("./pushTestUtils");
/**
 * Generic DSL support for returning an object on a push
 */
class PushRule {
    constructor(guard1, guards, reason) {
        this.guard1 = guard1;
        this.guards = guards;
        this.pushTest = pushTestUtils_1.allSatisfied(guard1, ...guards);
        this.reason = reason || this.pushTest.name;
    }
    get value() {
        return this.staticValue;
    }
    get name() {
        return this.reason;
    }
    get test() {
        return this.pushTest;
    }
    /**
     * Set an additional reason if we want to add information to that which is
     * available from the push tests themselves
     * @param {string} reason
     * @return {this}
     */
    itMeans(reason) {
        this.reason = reason;
        return this;
    }
    /**
     * Set the value that will be resolved from this rule
     * @param {V} value
     * @return {this}
     */
    set(value) {
        this.staticValue = value;
        return this;
    }
    async mapping(p) {
        if (await this.pushTest.mapping(p)) {
            logger_1.logger.debug(`Push test passed: ${this.pushTest.name}`);
            return this.staticValue;
        }
        else {
            logger_1.logger.debug(`Push test failed: ${this.pushTest.name}`);
            return undefined;
        }
    }
}
exports.PushRule = PushRule;
function isPredicatedStaticValue(pushMapping) {
    const maybe = pushMapping;
    return maybe.test && maybe.value;
}
exports.isPredicatedStaticValue = isPredicatedStaticValue;
//# sourceMappingURL=PushRule.js.map