"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const minimist = require("minimist");
const dsconnect_1 = require("./dsconnect");
class ActiveOptions {
    static init() {
        ActiveOptions.argv = minimist(process.argv.slice(2));
    }
    static parseConfig() {
        ActiveOptions.config = JSON.parse(fs.readFileSync(ActiveOptions.get("config", "./config.json"), "utf8"));
        ActiveOptions.config.__filename = ActiveOptions.get("config", "./config.json");
        global.argv = ActiveOptions.argv;
        global.config = ActiveOptions.config;
    }
    static extendConfig(automerge = true) {
        return new Promise((resolve, reject) => {
            let tmpDb = new dsconnect_1.ActiveDSConnect(ActiveOptions.get("db", {}).url +
                "/" +
                ActiveOptions.get("db", {}).database);
            let network = ActiveOptions.get("network", "");
            network = network.substr(0, network.indexOf("@"));
            tmpDb
                .get(network)
                .then((config) => {
                if (automerge) {
                    if (config._rev)
                        ActiveOptions.set("network", config._id + "@" + config._rev);
                    if (config.security)
                        ActiveOptions.set("security", config.security);
                    if (config.consensus)
                        ActiveOptions.set("consensus", config.consensus);
                    if (config.neighbourhood)
                        ActiveOptions.set("neighbourhood", config.neighbourhood);
                    resolve(config);
                }
                else {
                    resolve(config);
                }
            })
                .catch(() => {
                resolve(false);
            });
        });
    }
    static get(name, defValue = null) {
        return ActiveOptions.config[name] || ActiveOptions.argv[name] || defValue;
    }
    static set(name, value, reload = false) {
        ActiveOptions.config[name] = value;
        if (reload) {
        }
    }
    static fetch(argv) {
        if (argv) {
            return ActiveOptions.argv;
        }
        else {
            return ActiveOptions.config;
        }
    }
}
ActiveOptions.argv = { _: [] };
ActiveOptions.config = {};
exports.ActiveOptions = ActiveOptions;
//# sourceMappingURL=options.js.map