text: // Copyright (c) Microsoft Corporation.
  // Licensed under the MIT License.
  import { __assign } from "tslib";
  import { URLBuilder, URLQuery } from "../url";
  var RedactedString = "REDACTED";
  var defaultAllowedHeaderNames = [
      "x-ms-client-request-id",
      "x-ms-return-client-request-id",
      "x-ms-useragent",
      "x-ms-correlation-request-id",
      "x-ms-request-id",
      "client-request-id",
      "return-client-request-id",
      "traceparent",
      "Access-Control-Allow-Credentials",
      "Access-Control-Allow-Headers",
      "Access-Control-Allow-Methods",
      "Access-Control-Allow-Origin",
      "Access-Control-Expose-Headers",
      "Access-Control-Max-Age",
      "Access-Control-Request-Headers",
      "Access-Control-Request-Method",
      "Origin",
      "Accept",
      "Cache-Control",
      "Connection",
      "Content-Length",
      "Content-Type",
      "Date",
      "ETag",
      "Expires",
      "If-Match",
      "If-Modified-Since",
      "If-None-Match",
      "If-Unmodified-Since",
      "Last-Modified",
      "Pragma",
      "Request-Id",
      "Retry-After",
      "Server",
      "Transfer-Encoding",
      "User-Agent"
  ];
  var defaultAllowedQueryParameters = ["api-version"];
  var Sanitizer = /** @class */ (function () {
      function Sanitizer(_a) {
          var _b = _a === void 0 ? {} : _a, _c = _b.allowedHeaderNames, allowedHeaderNames = _c === void 0 ? [] : _c, _d = _b.allowedQueryParameters, allowedQueryParameters = _d === void 0 ? [] : _d;
          allowedHeaderNames = Array.isArray(allowedHeaderNames)
              ? defaultAllowedHeaderNames.concat(allowedHeaderNames)
              : defaultAllowedHeaderNames;
          allowedQueryParameters = Array.isArray(allowedQueryParameters)
              ? defaultAllowedQueryParameters.concat(allowedQueryParameters)
              : defaultAllowedQueryParameters;
          this.allowedHeaderNames = new Set(allowedHeaderNames.map(function (n) { return n.toLowerCase(); }));
          this.allowedQueryParameters = new Set(allowedQueryParameters.map(function (p) { return p.toLowerCase(); }));
      }
      Sanitizer.prototype.sanitize = function (obj) {
          return JSON.stringify(obj, this.replacer.bind(this), 2);
      };
      Sanitizer.prototype.replacer = function (key, value) {
          // Ensure Errors include their interesting non-enumerable members
          if (value instanceof Error) {
              return __assign(__assign({}, value), { name: value.name, message: value.message });
          }
          if (key === "_headersMap") {
              return this.sanitizeHeaders(key, value);
          }
          else if (key === "url") {
              return this.sanitizeUrl(value);
          }
          else if (key === "query") {
              return this.sanitizeQuery(value);
          }
          else if (key === "body") {
              // Don't log the request body
              return undefined;
          }
          else if (key === "response") {
              // Don't log response again
              return undefined;
          }
          else if (key === "operationSpec") {
              // When using sendOperationRequest, the request carries a massive
              // field with the autorest spec. No need to log it.
              return undefined;
          }
          return value;
      };
      Sanitizer.prototype.sanitizeHeaders = function (_, value) {
          return this.sanitizeObject(value, this.allowedHeaderNames, function (v, k) { return v[k].value; });
      };
      Sanitizer.prototype.sanitizeQuery = function (value) {
          return this.sanitizeObject(value, this.allowedQueryParameters, function (v, k) { return v[k]; });
      };
      Sanitizer.prototype.sanitizeObject = function (value, allowedKeys, accessor) {
          if (typeof value !== "object" || value === null) {
              return value;
          }
          var sanitized = {};
          for (var _i = 0, _a = Object.keys(value); _i < _a.length; _i++) {
              var k = _a[_i];
              if (allowedKeys.has(k.toLowerCase())) {
                  sanitized[k] = accessor(value, k);
              }
              else {
                  sanitized[k] = RedactedString;
              }
          }
          return sanitized;
      };
      Sanitizer.prototype.sanitizeUrl = function (value) {
          if (typeof value !== "string" || value === null) {
              return value;
          }
          var urlBuilder = URLBuilder.parse(value);
          var queryString = urlBuilder.getQuery();
          if (!queryString) {
              return value;
          }
          var query = URLQuery.parse(queryString);
          for (var _i = 0, _a = query.keys(); _i < _a.length; _i++) {
              var k = _a[_i];
              if (!this.allowedQueryParameters.has(k.toLowerCase())) {
                  query.set(k, RedactedString);
              }
          }
          urlBuilder.setQuery(query.toString());
          return urlBuilder.toString();
      };
      return Sanitizer;
  }());
  export { Sanitizer };
  //# sourceMappingURL=sanitizer.js.map