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 { Base } from './base/base';
export class Loader {
    /* System.js library loader - usage:

   SystemJS.config({
        baseURL: 'https://unpkg.com/@ema/js-base',
        packages: {
            'dist/lib': {
                defaultExtension: 'js'
            },
            'src/lib': {
                defaultExtension: 'ts'
            }
        }
    });
    System.import('loader').then(function(module) {
        let namespace = 'myNamespace';
        let  modules = [
            'cms/wordpress',
            'base/api',
            '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 loadModules(modules, namespace = '_ema') {
        return __awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => {
                let imports = [];
                let library = {};
                if (!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);
                });
            });
        });
    }
    // deprecated
    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;
                    // }
                });
                abl.base = new Base();
                if (options.namespace)
                    namespace = options.namespace;
                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