'use strict';

var _2ASNEWIN_cjs = require('./2ASNEWIN.cjs');
var VO65XOWD_cjs = require('./VO65XOWD.cjs');

// ../../node_modules/.pnpm/@aws-sdk+core@3.879.0/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/parseJsonBody.js
VO65XOWD_cjs.init_cjs_shims();
var parseJsonBody = /* @__PURE__ */ VO65XOWD_cjs.__name((streamBody, context) => _2ASNEWIN_cjs.collectBodyString(streamBody, context).then((encoded) => {
  if (encoded.length) {
    try {
      return JSON.parse(encoded);
    } catch (e) {
      if (e?.name === "SyntaxError") {
        Object.defineProperty(e, "$responseBodyText", {
          value: encoded
        });
      }
      throw e;
    }
  }
  return {};
}), "parseJsonBody");
var parseJsonErrorBody = /* @__PURE__ */ VO65XOWD_cjs.__name(async (errorBody, context) => {
  const value = await parseJsonBody(errorBody, context);
  value.message = value.message ?? value.Message;
  return value;
}, "parseJsonErrorBody");
var loadRestJsonErrorCode = /* @__PURE__ */ VO65XOWD_cjs.__name((output, data) => {
  const findKey = /* @__PURE__ */ VO65XOWD_cjs.__name((object, key) => Object.keys(object).find((k) => k.toLowerCase() === key.toLowerCase()), "findKey");
  const sanitizeErrorCode = /* @__PURE__ */ VO65XOWD_cjs.__name((rawValue) => {
    let cleanValue = rawValue;
    if (typeof cleanValue === "number") {
      cleanValue = cleanValue.toString();
    }
    if (cleanValue.indexOf(",") >= 0) {
      cleanValue = cleanValue.split(",")[0];
    }
    if (cleanValue.indexOf(":") >= 0) {
      cleanValue = cleanValue.split(":")[0];
    }
    if (cleanValue.indexOf("#") >= 0) {
      cleanValue = cleanValue.split("#")[1];
    }
    return cleanValue;
  }, "sanitizeErrorCode");
  const headerKey = findKey(output.headers, "x-amzn-errortype");
  if (headerKey !== void 0) {
    return sanitizeErrorCode(output.headers[headerKey]);
  }
  if (data && typeof data === "object") {
    const codeKey = findKey(data, "code");
    if (codeKey && data[codeKey] !== void 0) {
      return sanitizeErrorCode(data[codeKey]);
    }
    if (data["__type"] !== void 0) {
      return sanitizeErrorCode(data["__type"]);
    }
  }
}, "loadRestJsonErrorCode");

exports.loadRestJsonErrorCode = loadRestJsonErrorCode;
exports.parseJsonBody = parseJsonBody;
exports.parseJsonErrorBody = parseJsonErrorBody;
//# sourceMappingURL=L34TQNQN.cjs.map
//# sourceMappingURL=L34TQNQN.cjs.map