function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
import { get as lodashGet, pick } from 'lodash';
import { buildSettings } from '..';
import { settingsCache } from '../cache';
import { globalSettings } from '../schema/global';
import { facilitySettings } from '../schema/facility';
import { centralSettings } from '../schema/central';
const allSchemas = [
    globalSettings,
    facilitySettings,
    centralSettings
];
const getSchemasForSettingsContext = (facilityId)=>facilityId ? [
        facilitySettings,
        globalSettings
    ] : [
        centralSettings,
        globalSettings
    ];
// Recursively walks the schema tree collecting keys that have the given flag set.
// When a node has the flag, its full dot-notated path is included and children are
// skipped (the whole subtree is exposed). When a node is a nested schema without the
// flag, we recurse into it to check its children (e.g. security.mobile).
const extractExposedKeys = (schema, flag, prefix = '')=>{
    const keys = [];
    for (const [key, value] of Object.entries(schema.properties)){
        const fullKey = prefix ? `${prefix}.${key}` : key;
        if (value[flag]) {
            keys.push(fullKey);
        } else if ('properties' in value) {
            keys.push(...extractExposedKeys(value, flag, fullKey));
        }
    }
    return keys;
};
export const getKeysByFlag = (flag, schemas = allSchemas)=>schemas.flatMap((schema)=>extractExposedKeys(schema, flag));
export class ReadSettings {
    async get(key) {
        const settings = await this.getAll();
        return lodashGet(settings, key);
    }
    // This is what is called on tamanu-web login. This gets only settings relevant to
    // the frontend so only what is needed is sent. No sensitive data is sent.
    // Settings are extracted from the schemas that apply to this reader's context.
    async getFrontEndSettings() {
        const allSettings = await this.getAll();
        return pick(allSettings, getKeysByFlag('exposedToWeb', getSchemasForSettingsContext(this.facilityId)));
    }
    async getPatientPortalSettings() {
        const allSettings = await this.getAll();
        return pick(allSettings, getKeysByFlag('exposedToPatientPortal', getSchemasForSettingsContext(this.facilityId)));
    }
    async getAll() {
        let settings = settingsCache.getAllSettings(this.facilityId);
        if (!settings) {
            settings = await buildSettings(this.models, this.facilityId);
            settingsCache.setAllSettings(settings, this.facilityId);
        }
        return settings;
    }
    constructor(models, facilityId){
        _define_property(this, "models", void 0);
        _define_property(this, "facilityId", void 0);
        this.models = models;
        this.facilityId = facilityId;
    }
}

//# sourceMappingURL=ReadSettings.js.map