declare var _: any;
declare var System: any;
declare var Promise: any;

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: any = {};
        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 (!(<any>window)[namespace]) {
                        (<any>window)[namespace] = abl;
                    }
                    if (options.namespace) {
                        if (!(<any>window)[options.namespace]) {
                            (<any>window)[options.namespace] = abl;
                        }
                    }
                }
                resolve(abl);
            });
        });
    };

    // deprecated:

    /* usage:
    System.config({
          baseURL: 'https://app-base-library.firebaseapp.com/dist',
          packages: {
              'library': {
                  defaultExtension: 'js'
              }
          },
      });
    System.import('loader').then(function(module) {
        let namespace = 'myNamespace';
        module.Loader.load({path:'shared/',namespace:namespace,exclude:['Server','Permissions'],extension:''}).then(function(){
            console.log(window['myNamespace']);
            // call your applications init function here.
        });
    });
    */


    // static load(options) {
    //     const namespace = '_abl';
    //     return new Promise(function(resolve, reject) {
    //         let extension = '';
    //         if (options.extension) {
    //             extension = options.extension;
    //         }
    //         if (options.path.match('shared/')) {

    //             const abl: any = {};

    //             System.import(options.path + 'classMap' + extension).then(function (mapping) {
    //                 const imports = [];
    //                 let classPath;
    //                 _.each(mapping, function(map){
    //                 // _.each(options.mapping, function(map){
    //                     let exclude = false;
    //                     _.each(map, function(v, k){
    //                         classPath = v;
    //                         if (options.exclude && options.exclude.length) {
    //                             _.each(options.exclude, function(className){
    //                                 if (className === k) {
    //                                     exclude = true;
    //                                 }
    //                             });
    //                         }
    //                         if (!exclude) {
    //                             imports.push(System.import(options.path + classPath + extension));
    //                         }
    //                         exclude = false;
    //                     });
    //                 });
    //                 Promise.all(imports).then(function(modules) {
    //                     _.each(modules, function(module){
    //                         if (module.Base) {
    //                             abl.base = new module.Base;
    //                         }
    //                     });
    //                     _.each(modules, function(module){
    //                         _.each(module, function(v, k){
    //                             abl[k] = v;
    //                         });
    //                     });
    //                     if (typeof window !== 'undefined') {
    //                         (<any>window)[namespace] = abl;
    //                         if (options.namespace) {
    //                             (<any>window)[options.namespace] = abl;
    //                         }
    //                     }
    //                     resolve(abl);
    //                 });
    //             });

    //         }

    //     });
    // };

}

