var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
    return new Promise(function (resolve, reject) {
        generator = generator.call(thisArg, _arguments);
        function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
        function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
        function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
        function step(verb, value) {
            var result = generator[verb](value);
            result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
        }
        step("next", void 0);
    });
};
export class Loader {
    /* usage:

    System.config({
          baseURL: 'https://app-base-library.firebaseapp.com',
          packages: {
              'dist': {
                  defaultExtension: 'js'
              },
              'src': {
                  defaultExtension: 'ts'
              }
          },
      });
    System.import('loader').then(function(module) {
        let namespace = 'myNamespace';
        let  modules = [
            'cms/wordpress',
            'api',
            'base',
            'browser',
            'server',
            'user',
            'ui/form',
            'util/storage',
            'util/util',
            'validation/validationConstants',
        ];
        module.Loader.loadLibrary({modules: modules, path:'shared/',namespace:namespace,extension:''}).then(function(){
            let library = window['myNamespace'];
            console.log(window['myNamespace']);
            //
            // call your applications init function here.
            //
        });
    });
    */
    static loadLibrary(options) {
        const namespace = '_ab';
        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 (typeof window !== 'undefined') {
                    if (!window[namespace]) {
                        window[namespace] = abl;
                    }
                    if (options.namespace) {
                        if (!window[options.namespace]) {
                            window[options.namespace] = abl;
                        }
                    }
                }
                resolve(abl);
            });
        });
    }
    ;
}
//# sourceMappingURL=loader.js.map