"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseAccountStore = exports.ACCOUNT_STORAGE_DESCRIPTION = void 0;
const Initializer_1 = require("../../../../init/Initializer");
const LogUtil_1 = require("../../../../logging/LogUtil");
const ErrorUtil_1 = require("../../../../util/errors/ErrorUtil");
const InternalServerError_1 = require("../../../../util/errors/InternalServerError");
const AccountStore_1 = require("./AccountStore");
const LoginStorage_1 = require("./LoginStorage");
exports.ACCOUNT_STORAGE_DESCRIPTION = {
    [AccountStore_1.ACCOUNT_SETTINGS_REMEMBER_LOGIN]: 'boolean?',
};
/**
 * A {@link AccountStore} that uses an {@link AccountLoginStorage} to keep track of the accounts.
 * Needs to be initialized before it can be used.
 */
class BaseAccountStore extends Initializer_1.Initializer {
    logger = (0, LogUtil_1.getLoggerFor)(this);
    storage;
    initialized = false;
    constructor(storage) {
        super();
        this.storage = storage;
    }
    // Initialize the type definitions
    async handle() {
        if (this.initialized) {
            return;
        }
        try {
            await this.storage.defineType(LoginStorage_1.ACCOUNT_TYPE, exports.ACCOUNT_STORAGE_DESCRIPTION, false);
            this.initialized = true;
        }
        catch (cause) {
            throw new InternalServerError_1.InternalServerError(`Error defining account in storage: ${(0, ErrorUtil_1.createErrorMessage)(cause)}`, { cause });
        }
    }
    async create() {
        const { id } = await this.storage.create(LoginStorage_1.ACCOUNT_TYPE, {});
        this.logger.debug(`Created new account ${id}`);
        return id;
    }
    async getSetting(id, setting) {
        const account = await this.storage.get(LoginStorage_1.ACCOUNT_TYPE, id);
        if (!account) {
            return;
        }
        const { id: unused, ...settings } = account;
        return settings[setting];
    }
    async updateSetting(id, setting, value) {
        await this.storage.setField(LoginStorage_1.ACCOUNT_TYPE, id, setting, value);
    }
}
exports.BaseAccountStore = BaseAccountStore;
//# sourceMappingURL=BaseAccountStore.js.map