"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const pubSubEventService_1 = require("./pubSubEventService");
/**
 * get and set global Arc.js settings
 */
class ConfigService {
    static get(setting) {
        const parts = setting.split(".");
        let result;
        if (parts.length) {
            result = ConfigService.data;
            parts.forEach((part) => {
                result = result[part];
            });
        }
        return result;
    }
    static set(setting, value) {
        const parts = setting.split(".");
        const count = parts.length - 1;
        let section = ConfigService.data;
        if (count > 0) {
            for (let i = 0; i < count; ++i) {
                section = section[parts[i]];
            }
        }
        section[parts[count]] = value;
        pubSubEventService_1.PubSubEventService.publish(`ConfigService.settingChanged.${setting}`, value);
    }
    constructor() {
        if (!ConfigService.instance) {
            const defaults = require("../config/default.json");
            const prefix = "arcjs_";
            if (process && process.env) {
                Object.keys(process.env).forEach((key) => {
                    if (key.startsWith(prefix)) {
                        const internalKey = key.replace(prefix, "");
                        if (defaults.hasOwnProperty(internalKey)) {
                            defaults[internalKey] = process.env[key];
                        }
                    }
                });
            }
            ConfigService.data = defaults;
            ConfigService.instance = this;
        }
        return ConfigService.instance;
    }
    get(setting) {
        return ConfigService.instance.get(setting);
    }
    set(setting, value) {
        ConfigService.instance.set(setting, value);
    }
}
exports.ConfigService = ConfigService;
/**
 * This will automagically create a static instance of ConfigService that will be used whenever
 * someone imports ConfigService.
 */
Object.freeze(new ConfigService());
//# sourceMappingURL=configService.js.map