"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PersistenceManager = exports.NAMESPACES = void 0;
require("reflect-metadata");
const path_1 = require("path");
const tsyringe_1 = require("tsyringe");
const Authentication_1 = require("./Authentication");
const DestroySignal_1 = require("./DestroySignal");
const LoggerFactory_1 = require("./utils/LoggerFactory");
const ServerPersistence_1 = __importDefault(require("./utils/persistence/ServerPersistence"));
exports.NAMESPACES = {
    ENCRYPTION_KEYS: 'EncryptionKeys',
    LATEST_ENCRYPTION_KEY_IDS: 'LatestEncryptionKeyIds'
};
let PersistenceManager = class PersistenceManager {
    persistence;
    authentication;
    loggerFactory;
    /* eslint-disable indent */
    constructor(authentication, destroySignal, loggerFactory) {
        this.authentication = authentication;
        this.loggerFactory = loggerFactory;
        destroySignal.onDestroy.listen(() => {
            if (this.persistence !== undefined) {
                return this.persistence.close();
            }
        });
    }
    async ensureInitialized() {
        if (this.persistence === undefined) {
            this.persistence = await ServerPersistence_1.default.createInstance({
                loggerFactory: this.loggerFactory,
                clientId: await this.authentication.getAddress(),
                namespaces: Object.values(exports.NAMESPACES),
                migrationsPath: (0, path_1.join)(__dirname, 'encryption/migrations') // TODO move migrations to some generic place?
            });
        }
    }
    async getPersistence(namespace) {
        await this.ensureInitialized();
        return {
            get: (key) => {
                return this.persistence.get(key, namespace);
            },
            set: (key, value) => {
                return this.persistence.set(key, value, namespace);
            }
        };
    }
};
exports.PersistenceManager = PersistenceManager;
exports.PersistenceManager = PersistenceManager = __decorate([
    (0, tsyringe_1.scoped)(tsyringe_1.Lifecycle.ContainerScoped),
    __param(0, (0, tsyringe_1.inject)(Authentication_1.AuthenticationInjectionToken)),
    __metadata("design:paramtypes", [Object, DestroySignal_1.DestroySignal,
        LoggerFactory_1.LoggerFactory])
], PersistenceManager);
//# sourceMappingURL=PersistenceManager.js.map