text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.getApplyMd5BodyChecksumPlugin = exports.applyMd5BodyChecksumMiddlewareOptions = exports.applyMd5BodyChecksumMiddleware = void 0;
  const is_array_buffer_1 = require("@aws-sdk/is-array-buffer");
  const protocol_http_1 = require("@aws-sdk/protocol-http");
  const applyMd5BodyChecksumMiddleware = (options) => (next) => async (args) => {
      let { request } = args;
      if (protocol_http_1.HttpRequest.isInstance(request)) {
          const { body, headers } = request;
          if (!hasHeader("content-md5", headers)) {
              let digest;
              if (body === undefined || typeof body === "string" || ArrayBuffer.isView(body) || is_array_buffer_1.isArrayBuffer(body)) {
                  const hash = new options.md5();
                  hash.update(body || "");
                  digest = hash.digest();
              }
              else {
                  digest = options.streamHasher(options.md5, body);
              }
              request = {
                  ...request,
                  headers: {
                      ...headers,
                      "content-md5": options.base64Encoder(await digest),
                  },
              };
          }
      }
      return next({
          ...args,
          request,
      });
  };
  exports.applyMd5BodyChecksumMiddleware = applyMd5BodyChecksumMiddleware;
  exports.applyMd5BodyChecksumMiddlewareOptions = {
      name: "applyMd5BodyChecksumMiddleware",
      step: "build",
      tags: ["SET_CONTENT_MD5", "BODY_CHECKSUM"],
      override: true,
  };
  const getApplyMd5BodyChecksumPlugin = (config) => ({
      applyToStack: (clientStack) => {
          clientStack.add(exports.applyMd5BodyChecksumMiddleware(config), exports.applyMd5BodyChecksumMiddlewareOptions);
      },
  });
  exports.getApplyMd5BodyChecksumPlugin = getApplyMd5BodyChecksumPlugin;
  const hasHeader = (soughtHeader, headers) => {
      soughtHeader = soughtHeader.toLowerCase();
      for (const headerName of Object.keys(headers)) {
          if (soughtHeader === headerName.toLowerCase()) {
              return true;
          }
      }
      return false;
  };
  