"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.AccessControlKeysHeadersProvider = exports.ACCESS_CONTROL_KEYS_HEADER = void 0;
const XHR_1 = require("../../icc-api/api/XHR");
exports.ACCESS_CONTROL_KEYS_HEADER = 'Icure-Access-Control-Keys';
/**
 * @internal this class is intended for internal use only and may be changed without notice.
 */
class AccessControlKeysHeadersProvider {
    constructor(exchangeDataManager) {
        this.exchangeDataManager = exchangeDataManager;
    }
    // This will be enough only as long as we don't use the entities sfks in the access control keys.
    /**
     * Add the access control keys headers to the provided initial headers, allowing access control on users with anonymous delegations.
     * @param initialHeaders the initial headers
     * @param entityType the type of entity which the user is attempting to retrieve.
     */
    addAccessControlKeysHeaders(initialHeaders, entityType) {
        return __awaiter(this, void 0, void 0, function* () {
            const newHeaders = yield this.getAccessControlKeysHeaders(entityType);
            if (newHeaders.length > 0)
                return [...initialHeaders, ...newHeaders];
            else
                return initialHeaders;
        });
    }
    getAccessControlKeysHeaders(entityType) {
        return __awaiter(this, void 0, void 0, function* () {
            const accessControlKeysValue = yield this.exchangeDataManager.getAccessControlKeysValue(entityType);
            if (accessControlKeysValue) {
                return [new XHR_1.XHR.Header(exports.ACCESS_CONTROL_KEYS_HEADER, accessControlKeysValue)];
            }
            else {
                return [];
            }
        });
    }
}
exports.AccessControlKeysHeadersProvider = AccessControlKeysHeadersProvider;
//# sourceMappingURL=AccessControlKeysHeadersProvider.js.map