text: import { _QosMonitor } from '@microsoft/sp-diagnostics';
  import { Guid } from '@microsoft/sp-core-library';
  import AadConstants from './AadConstants';
  import { AadErrorHandler } from './AadErrorHandler';
  /**
   * Class that wraps the MSAL's authentication class
   * @internal
   */
  var MsalTokenProvider = /** @class */ (function () {
      function MsalTokenProvider(configuration) {
          this._defaultConfiguration = configuration;
      }
      MsalTokenProvider.prototype.getToken = function (resourceEndpoint, useCachedToken) {
          var _this = this;
          if (useCachedToken === void 0) { useCachedToken = true; }
          var acquireAccessTokenQosMonitor = new _QosMonitor('MsalTokenProvider.GetToken');
          var extraData = this._generateTelemetryExtraData();
          return this._loginSilent()
              .then(function () { return _this._acquireToken(resourceEndpoint); })
              .then(function (accessToken) {
              acquireAccessTokenQosMonitor.writeSuccess(extraData);
              return accessToken;
          })
              .catch(function (e) { return _this._handleAuthErrors(acquireAccessTokenQosMonitor, e, extraData); });
      };
      MsalTokenProvider.prototype._acquireToken = function (resourceEndpoint) {
          var _this = this;
          var acquireAccessTokenQosMonitor = new _QosMonitor('MsalTokenProvider.AcquireAccessTokenSilent');
          var extraData = this._generateTelemetryExtraData();
          if (!this._msalPromise) {
              this._msalPromise = this._loadMsalJs();
          }
          return this._msalPromise
              .then(function (msal) {
              // Adding ./default returns all of a resource's permission scopes
              return msal.acquireTokenSilent({
                  authority: 'https://login.microsoftonline.com/' + _this._defaultConfiguration.aadTenantId,
                  correlationId: extraData.CorrelationId,
                  forceRefresh: true,
                  loginHint: _this._defaultConfiguration.userPrincipalName,
                  scopes: [resourceEndpoint + '/.default']
              });
          })
              .then(function (response) {
              acquireAccessTokenQosMonitor.writeSuccess(extraData);
              return response.accessToken;
          })
              .catch(function (e) { return _this._handleAuthErrors(acquireAccessTokenQosMonitor, e, extraData); });
      };
      MsalTokenProvider.prototype._loginSilent = function () {
          var _this = this;
          var acquireIdTokenQosMonitor = new _QosMonitor('MsalTokenProvider.AcquireIdTokenSilent');
          var extraData = this._generateTelemetryExtraData();
          if (!this._msalPromise) {
              this._msalPromise = this._loadMsalJs();
          }
          return this._msalPromise
              .then(function (msal) {
              return msal.acquireTokenSilent({
                  authority: 'https://login.microsoftonline.com/' + _this._defaultConfiguration.aadTenantId,
                  correlationId: extraData.CorrelationId,
                  forceRefresh: true,
                  loginHint: _this._defaultConfiguration.userPrincipalName,
                  scopes: [_this._defaultConfiguration.servicePrincipalId]
              });
          })
              .then(function (response) {
              acquireIdTokenQosMonitor.writeSuccess(extraData);
              return response.accessToken;
          })
              .catch(function (e) { return _this._handleAuthErrors(acquireIdTokenQosMonitor, e, extraData); });
      };
      MsalTokenProvider.prototype._loadMsalJs = function () {
          var _this = this;
          var loadMsalQosMonitor = new _QosMonitor('MsalTokenProvider.LoadMsal');
          return import(/* webpackChunkName: 'sp-http-msal' */ './msal.min.js')
              .then(function (msal) {
              loadMsalQosMonitor.writeSuccess();
              return new msal.UserAgentApplication({
                  auth: {
                      clientId: _this._defaultConfiguration.servicePrincipalId,
                      navigateToLoginRequestUrl: false,
                      redirectUri: _this._defaultConfiguration.redirectUri
                  },
                  system: {
                      loadFrameTimeout: 10000,
                      navigateFrameWait: 0
                  }
              });
          })
              .catch(function (e) {
              loadMsalQosMonitor.writeUnexpectedFailure(e);
              throw e;
          });
      };
      MsalTokenProvider.prototype._generateTelemetryExtraData = function () {
          return {
              CorrelationId: Guid.newGuid().toString(),
              isInternal: this._defaultConfiguration.servicePrincipalId === AadConstants.PRE_AUTHORIZED_APP_PRINCIPAL_ID
          };
      };
      MsalTokenProvider.prototype._handleAuthErrors = function (monitor, error, extraData) {
          if (AadErrorHandler._doesAadErrorCodeExist(error.message, AadConstants.EXPECTED_AAD_ERRORS)) {
              monitor.writeExpectedFailure(error.message, error, extraData);
          }
          else {
              monitor.writeUnexpectedFailure(error.message, error, extraData);
          }
          throw error;
      };
      return MsalTokenProvider;
  }());
  export { MsalTokenProvider };
  //# sourceMappingURL=MsalTokenProvider.js.map