"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkDelete = checkDelete;
const get_with_default_1 = require("../../objects/get-with-default");
const getPolicyResponseCode_1 = require("./getPolicyResponseCode");
/**
 * Validate entityDelete policy - delegates to the
 * entity's canDelete property
 * @param policy
 * @param context
 * @param entity
 * @returns
 */
function checkDelete(policy, context, entity) {
    const checks = [];
    if (Object.prototype.hasOwnProperty.call(policy, "entityDelete")) {
        let response = "granted";
        if (!entity) {
            // fail b/c no entity
            response = "entity-required";
        }
        else {
            if (policy.entityDelete && !entity.canDelete) {
                response = "no-delete-access";
            }
            else if (!policy.entityDelete && entity.canDelete) {
                response = "delete-access";
            }
        }
        const canDelete = (0, get_with_default_1.getWithDefault)(entity, "canDelete", false);
        // create the check
        const check = {
            name: "entity delete required",
            value: `entity.canDelete: ${canDelete.toString()}`,
            code: (0, getPolicyResponseCode_1.getPolicyResponseCode)(response),
            response,
        };
        checks.push(check);
    }
    return checks;
}
//# sourceMappingURL=checkDelete.js.map