text: import { ServiceKey, Validate, _SPFlight, _SPKillSwitch } from '@microsoft/sp-core-library';
  import { _LogSource, _TraceLogger } from '@microsoft/sp-diagnostics';
  import { _PageContextDataSource } from '@microsoft/sp-dynamic-data';
  import AzureActiveDirectoryInfo from './AzureActiveDirectoryInfo';
  import CultureInfo from './CultureInfo';
  import SPFeatureInfo from './SPFeatureInfo';
  import SPList from './SPList';
  import SPListItem from './SPListItem';
  import SPSite from './SPSite';
  import SPUser from './SPUser';
  import SPWeb from './SPWeb';
  import * as strings from './loc/Strings.resx';
  /**
   * The SharePoint page context object.
   *
   * @remarks
   * The page context provides standard definitions for common SharePoint objects
   * that need to be shared between the client-side application, web parts, and other
   * components.  Typically the data is fetched via REST queries when navigating to a
   * new page, but it can also be preloaded by the web server, or filled from a custom
   * application cache.
  
   * @public
   */
  var PageContext = /** @class */ (function () {
      function PageContext(serviceScope) {
          var _this = this;
          this._isInitialized = false;
          this._serviceScope = serviceScope;
          this._serviceScope.whenFinished(function () {
              // Create the page context data source:
              // Note that the Initialize function is called multiple times, and each time
              // we mark the datasource as having changed.
              _this._pageContextDataSource = new _PageContextDataSource(serviceScope);
              _this._pageContextDataSource.addPropertyHandler({
                  id: PageContext._pageContextInfoId,
                  dynamicDataFunctions: {
                      getPropertyDefinitions: _this._getPropertyDefinitions.bind(_this),
                      getPropertyValue: _this._getPropertyValue.bind(_this),
                      getAnnotatedPropertyValue: _this._getAnnotatedPropertyValue.bind(_this),
                      allowedEvents: function () {
                          return [];
                      }
                  }
              });
          });
          this._getPropertyValue = this._getPropertyValue.bind(this);
      }
      /**
       * Initializes the Page Context.
       * @internal
       */
      // tslint:disable-next-line:no-any
      PageContext.prototype.initialize = function (options, legacyPageContext) {
          var _this = this;
          Validate.isNotNullOrUndefined(options, 'options');
          try {
              this._aadInfo = options.aadInfo ? new AzureActiveDirectoryInfo(options.aadInfo) : undefined;
              this._cultureInfo = new CultureInfo(options.cultureInfo);
              this._list = options.list ? new SPList(options.list) : undefined;
              this._listItem = options.listItem ? new SPListItem(options.listItem) : undefined;
              this._site = new SPSite(options.site);
              this._user = new SPUser(options.user);
              this._web = new SPWeb(options.web);
              this._spFeatureInfo = new SPFeatureInfo(options.featureInfo);
              this._legacyPageContext = legacyPageContext;
          }
          catch (e) {
              _TraceLogger.logError(PageContext._logSource, e, 'initialize');
              throw e;
          }
          this._serviceScope.whenFinished(function () {
              // The datasource was initialized in the constructor because this initialize method is
              // called numerous times.
              _this._pageContextDataSource.notifyPropertyChanged(PageContext._pageContextUserDynamicProperty);
              _this._pageContextDataSource.notifyPropertyChanged(PageContext._pageContextSiteDataDynamicProperty);
              _this._pageContextDataSource.notifyPropertyChanged(PageContext._pageContextUserDynamicProperty);
          });
          this._isInitialized = true;
      };
      /**
       * Sets the search query value for the page context dynamic data.
       * Updating the value triggers a notification to dynamic data consumers for that property.
       *
       * @param searchQuery - Search query to set.
       * @internal
       */
      PageContext.prototype._setSearchQuery = function (searchQuery) {
          // If the value is the same, do not update. This avoids unnecessary callbacks for the same value.
          if (this._isSearchDynamicDataEnabled() && this._searchQuery !== searchQuery) {
              this._searchQuery = searchQuery;
              this._pageContextDataSource.notifyPropertyChanged(PageContext._pageContextSearchDataDynamicProperty);
          }
      };
      Object.defineProperty(PageContext.prototype, "aadInfo", {
          /**
           * Contextual information for communicating with Azure Active Directory.
           * If the current page doesn't have an associated Azure Active Directory tenant, this
           * property will be undefined.
           */
          get: function () {
              return this._aadInfo;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(PageContext.prototype, "cultureInfo", {
          /**
           * It provides culture info for the current user of the application.
           * This class is primarily used with the `PageContext` class.
           */
          get: function () {
              return this._cultureInfo;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(PageContext.prototype, "list", {
          /**
           * Contextual information for the SharePoint list that is hosting the page.
           * If there is no list associated to the current page, this property will be undefined.
           */
          get: function () {
              return this._list;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(PageContext.prototype, "listItem", {
          /**
           * Contextual information for the SharePoint list item that is hosting the page.
           * If there is no list item associated to the current page, this property will be undefined.
           */
          get: function () {
              return this._listItem;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(PageContext.prototype, "page", {
          /**
           * Exposes information that is associated with the SharePoint web page that is currently being displayed.
           * @alpha
           */
          get: function () {
              return this._page;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(PageContext.prototype, "site", {
          /**
           * Contextual information for the SharePoint site collection ("SPSite") that is hosting the page.
           */
          get: function () {
              return this._site;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(PageContext.prototype, "user", {
          /**
           * It provides contextual information for the SharePoint user that is accessing the page.
           * This class is primarily used with the `PageContext` class.
           */
          get: function () {
              return this._user;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(PageContext.prototype, "web", {
          /**
           * Contextual information for the SharePoint site ("SPWeb") that is hosting the page.
           */
          get: function () {
              return this._web;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(PageContext.prototype, "_featureInfo", {
          /**
           * Contextual information for the features that are enabled on the SharePoint site
           * that is hosting the page.
           * @internal
           */
          get: function () {
              return this._spFeatureInfo;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(PageContext.prototype, "legacyPageContext", {
          /**
           * An object providing classic SharePoint properties that may be required by certain legacy scripts.
           *
           * @remarks
           * This property is provided to facilitate migration of legacy code. It returns a JavaScript
           * object whose contents are similar to the `_spPageContextInfo` window variable from classic pages.
           * The contents of this variable may change in future releases of SharePoint.  For this reason,
           * new projects are encouraged to use the SharePoint Framework TypeScript APIs instead, since they
           * are fully documented and provide reliable backwards compatibility guarantees.
           *
           * NOTE: If certain functionality is exposed in the `legacyPageContext` but does not seem to have
           * a proper TypeScript API, please file an issue on GitHub.  The intention is that no modern application
           * should need to rely on the `legacyPageContext` object.
           */
          // tslint:disable-next-line:no-any
          get: function () {
              return this._legacyPageContext;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(PageContext.prototype, "isInitialized", {
          /**
           * Returns whether the `PageContext` has been initialized.
           */
          get: function () {
              return this._isInitialized;
          },
          enumerable: false,
          configurable: true
      });
      PageContext.prototype._getPropertyDefinitions = function () {
          var properties = [
              {
                  id: PageContext._pageContextSiteDataDynamicProperty,
                  title: strings.ddpSiteDataTitle
              },
              {
                  id: PageContext._pageContextUserDynamicProperty,
                  title: strings.ddpUserDataTitle
              },
              {
                  id: PageContext._pageContextUrlDataDynamicProperty,
                  title: strings.ddpUrlDataTitle
              }
          ];
          if (this._isSearchDynamicDataEnabled()) {
              properties.push({
                  id: PageContext._pageContextSearchDataDynamicProperty,
                  title: strings.ddpSearchDataTitle
              });
          }
          return properties;
      };
      // tslint:disable-next-line:no-any
      PageContext.prototype._getPropertyValue = function (propertyId) {
          switch (propertyId) {
              case PageContext._pageContextUserDynamicProperty:
                  return {
                      userName: this.user.displayName,
                      userEmail: this.user.email,
                      userLogin: this.user.loginName
                  };
              case PageContext._pageContextSiteDataDynamicProperty:
                  return {
                      siteUrl: this.web.absoluteUrl,
                      siteCollectionUrl: this.site.absoluteUrl,
                      listUrl: document &&
                          document.location &&
                          this.list &&
                          document.location.origin + this.list.serverRelativeUrl,
                      itemId: this.listItem && this.listItem.id,
                      siteClassification: this.site.classification,
                      siteTitle: this.web.title,
                      siteDescription: this.web.description,
                      siteLogoUrl: document && document.location && document.location.origin + this.web.logoUrl
                  };
              case PageContext._pageContextUrlDataDynamicProperty:
                  return {
                      queryParameters: this._getUrlQueryParameterCollection(),
                      fragment: this._getUrlFragmentIdentifier()
                  };
              case PageContext._pageContextSearchDataDynamicProperty:
                  return this._isSearchDynamicDataEnabled()
                      ? {
                          searchQuery: this._searchQuery
                      }
                      : undefined;
              default:
                  return undefined;
          }
      };
      PageContext.prototype._getAnnotatedPropertyValue = function (propertyId) {
          var propMetadata = {};
          switch (propertyId) {
              case PageContext._pageContextUserDynamicProperty:
                  propMetadata = {
                      userName: {
                          title: strings.ddvUserName,
                          description: strings.ddvUserNameDesc
                      },
                      userEmail: {
                          title: strings.ddvUserEmail,
                          description: strings.ddvUserEmailDesc
                      },
                      userLogin: {
                          title: strings.ddvUserLogin,
                          description: strings.ddvUserLoginDesc
                      }
                  };
                  break;
              case PageContext._pageContextSiteDataDynamicProperty:
                  propMetadata = {
                      siteUrl: {
                          title: strings.ddvSiteUrl,
                          description: strings.ddvSiteUrlDesc
                      },
                      siteCollectionUrl: {
                          title: strings.ddvSiteCollectionUrl,
                          description: strings.ddvSiteCollectionUrlDesc
                      },
                      listUrl: {
                          title: strings.ddvListUrl,
                          description: strings.ddvListUrlDesc
                      },
                      itemId: {
                          title: strings.ddvItemId,
                          description: strings.ddvItemIdDesc
                      },
                      siteClassification: {
                          title: strings.ddvSiteClassification,
                          description: strings.ddvSiteClassificationDesc
                      },
                      siteTitle: {
                          title: strings.ddvSiteTitle,
                          description: strings.ddvSiteTitleDesc
                      },
                      siteDescription: {
                          title: strings.ddvSiteDescription,
                          description: strings.ddvSiteDescriptionDesc
                      },
                      siteLogoUrl: {
                          title: strings.ddvSiteLogoUrl,
                          description: strings.ddvSiteLogoUrlDesc
                      }
                  };
                  break;
              case PageContext._pageContextUrlDataDynamicProperty:
                  propMetadata = {
                      queryParameters: {
                          title: strings.ddvQueryParameters,
                          description: strings.ddvQueryParametersDesc
                      },
                      fragment: {
                          title: strings.ddvFragment,
                          description: strings.ddvFragmentDesc
                      }
                  };
                  break;
              case PageContext._pageContextSearchDataDynamicProperty:
                  if (this._isSearchDynamicDataEnabled()) {
                      propMetadata = {
                          searchQuery: {
                              title: strings.ddvSearchQuery,
                              description: strings.ddvSearchQueryDesc
                          }
                      };
                  }
                  else {
                      propMetadata = {};
                  }
                  break;
          }
          return {
              sampleValue: this._getPropertyValue(propertyId),
              metadata: propMetadata
          };
      };
      /**
       * Returns decoded key-value pair collection for the current page's query parameters.
       *
       * @remarks
       * If the key or value doesn't exist, it adds an empty string instead.
       */
      PageContext.prototype._getUrlQueryParameterCollection = function () {
          var queryParamCollection = {};
          if (this._isQueryStringParamsKSActivated()) {
              var queryParams = this._getUrlParser().search.substring(1).split('&');
              for (var _i = 0, queryParams_1 = queryParams; _i < queryParams_1.length; _i++) {
                  var queryParam = queryParams_1[_i];
                  if (queryParam !== '') {
                      var params = queryParam.split('=');
                      queryParamCollection[decodeURIComponent(params[0])] = decodeURIComponent(params[1] || '');
                  }
              }
          }
          else {
              var queryParams = new URL(self.location.href).searchParams;
              queryParams.forEach(function (value, key) {
                  queryParamCollection[decodeURIComponent(key)] = decodeURIComponent(value || '');
              });
          }
          return queryParamCollection;
      };
      /**
       * Returns the fragment identifier for the current page's url.
       */
      PageContext.prototype._getUrlFragmentIdentifier = function () {
          if (this._isQueryStringParamsKSActivated()) {
              return this._getUrlParser().hash.substring(1); // removing '#' which is at the 0 index.
          }
          else {
              return new URL(self.location.href).hash.substring(1); // removing '#' which is at the 0 index.
          }
      };
      PageContext.prototype._getUrlParser = function () {
          this._urlParser = this._urlParser || document.createElement('a');
          this._urlParser.href = self.location.href;
          return this._urlParser;
      };
      PageContext.prototype._isSearchDynamicDataEnabled = function () {
          return _SPFlight.isEnabled(1626 /* CSISearchQueryExtension */);
      };
      PageContext.prototype._isQueryStringParamsKSActivated = function () {
          return _SPKillSwitch.isActivated('a7c756f2-39a2-4d56-b46e-d6fd3fb056c7' /* '12/02/2020', 'Use URLSearchParams to extract query string params'*/);
      };
      /**
       * The service key for PageContext.
       */
      PageContext.serviceKey = ServiceKey.create('sp-page-context:PageContext', PageContext);
      PageContext._logSource = _LogSource.create('PageContext');
      PageContext._pageContextInfoId = 'PageContextInfoProperties';
      PageContext._pageContextUserDynamicProperty = 'UserData';
      PageContext._pageContextSiteDataDynamicProperty = 'SiteData';
      PageContext._pageContextUrlDataDynamicProperty = 'UrlData';
      PageContext._pageContextSearchDataDynamicProperty = 'SearchData';
      return PageContext;
  }());
  export default PageContext;
  //# sourceMappingURL=PageContext.js.map