var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { ConfigService } from '../config/config-service';
export class Base {
    constructor() {
        /*
        * Application base class: initializes global '_ema' namespace
        * */
        this._environment = { type: 'js' };
        this._config = ConfigService.instance;
    }
    init(config, globalNameSpace = undefined) {
        return __awaiter(this, void 0, void 0, function* () {
            if (globalNameSpace) {
                this.initNameSpace(globalNameSpace);
            }
            if (typeof config === 'string') {
                let configFetched = yield this._config.fetch(config, true);
                return configFetched;
            }
            else {
                this._config.add(config);
                return config;
            }
        });
    }
    initNameSpace(namespace) {
        this._config.set('_environment', this._environment);
        this._environment.namespace = namespace;
        // browser
        if (typeof window !== 'undefined') {
            this._environment.type = 'browser';
            this._environment.framework = 'js';
            if (window['angular']) {
                this._environment.framework = 'angularJs';
            }
            if (!window[namespace]) {
                window[namespace] = {
                    config: this._config,
                    environment: this._environment
                };
            }
        }
        // nodejs
        if (typeof window === 'undefined') {
            // todo: set global namespace
            this._environment.type = 'server';
            this._environment.framework = 'node';
        }
        this._environment.date = new Date();
        this._environment.started = Math.round(+new Date() / 1000);
    }
    fetchJson(url) {
        return __awaiter(this, void 0, void 0, function* () {
            const response = yield fetch(url);
            const json = yield response.json();
            return json;
        });
    }
    ;
    get config() {
        return this._config;
    }
}
//# sourceMappingURL=base.js.map