export class Loader {
    /* System.js library loader - usage:

    System.config({
          baseURL: 'https://unpkg.com/@ema/js-base',
          packages: {
              'dist': {
                  defaultExtension: 'js'
              },
              'src': {
                  defaultExtension: 'ts'
              }
          },
      });
    System.import('loader').then(function(module) {
        let namespace = 'myNamespace';
        let  modules = [
            'cms/wordpress',
            'base/api',
            'base/base',
            'base/browser',
            'base/server',
            'config/config',
            'config/config-service',
            'config/config-defaults',
            'ui/form',
            'util/storage',
            'util/util',
            'validation/validationConstants',
        ];
        module.Loader.loadLibrary({modules: modules, path:'global/',namespace:namespace,extension:''}).then(function(){
            let library = window['myNamespace'];
            console.log(window['myNamespace']);
            //
            // call your applications init function here.
            //
        });
    });
    */
    static loadLibrary(options) {
        let namespace = '_ema';
        let path = '';
        let abl = {};
        let imports = [];
        return new Promise(function (resolve, reject) {
            let extension = '';
            if (options.path) {
                path = options.path;
            }
            if (options.extension) {
                extension = options.extension;
            }
            options.modules.forEach((module) => {
                imports.push(System.import(options.path + module + extension));
            });
            Promise.all(imports).then(function (modules) {
                modules.forEach((module) => {
                    for (let C in module) {
                        abl[C] = module[C];
                    }
                    if (module.Base) {
                        abl.base = new module.Base;
                    }
                });
                if (options.namespace)
                    namespace = options.namespace;
                if (abl.base)
                    abl.base.initNameSpace(namespace);
                if (typeof window !== 'undefined' && window[namespace]) {
                    Object.keys(abl).forEach((k, i) => {
                        window[namespace][k] = abl[k];
                    });
                }
                // if (typeof window !== 'undefined') {
                //     if (!(<any>window)[namespace]) {
                //         (<any>window)[namespace] = abl;
                //     }
                //     if (options.namespace) {
                //         if (!(<any>window)[options.namespace]) {
                //             (<any>window)[options.namespace] = abl;
                //         }
                //     }
                // }
                resolve(abl);
            });
        });
    }
    ;
}
//# sourceMappingURL=loader.js.map