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:

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

    async function loadLibrary(modules){
        let loader;
        await System.import("dist/lib/loader").then((module) => {
           loader = module.Loader;
        });
        let library = await loader.loadModules(modules);
        return library;
    }

    loadLibrary([
        "dist/lib/base/api",
        "dist/lib/base/base-service",
        "dist/lib/config/config-service",
        "dist/lib/cms/wordpress",
        "dist/lib/ui/css",
        "dist/lib/util/dom-util",
        "dist/lib/util/util"
    ]).then((library) => {
         // call your library custom code here
         // example:
         let uuid = library.Util.getUUID();
    })

    */
    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;
                // if(typeof window !== 'undefined' && !window[namespace]) window[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() {
        // the only reliable means to get the global object is
        // `Function('return this')()`
        // However, this causes CSP violations in Chrome apps.
        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