"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.prefMatchNamespace = exports.PreferenceReader = void 0;
const util_1 = require("./util");
class PreferenceReader {
    constructor(db) {
        Object.defineProperty(this, "db", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: db
        });
    }
    async getPreferences(namespace, opts) {
        const prefsRes = await this.db.db
            .selectFrom('account_pref')
            .orderBy('id')
            .selectAll()
            .execute();
        const prefs = prefsRes
            .filter((pref) => !namespace || (0, exports.prefMatchNamespace)(namespace, pref.name))
            .map((pref) => JSON.parse(pref.valueJson));
        const personalDetailsPref = prefs.find((pref) => pref.$type === util_1.PERSONAL_DETAILS_PREF);
        if (personalDetailsPref) {
            if (typeof personalDetailsPref.birthDate === 'string') {
                const age = (0, util_1.getAgeFromDatestring)(personalDetailsPref.birthDate);
                const declaredAgePref = {
                    $type: util_1.DECLARED_AGE_PREF,
                    isOverAge13: age >= 13,
                    isOverAge16: age >= 16,
                    isOverAge18: age >= 18,
                };
                prefs.push(declaredAgePref);
            }
        }
        return prefs.filter((pref) => (0, util_1.prefAllowed)(pref.$type, opts));
    }
}
exports.PreferenceReader = PreferenceReader;
const prefMatchNamespace = (namespace, fullname) => {
    return fullname === namespace || fullname.startsWith(`${namespace}.`);
};
exports.prefMatchNamespace = prefMatchNamespace;
//# sourceMappingURL=reader.js.map