"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadConfig = exports.getDefaultConfig = exports.getConfig = void 0;
const dirs = require("../lib/dirs");
const fs = require("fs");
const helpers = require("../lib/helpers");
const yaml = require("js-yaml");
const path = require("path");
let currentConfig = getDefaultConfig();
/** Returns the currently active configuration. */
function getConfig() {
    return currentConfig;
}
exports.getConfig = getConfig;
/** Returns the default configuration. */
function getDefaultConfig() {
    return {
        acquireActorsTimeoutSec: 1200,
        actorGroups: [],
        adminLogin: null,
        adminPassword: null,
        adminPasswordBcrypt: null,
        cleanupSessionsIntervalSec: 10,
        dbCompactionIntervalSec: 5 * 60,
        noActivityTimeoutSec: 3600,
        readOnlyPort: null,
        responseMaxSize: 3 * 1024 * 1024,
        serverPort: parseInt(process.env.PORT) || 3000,
        sessionHistoryMaxCount: 300,
        sessionHistoryMinCount: 50,
        sessionHistoryAgeDays: 60,
        setupTestPrefix: '(setup)',
        teardownTestPrefix: '(teardown)',
        testRepoDir: null,
        urlPrefix: '',
        watchTestRepo: true
    };
}
exports.getDefaultConfig = getDefaultConfig;
/** Loads the configuration from an absolute or relative path. */
function loadConfig(config) {
    currentConfig = getDefaultConfig();
    if (typeof config === 'string') {
        // Load configuration from the specified config file
        let loadedConfig = loadFromFile(config);
        Object.assign(currentConfig, loadedConfig);
    }
    else {
        // Load configuration from an object that was passed in
        Object.assign(currentConfig, config);
    }
    // CLEAN-UP CONFIG
    // Make sure the URL base path is valid
    currentConfig.urlPrefix = helpers.trimRight(currentConfig.urlPrefix, '/ ');
}
exports.loadConfig = loadConfig;
function loadFromFile(configFile) {
    let config = getDefaultConfig();
    let configFilePath;
    if (path.isAbsolute(configFile)) {
        configFilePath = configFile;
    }
    else {
        configFilePath = path.normalize(path.join(dirs.workingDir(), configFile));
    }
    // Override defaults values with the ones from the config file
    if (helpers.fileExists(configFilePath)) {
        let configFile = yaml.safeLoad(fs.readFileSync(configFilePath, 'utf8'));
        for (let prop in configFile) {
            if (configFile.hasOwnProperty(prop)) {
                if (config.hasOwnProperty(prop)) {
                    config[prop] = configFile[prop];
                }
                else {
                    if (prop === 'testRepoLocation' || prop === 'testRepoDirName') {
                        console.log(helpers.format('ERROR: Configuration file "{0}" uses parameters "testRepoLocation" and/or "testRepoDirName". Please remove them and use the "testRepoDir" parameter instead. See the "config.sample.yaml" file for sample usage.', configFilePath, prop));
                        process.exit(1);
                    }
                    else {
                        console.log(helpers.format('WARN: Configuration file {0} contains parameter "{1}", which doesn\'t appear to be a valid parameter name.', configFilePath, prop));
                    }
                }
            }
        }
    }
    else {
        throw new Error(helpers.format('Config file "{0}" doesn\'t exist', configFilePath));
    }
    return config;
}
//# sourceMappingURL=config-loader.js.map