"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkAvailability = checkAvailability;
const getPolicyResponseCode_1 = require("./getPolicyResponseCode");
/**
 * @internal
 * Verify that the policy.availability requirement is met.
 * If a value is specified in the policy, the current context must be one of those values.
 * @param policy
 * @param context
 * @param entity
 * @returns
 */
function checkAvailability(policy, context, _entity) {
    let checks = [];
    if (policy.availability && policy.availability.length) {
        // Build array of the context's availability values
        // based on the isAlphaOrg and isBetaOrg properties
        const contextAvailability = [];
        if (context.isAlphaOrg) {
            contextAvailability.push("alpha");
        }
        if (context.isBetaOrg) {
            contextAvailability.push("beta");
        }
        // if we have no values in the array, then we push in "ga"
        if (!contextAvailability.length) {
            contextAvailability.push("general");
        }
        // reduce over the policy.availability array, adding a check for each
        // value that is not included in the contextAvailability array
        // the 'flag' is only used when we want to gate the feature behind
        // a feature flag, e.g. ?pe=hub:card:follow
        checks = policy.availability.reduce((acc, value) => {
            let result = "granted";
            if (!contextAvailability.includes(value)) {
                if (value === "flag") {
                    result = `feature-flag-required`;
                }
                else {
                    result = `not-${value}-org`;
                }
            }
            let name = `user in ${value} org`;
            if (value === "flag") {
                name = `with feature flag`;
            }
            const check = {
                name,
                value: contextAvailability.join(", "),
                code: (0, getPolicyResponseCode_1.getPolicyResponseCode)(result),
                response: result,
            };
            acc.push(check);
            return acc;
        }, []);
    }
    return checks;
}
//# sourceMappingURL=checkAvailability.js.map