"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkOwner = checkOwner;
const getPolicyResponseCode_1 = require("./getPolicyResponseCode");
/**
 * Validate entityOwner policy
 * @param policy
 * @param context
 * @param entity
 * @returns
 */
function checkOwner(policy, context, entity) {
    var _a;
    const checks = [];
    // Only return a check if the policy is defined
    if (policy.entityOwner) {
        let response = "granted";
        let name = "entity owner required";
        if (!context.isAuthenticated) {
            response = "not-authenticated";
        }
        else if (!entity) {
            // fail b/c no entity
            response = "entity-required";
        }
        else {
            name = `entity owner required: ${entity.owner}`;
            if (entity.owner !== context.currentUser.username) {
                response = "not-owner";
            }
        }
        // create the check
        const check = {
            name,
            value: `current user: ${(_a = context.currentUser) === null || _a === void 0 ? void 0 : _a.username}`,
            code: (0, getPolicyResponseCode_1.getPolicyResponseCode)(response),
            response,
        };
        checks.push(check);
    }
    return checks;
}
//# sourceMappingURL=checkOwner.js.map