"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.memoize = exports.anySatisfied = exports.allSatisfied = exports.or = exports.and = exports.not = void 0;
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const LruCache_1 = require("../../../api-helper/project/support/LruCache");
const Mapping_1 = require("../Mapping");
const PushTest_1 = require("../PushTest");
const pred = require("./predicateUtils");
/**
 * Return the opposite of this push test
 */
exports.not = pred.whenNot;
/**
 * Wrap all these PushTests or ProjectPredicates in a single PushTest
 * AND: Return true if all are satisfied
 */
exports.and = allSatisfied;
/**
 * Wrap all these PushTests or ProjectPredicates in a single PushTest
 * OR: Return true if any is satisfied
 */
exports.or = anySatisfied;
/**
 * Wrap all these PushTests or ProjectPredicates in a single PushTest
 * AND: Return true if all are satisfied
 * @param {PushTest} pushTests
 * @return {PushTest}
 */
function allSatisfied(...pushTests) {
    const asPushTests = pushTests.map(p => Mapping_1.isMapping(p) ? p : PushTest_1.predicatePushTest(p.name, p));
    return pred.all(asPushTests);
}
exports.allSatisfied = allSatisfied;
/**
 * Wrap all these PushTests or ProjectPredicates in a single PushTest
 * OR: Return true if any is satisfied
 * @param {PushTest} pushTests
 * @return {PushTest}
 */
function anySatisfied(...pushTests) {
    const asPushTests = pushTests.map(p => Mapping_1.isMapping(p) ? p : PushTest_1.predicatePushTest(p.name, p));
    return pred.any(asPushTests);
}
exports.anySatisfied = anySatisfied;
const pushTestResultMemory = new LruCache_1.LruCache(1000);
/**
 * Cache the PushTest results for this push. The results will be cached based
 * on the name of the pushTest and the ID of the push. Make sure your push test
 * has a unique name!
 * @param {PushTest} pt
 * @return {PushTest}
 */
function memoize(pt) {
    return {
        name: pt.name,
        mapping: async (pti) => {
            const key = ptCacheKey(pt, pti);
            let result = pushTestResultMemory.get(key);
            if (result === undefined) {
                result = await pt.mapping(pti);
                logger_1.logger.debug(`Evaluated push test [%s] to %s: cache stats=%j`, pt.name, result, pushTestResultMemory.stats);
                pushTestResultMemory.put(key, result);
            }
            else {
                logger_1.logger.debug("Push test [%s] result was cached: " + result);
            }
            return result;
        },
        structure: pt.structure,
    };
}
exports.memoize = memoize;
function ptCacheKey(pt, pti) {
    return pti.push.id + "_" + pt.name;
}
//# sourceMappingURL=pushTestUtils.js.map