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