text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  var sp_telemetry_1 = require("@ms/sp-telemetry");
  var sp_core_library_1 = require("@microsoft/sp-core-library");
  var ManifestStore_1 = require("../stores/ManifestStore");
  var AddressStore_1 = require("../stores/AddressStore");
  var resolveAddress_1 = require("../utilities/resolveAddress");
  var normalizeName_1 = require("./normalizeName");
  var SPLoader_resx_1 = require("../SPLoader.resx");
  var telemetryConstants = require("../utilities/telemetryConstants");
  var SystemJsLoader = (function () {
      function SystemJsLoader() {
          this._configuredFailoverPaths = [];
      }
      Object.defineProperty(SystemJsLoader, "instance", {
          get: function () {
              if (!this._instance) {
                  this._instance = new SystemJsLoader();
                  this._instance._initialize();
              }
              return this._instance;
          },
          enumerable: true,
          configurable: true
      });
      SystemJsLoader.prototype.ensure = function (name, module) {
          var system = this._system;
          if (!system.has(name)) {
              system.set(name, system.newModule(module));
          }
      };
      SystemJsLoader.prototype.delete = function (name) {
          var system = this._system;
          if (system.has(name)) {
              system.delete(name);
          }
      };
      SystemJsLoader.prototype.systemConfig = function (config) {
          this._originalSystemConfig.call(this._system, config);
      };
      SystemJsLoader.prototype.systemImport = function (name) {
          try {
              return this._system.import(name);
          }
          catch (error) {
              sp_telemetry_1._TraceLogger.logError(telemetryConstants.loadComponentLogSource, error, SystemJsLoader.systemImportEventName);
              return Promise.reject(error);
          }
      };
      SystemJsLoader.prototype.systemDelete = function (manifest) {
          this._system.delete(normalizeName_1.default(manifest));
      };
      SystemJsLoader.prototype.configure = function (manifest) {
          var resources = manifest.loaderConfig.scriptResources;
          var depsMap = {};
          var globalDepsMap = {};
          for (var name_1 in resources) {
              if (resources[name_1].type === 'component') {
                  var moduleConfiguration = resources[name_1];
                  var resourceManifest = ManifestStore_1.default.instance.tryGetManifest(moduleConfiguration.id, moduleConfiguration.version);
                  if (resourceManifest) {
                      depsMap[name_1] = normalizeName_1.default(resourceManifest);
                  }
                  if (moduleConfiguration.failoverPath) {
                      if (this._configuredFailoverPaths.indexOf(name_1) === -1) {
                          var normalizedName = normalizeName_1.normalizeFailoverPathName(name_1);
                          AddressStore_1.default.instance.set(normalizedName, resolveAddress_1.default(manifest, name_1));
                          globalDepsMap[name_1] = normalizedName;
                          this._configuredFailoverPaths.push(name_1);
                      }
                  }
              }
              else {
                  var normalizedName = void 0;
                  if (name_1 === manifest.loaderConfig.entryModuleId) {
                      normalizedName = normalizeName_1.default(manifest);
                  }
                  else {
                      normalizedName = normalizeName_1.default(manifest, name_1);
                      depsMap[name_1] = normalizedName;
                  }
                  AddressStore_1.default.instance.set(normalizedName, resolveAddress_1.default(manifest, name_1));
                  var resource = resources[name_1];
                  if (resource && resource.globalName) {
                      var meta = {};
                      meta[normalizedName] = {
                          format: 'global',
                          exports: resource.globalName
                      };
                      var globalConfig = {
                          meta: meta
                      };
                      this.systemConfig(globalConfig);
                  }
              }
          }
          var packages = {};
          packages[normalizeName_1.default(manifest)] = {
              map: depsMap,
              defaultExtension: false
          };
          var config = {
              packages: packages
          };
          this.systemConfig(config);
          if (Object.keys(globalDepsMap).length) {
              this.systemConfig({
                  map: globalDepsMap
              });
          }
      };
      SystemJsLoader.prototype.getDependencies = function (manifest) {
          sp_core_library_1.Validate.isNotNullOrUndefined(manifest, 'manifest');
          var defined = this._system.defined; 
          var definedName = this.getDefinedId(manifest);
          var componentDefinition = defined[definedName];
          if (!componentDefinition) {
              return [];
          }
          return componentDefinition.deps;
      };
      SystemJsLoader.prototype._baseSystemConfig = function (pluginName, scriptLoad) {
          var systemConfig = {
              meta: {
                  '*': {
                      loader: pluginName,
                      scriptLoad: scriptLoad
                  }
              }
          };
          this.systemConfig(systemConfig);
      };
      SystemJsLoader.prototype._initialize = function () {
          this._system = this._loadSystemJs();
          this._setCustomLoader(SystemJsLoader.pluginName, this._system);
          var systemConfig = {
              baseURL: SystemJsLoader._invalidBaseUrl,
              defaultJsExtensions: false
          };
          this.systemConfig(systemConfig);
          this._baseSystemConfig(SystemJsLoader.pluginName, true);
      };
      SystemJsLoader.prototype._loadSystemJs = function () {
          var system = require('@microsoft/loader-raw-script!../../../../blobs/systemjs/0.19.25/dist/system.spfx');
          this._originalSystemConfig = system.config;
          system.config = function (config) {
              throw new Error(SPLoader_resx_1.default.systemConfigDisabledError);
          };
          return system;
      };
      SystemJsLoader.prototype._setCustomLoader = function (pluginName, system) {
          var loader = {
              locate: function (module) {
                  var address = AddressStore_1.default.instance.tryGetAddress(module.name);
                  if (address) {
                      return address;
                  }
                  else {
                      sp_telemetry_1._TraceLogger.logVerbose(telemetryConstants.loadComponentLogSource, "Address not found for module name: " + module.name + ". Using it as absolute URL.");
                      return module.name;
                  }
              }
          };
          system.set(pluginName, system.newModule(loader));
      };
      SystemJsLoader.prototype.getDefinedId = function (manifest) {
          return SystemJsLoader._invalidBaseUrl + manifest.id + '_' + manifest.version;
      };
      SystemJsLoader.pluginName = 'sp-loader-resolver';
      SystemJsLoader.systemImportEventName = 'System.import';
      SystemJsLoader._invalidBaseUrl = 'https://relative-path.invalid/';
      return SystemJsLoader;
  }());
  exports.default = SystemJsLoader;
  