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';
import { configDefaults } from '../config/config-defaults';
import { TranslateService } from '../translate/translate-service';
// import { observable } from '../util/observable';
const singleton = Symbol();
const singletonEnforcer = Symbol();
export class BaseService {
    constructor(enforcer) {
        /*
        * js-base application singleton service
        * */
        this._environment = { type: 'node' };
        if (enforcer !== singletonEnforcer) {
            throw new Error('Cannot construct singleton');
        }
        this._configService = ConfigService.instance;
        this._translateService = TranslateService.instance;
        this._global = this.getGlobal();
        // this._observable = observable;
        this._type = 'BaseService';
    }
    initApp(config, useDefaults = false, namespace = undefined) {
        return __awaiter(this, void 0, void 0, function* () {
            if (namespace) {
                this.initGlobalNameSpace(namespace);
            }
            if (useDefaults) {
                this.config.setDefaults(configDefaults);
            }
            if (typeof config === 'string') {
                let configFetched = yield this.config.fetch(config, true);
                return configFetched;
            }
            if (typeof config === 'object') {
                this.config.add(config);
                return config;
            }
        });
    }
    initGlobalNameSpace(namespace = '_ema') {
        // this._global = this.getGlobal();
        this.config.set('globalNameSpace', namespace);
        this._environment.namespace = namespace;
        this._environment.date = new Date();
        this._environment.started = Math.round(+new Date() / 1000);
        this._environment.framework = 'js';
        this.getGlobal()[namespace] = this;
        // browser
        if (typeof window !== 'undefined') {
            this._environment.type = 'browser';
            if (window['angular']) {
                this._environment.framework = 'angularJs';
            }
        }
        // todo: check for native or webworkers
    }
    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._configService;
    }
    get environment() {
        return this._environment;
    }
    getGlobal() {
        if (typeof self !== 'undefined') {
            return self;
        }
        if (typeof window !== 'undefined') {
            return window;
        }
        if (typeof global !== 'undefined') {
            return global;
        }
        throw new Error('unable to locate global object');
    }
    get translation() {
        return this._translateService;
    }
    // get observable() {
    //     return this._observable;
    // }
    static get instance() {
        if (!this[singleton]) {
            this[singleton] = new BaseService(singletonEnforcer);
        }
        return this[singleton];
    }
    get type() {
        return this._type;
    }
    set type(value) {
        this._type = value;
    }
}
//# sourceMappingURL=base-service.js.map