"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkReleaseGating = checkReleaseGating;
const getPolicyResponseCode_1 = require("./getPolicyResponseCode");
/**
 * @internal
 * Verify that the policy.releaseAfter and policy.retireAfter requirements are met.
 * These checks only apply in the "production" environment.
 * @param policy
 * @param context
 * @param entity
 * @returns
 */
function checkReleaseGating(policy, context, _entity) {
    const checks = [];
    // if neither value is specified, we return an empty array
    if (!policy.releaseAfter && !policy.retireAfter) {
        return checks;
    }
    // We only apply release gating in the production environment
    if (context.environment === "production") {
        const now = new Date();
        if (policy.releaseAfter) {
            const releaseDate = new Date(policy.releaseAfter);
            let result = "release-date-not-reached";
            if (now > releaseDate) {
                result = "granted";
            }
            const check = {
                name: `feature released`,
                value: policy.releaseAfter,
                code: (0, getPolicyResponseCode_1.getPolicyResponseCode)(result),
                response: result,
            };
            checks.push(check);
        }
        if (policy.retireAfter) {
            const retireDate = new Date(policy.retireAfter);
            let result = "retire-date-not-reached";
            if (now > retireDate) {
                result = "granted";
            }
            const check = {
                name: `feature retired`,
                value: policy.retireAfter,
                code: (0, getPolicyResponseCode_1.getPolicyResponseCode)(result),
                response: result,
            };
            checks.push(check);
        }
    }
    else {
        // if not in production, we always grant access
        const check = {
            name: `release gating only applies to production`,
            value: policy.releaseAfter,
            code: (0, getPolicyResponseCode_1.getPolicyResponseCode)("granted"),
            response: "granted",
        };
        checks.push(check);
    }
    return checks;
}
//# sourceMappingURL=checkReleaseGating.js.map