text: "use strict";
  /*
   * Copyright (c) Microsoft Corporation. All rights reserved.
   * Licensed under the MIT License.
   */
  Object.defineProperty(exports, "__esModule", { value: true });
  var ClientConfigurationError_1 = require("./error/ClientConfigurationError");
  var Constants_1 = require("./utils/Constants");
  var ScopeSet = /** @class */ (function () {
      function ScopeSet() {
      }
      /**
       * Check if there are dup scopes in a given request
       *
       * @param cachedScopes
       * @param scopes
       */
      // TODO: Rename this, intersecting scopes isn't a great name for duplicate checker
      ScopeSet.isIntersectingScopes = function (cachedScopes, scopes) {
          cachedScopes = this.convertToLowerCase(cachedScopes);
          for (var i = 0; i < scopes.length; i++) {
              if (cachedScopes.indexOf(scopes[i].toLowerCase()) > -1) {
                  return true;
              }
          }
          return false;
      };
      /**
       * Check if a given scope is present in the request
       *
       * @param cachedScopes
       * @param scopes
       */
      ScopeSet.containsScope = function (cachedScopes, scopes) {
          cachedScopes = this.convertToLowerCase(cachedScopes);
          return scopes.every(function (value) { return cachedScopes.indexOf(value.toString().toLowerCase()) >= 0; });
      };
      /**
       * toLower
       *
       * @param scopes
       */
      // TODO: Rename this, too generic name for a function that only deals with scopes
      ScopeSet.convertToLowerCase = function (scopes) {
          return scopes.map(function (scope) { return scope.toLowerCase(); });
      };
      /**
       * remove one element from a scope array
       *
       * @param scopes
       * @param scope
       */
      // TODO: Rename this, too generic name for a function that only deals with scopes
      ScopeSet.removeElement = function (scopes, scope) {
          return scopes.filter(function (value) { return value !== scope; });
      };
      /**
       * Parse the scopes into a formatted scopeList
       * @param scopes
       */
      ScopeSet.parseScope = function (scopes) {
          var scopeList = "";
          if (scopes) {
              for (var i = 0; i < scopes.length; ++i) {
                  scopeList += (i !== scopes.length - 1) ? scopes[i] + " " : scopes[i];
              }
          }
          return scopeList;
      };
      /**
       * @hidden
       *
       * Used to validate the scopes input parameter requested  by the developer.
       * @param {Array<string>} scopes - Developer requested permissions. Not all scopes are guaranteed to be included in the access token returned.
       * @param {boolean} scopesRequired - Boolean indicating whether the scopes array is required or not
       * @ignore
       */
      ScopeSet.validateInputScope = function (scopes, scopesRequired, clientId) {
          if (!scopes) {
              if (scopesRequired) {
                  throw ClientConfigurationError_1.ClientConfigurationError.createScopesRequiredError(scopes);
              }
              else {
                  return;
              }
          }
          // Check that scopes is an array object (also throws error if scopes == null)
          if (!Array.isArray(scopes)) {
              throw ClientConfigurationError_1.ClientConfigurationError.createScopesNonArrayError(scopes);
          }
          // Check that scopes is not an empty array
          if (scopes.length < 1) {
              throw ClientConfigurationError_1.ClientConfigurationError.createEmptyScopesArrayError(scopes.toString());
          }
          // Check that clientId is passed as single scope
          if (scopes.indexOf(clientId) > -1) {
              if (scopes.length > 1) {
                  throw ClientConfigurationError_1.ClientConfigurationError.createClientIdSingleScopeError(scopes.toString());
              }
          }
      };
      /**
       * @hidden
       *
       * Extracts scope value from the state sent with the authentication request.
       * @param {string} state
       * @returns {string} scope.
       * @ignore
       */
      ScopeSet.getScopeFromState = function (state) {
          if (state) {
              var splitIndex = state.indexOf(Constants_1.Constants.resourceDelimiter);
              if (splitIndex > -1 && splitIndex + 1 < state.length) {
                  return state.substring(splitIndex + 1);
              }
          }
          return "";
      };
      /**
       * @ignore
       * Appends extraScopesToConsent if passed
       * @param {@link AuthenticationParameters}
       */
      ScopeSet.appendScopes = function (reqScopes, reqExtraScopesToConsent) {
          if (reqScopes) {
              return reqExtraScopesToConsent ? reqScopes.concat(reqExtraScopesToConsent) : reqScopes;
          }
          return null;
      };
      return ScopeSet;
  }());
  exports.ScopeSet = ScopeSet;
  //# sourceMappingURL=ScopeSet.js.map