"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.ExchangeDataMapManager = void 0;
const lru_temporised_async_cache_1 = require("../utils/lru-temporised-async-cache");
const ExchangeDataMapCreationBatch_1 = require("../../icc-api/model/ExchangeDataMapCreationBatch");
/**
 * @internal this class is intended for internal use only and may be modified without notice
 * This class manages the Exchange Data Map entities, caching with an LRU cache on retrieval.
 */
class ExchangeDataMapManager {
    constructor(api) {
        this.api = api;
        this.exchangeDataMapCache = new lru_temporised_async_cache_1.LruTemporisedAsyncCache(1000, () => -1);
    }
    /**
     * This function creates a batch of Exchange Data Map, ignoring the one that already exist and are already present in the cache.
     * The ones that are not are created and their ids are cached.
     * @param batch a map where each key is the hex-encoded access control key to another map that associates the encoded id of an
     * Exchange Data entity to the fingerprint of the key used to encrypt it.
     */
    createExchangeDataMaps(batch) {
        return __awaiter(this, void 0, void 0, function* () {
            const entriesToCreate = {};
            for (const [k, v] of Object.entries(batch)) {
                if (!(yield this.exchangeDataMapCache.getIfCachedJob(k))) {
                    entriesToCreate[k] = v;
                }
            }
            if (Object.keys(entriesToCreate).length > 0) {
                yield this.api.createExchangeDataMapBatch(new ExchangeDataMapCreationBatch_1.ExchangeDataMapCreationBatch({ batch: entriesToCreate }));
                yield Promise.all(Object.keys(entriesToCreate).map((entry) => __awaiter(this, void 0, void 0, function* () {
                    yield this.exchangeDataMapCache.get(entry, () => Promise.resolve({ item: null }));
                })));
            }
        });
    }
    /**
     * Retrieves a batch of Exchange Data Maps.
     * @param accessControlKeyHashes the hex-encoded hashes of the Exchange Data Maps to retrieve.
     */
    getExchangeDataMapBatch(accessControlKeyHashes) {
        return __awaiter(this, void 0, void 0, function* () {
            const exchangeDataMaps = yield this.api.getExchangeDataMapByBatch(accessControlKeyHashes);
            yield Promise.all(exchangeDataMaps.map((entry) => __awaiter(this, void 0, void 0, function* () {
                yield this.exchangeDataMapCache.get(entry.id, () => Promise.resolve({ item: null }));
            })));
            return exchangeDataMaps;
        });
    }
}
exports.ExchangeDataMapManager = ExchangeDataMapManager;
//# sourceMappingURL=ExchangeDataMapManager.js.map