import * as constants from './constants';
import {isAutomationTestActive} from './testAutomation';

let config = {};
let initialized = false;

function setConfig(conf: any): void {
    config = conf; // IMPORTANT - config should be link to RC.Config, do not clone conf object!!!
    if (!initialized) {
        initialized = Object.keys(config).length > 0;
    }
}

function getConfig(): any {
    if (!initialized) {
        const message = 'getConfig used earlier than setConfig';
        if (isAutomationTestActive()) {
            setTimeout(() => alert(message));
        }
        throw new Error(message);
    }
    return config;
}

export function isConfigInitialized(): boolean {
    return initialized;
}

function testConfig(config) {
    setConfig(
        Object.assign(
            {
                brandId: constants.defaultBrandId,
                userLanguage: constants.defaultUserLanguage,
                brandDisplayName: constants.defaultBrandDisplayName,
                shortBrandName: constants.defaultShortBrandName
            },
            config || {}
        )
    );
}

export {setConfig, getConfig, testConfig};
