"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkAssertions = checkAssertions;
const checkAssertion_1 = require("./checkAssertion");
/**
 * Validate entityOwner policy
 * @param policy
 * @param context
 * @param entity
 * @returns
 */
function checkAssertions(policy, context, entity) {
    let checks = [];
    // Only return a check if the policy is defined
    if (policy.assertions) {
        // iterate over the assertions, creating a check for each entry
        checks = policy.assertions.reduce((acc, assertion) => {
            var _a;
            let shouldCheckAssertion = true;
            // if conditions, check them first
            if ((_a = assertion.conditions) === null || _a === void 0 ? void 0 : _a.length) {
                shouldCheckAssertion = assertion.conditions.every((condition) => (0, checkAssertion_1.checkAssertion)(condition, entity, context).response === "granted");
            }
            // if we pass all conditions/there are no conditions, we evaluate the assertion
            // otherwise, the assertion is ignored
            if (shouldCheckAssertion) {
                const chk = (0, checkAssertion_1.checkAssertion)(assertion, entity, context);
                acc = [...acc, chk];
            }
            return acc;
        }, []);
    }
    return checks;
}
//# sourceMappingURL=checkAssertions.js.map