"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessControlSecretUtils = void 0;
const EntityWithDelegationTypeName_1 = require("../utils/EntityWithDelegationTypeName");
const utils_1 = require("../utils");
const ACCESS_CONTROL_KEY_LENGTH_BYTES = 16;
/**
 * @internal this class is intended for internal use only and may be changed without notice.
 */
class AccessControlSecretUtils {
    constructor(primitives) {
        this.primitives = primitives;
    }
    /**
     * Size of the access control keys returned by this class.
     */
    get accessControlKeyLengthBytes() {
        return ACCESS_CONTROL_KEY_LENGTH_BYTES;
    }
    /**
     * Get the access control key to use for entities of the provided type and using the provided secret foreign key. The combination of secret foreign
     * keys and entity type ensures that unauthorised people will not be able to draw links between entities of different types of data or different
     * confidentiality levels.
     * These keys will be sent to the icure server for access control of data owners which require anonymous delegations.
     * @param accessControlSecret an access control secret
     * @param entityTypeName an entity type name
     */
    accessControlKeyFor(accessControlSecret, entityTypeName) {
        return __awaiter(this, void 0, void 0, function* () {
            return (yield this.primitives.sha256((0, utils_1.utf8_2ua)(accessControlSecret + entityTypeName))).slice(0, ACCESS_CONTROL_KEY_LENGTH_BYTES);
        });
    }
    allAccessControlKeyFor(accessControlSecret) {
        return __awaiter(this, void 0, void 0, function* () {
            const res = [];
            for (const entityTypeName of EntityWithDelegationTypeName_1.entityWithDelegationTypeNames) {
                res.push((yield this.primitives.sha256((0, utils_1.utf8_2ua)(accessControlSecret + entityTypeName))).slice(0, ACCESS_CONTROL_KEY_LENGTH_BYTES));
            }
            return res;
        });
    }
    /**
     * Get value to use as key in secure delegations for entities of the provided type with the provided secret foreign key. The combination of secret
     * foreign keys and entity type ensures that unauthorised people will not be able to draw links between entities of different types of data or
     * different confidentiality levels.
     * These keys will be used in the secure delegations map of security metadata.
     * @param accessControlSecret an access control secret
     * @param entityTypeName an entity type name
     */
    secureDelegationKeyFor(accessControlSecret, entityTypeName) {
        return __awaiter(this, void 0, void 0, function* () {
            return (0, utils_1.ua2hex)(yield this.primitives.sha256(yield this.accessControlKeyFor(accessControlSecret, entityTypeName)));
        });
    }
    allSecureDelegationKeysFor(accessControlSecret) {
        return __awaiter(this, void 0, void 0, function* () {
            const accessControlKeys = yield this.allAccessControlKeyFor(accessControlSecret);
            const res = [];
            for (const accessControlKey of accessControlKeys) {
                res.push((0, utils_1.ua2hex)(yield this.primitives.sha256(accessControlKey)));
            }
            return res;
        });
    }
    getEncodedAccessControlKeys(accessControlSecrets, entityTypeName) {
        return __awaiter(this, void 0, void 0, function* () {
            const fullBuffer = new Uint8Array(accessControlSecrets.length * this.accessControlKeyLengthBytes);
            for (let i = 0; i < accessControlSecrets.length; i++) {
                const accessControlSecret = accessControlSecrets[i];
                const key = yield this.accessControlKeyFor(accessControlSecret, entityTypeName);
                fullBuffer.set(new Uint8Array(key), i * this.accessControlKeyLengthBytes);
            }
            return (0, utils_1.ua2b64)(fullBuffer);
        });
    }
}
exports.AccessControlSecretUtils = AccessControlSecretUtils;
//# sourceMappingURL=AccessControlSecretUtils.js.map