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';
const singleton = Symbol();
const singletonEnforcer = Symbol();
export class BaseService {
    constructor(enforcer) {
        if (enforcer !== singletonEnforcer) {
            throw new Error('Cannot construct singleton');
        }
        this._configService = ConfigService.instance;
        this._translateService = TranslateService.instance;
        this._type = 'BaseService';
        this._environment = {
            global: this.getGlobal(),
            started: Math.round(+new Date() / 1000),
            type: 'js'
        };
    }
    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;
            }
        });
    }
    fetchJson(url) {
        return __awaiter(this, void 0, void 0, function* () {
            const response = yield fetch(url);
            const json = yield response.json();
            return json;
        });
    }
    ;
    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');
    }
    initGlobalNameSpace(namespace = '_ema') {
        this.config.set('globalNameSpace', namespace);
        this.getGlobal()[namespace] = this;
    }
    get config() {
        return this._configService;
    }
    get environment() {
        return this._environment;
    }
    get translation() {
        return this._translateService;
    }
    get translateService() {
        return this._translateService;
    }
    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