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());
    });
};
export class Loader {
    /* System.js ES5 library loader: usage

    include the following lines in your index.html:

    <script src='https://unpkg.com/systemjs'></script>
    <script src='https://unpkg.com/@ema/js-base/dist/webcomponents.js'></script>
    <link rel='stylesheet' href='https://unpkg.com/@ema/js-base/dist/css/theme.css' />


    include the following code in your javascript:

   SystemJS.config({
        baseURL: 'https://unpkg.com/@ema/js-base',
        packages: {
            'dist/lib': {
                defaultExtension: 'js'
            },
            'rxjs': {
                defaultExtension: 'js'
            }
        },
        paths: {
            'rxjs': [
                'https://unpkg.com/rxjs'
            ]
        }
    });

    async loadLibrary(modules){
        let loader;
        await System.import("dist/lib/loader").then((module) => {
           loader = module.Loader;
        });
        return await loader.loadModules(modules, this.settings.namespace+'_library');
    }

    // define and load the modules you want to include, set to your own requirements
    let modules = [
        "dist/lib/base/base-service",
        "dist/lib/config/config-service",
        "dist/lib/config/translate/translateService",
        "dist/lib/cms/wordpress",
        "dist/lib/util/dom-util",
        "dist/lib/util/util"
    ];
    let library = await this.loadLibrary(modules)

    // then you can call your library custom code here. Examples:

    let uuid = library.Util.getUUID();
    let translateService = library.TranslateService.instance;
    let wordpress = new library.Wordpress();

    */
    static loadModules(modules, namespace = '_ema_lib') {
        return __awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => {
                let imports = [];
                let library = {};
                let global = Loader.getGlobal();
                if (!global[namespace])
                    global[namespace] = library;
                modules.forEach((module) => {
                    imports.push(System.import(module));
                });
                Promise.all(imports).then(function (modules) {
                    modules.forEach((module) => {
                        for (let C in module) {
                            library[C] = module[C];
                        }
                    });
                    resolve(library);
                });
            });
        });
    }
    static 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');
    }
    ;
}
//# sourceMappingURL=loader.js.map