import { K as HttpBindingProtocol, L as HttpInterceptingShapeSerializer, O as HttpInterceptingShapeDeserializer, y as NormalizedSchema, q as toBase64$1, Q as determineTimestampFormat, U as dateToUtcString, V as NumericValue, W as generateIdempotencyToken, X as LazyJsonString, u as fromBase64$1, Y as parseEpochTimestamp, Z as parseRfc7231DateTime, _ as parseRfc3339DateTimeWithOffset, $ as collectBody, t as toUtf8$1 } from './role-executor-C4-3bmgC.js';
import { T as ProtocolLib, U as SerdeContextConfig, V as UnionSerde } from './UnionSerde-Bmr-M8zU.js';

//#region ../../node_modules/.pnpm/@aws-sdk+core@3.974.11/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/jsonReviver.js
function jsonReviver(key, value, context) {
	if (context?.source) {
		const numericString = context.source;
		if (typeof value === "number") {
			if (value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER || numericString !== String(value)) if (numericString.includes(".")) return new NumericValue(numericString, "bigDecimal");
			else return BigInt(numericString);
		}
	}
	return value;
}
//#endregion
//#region ../../node_modules/.pnpm/@aws-sdk+core@3.974.11/node_modules/@aws-sdk/core/dist-es/submodules/protocols/common.js
var collectBodyString = (streamBody, context) => collectBody(streamBody, context).then((body) => (context?.utf8Encoder ?? toUtf8$1)(body));
//#endregion
//#region ../../node_modules/.pnpm/@aws-sdk+core@3.974.11/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/parseJsonBody.js
var parseJsonBody = (streamBody, context) => 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 {};
});
var findKey = (object, key) => Object.keys(object).find((k) => k.toLowerCase() === key.toLowerCase());
var sanitizeErrorCode = (rawValue, removeNamespace = true) => {
	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 (removeNamespace && cleanValue.indexOf("#") >= 0) cleanValue = cleanValue.split("#")[1];
	return cleanValue;
};
var loadRestJsonErrorCode = (output, data) => {
	return loadErrorCode(output, data, true, [
		"header",
		"code",
		"type"
	]);
};
var loadErrorCode = ({ headers }, data, removeNamespace, order) => {
	while (order.length > 0) switch (order.shift()) {
		case "header":
			const headerKey = findKey(headers ?? {}, "x-amzn-errortype");
			if (headerKey !== void 0) return sanitizeErrorCode(headers[headerKey], removeNamespace);
			break;
		case "code":
			const codeKey = findKey(data ?? {}, "code");
			if (codeKey && data[codeKey] !== void 0) return sanitizeErrorCode(data[codeKey], removeNamespace);
			break;
		case "type":
			if (data?.__type !== void 0) return sanitizeErrorCode(data.__type, removeNamespace);
			break;
	}
};
//#endregion
//#region ../../node_modules/.pnpm/@aws-sdk+core@3.974.11/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/JsonShapeDeserializer.js
var JsonShapeDeserializer = class extends SerdeContextConfig {
	settings;
	constructor(settings) {
		super();
		this.settings = settings;
	}
	async read(schema, data) {
		return this._read(schema, typeof data === "string" ? JSON.parse(data, jsonReviver) : await parseJsonBody(data, this.serdeContext));
	}
	readObject(schema, data) {
		return this._read(schema, data);
	}
	_read(schema, value) {
		const isObject = value !== null && typeof value === "object";
		const ns = NormalizedSchema.of(schema);
		if (isObject) {
			if (ns.isStructSchema()) {
				const record = value;
				const union = ns.isUnionSchema();
				const out = {};
				let nameMap = void 0;
				const { jsonName } = this.settings;
				if (jsonName) nameMap = {};
				let unionSerde;
				if (union) unionSerde = new UnionSerde(record, out);
				for (const [memberName, memberSchema] of ns.structIterator()) {
					let fromKey = memberName;
					if (jsonName) {
						fromKey = memberSchema.getMergedTraits().jsonName ?? fromKey;
						nameMap[fromKey] = memberName;
					}
					if (union) unionSerde.mark(fromKey);
					if (record[fromKey] != null) out[memberName] = this._read(memberSchema, record[fromKey]);
				}
				if (union) unionSerde.writeUnknown();
				else if (typeof record.__type === "string") for (const k in record) {
					const v = record[k];
					const t = jsonName ? nameMap[k] ?? k : k;
					if (!(t in out)) out[t] = v;
				}
				return out;
			}
			if (Array.isArray(value) && ns.isListSchema()) {
				const listMember = ns.getValueSchema();
				const out = [];
				for (const item of value) out.push(this._read(listMember, item));
				return out;
			}
			if (ns.isMapSchema()) {
				const mapMember = ns.getValueSchema();
				const out = {};
				for (const _k in value) out[_k] = this._read(mapMember, value[_k]);
				return out;
			}
		}
		if (ns.isBlobSchema() && typeof value === "string") return fromBase64$1(value);
		const mediaType = ns.getMergedTraits().mediaType;
		if (ns.isStringSchema() && typeof value === "string" && mediaType) {
			if (mediaType === "application/json" || mediaType.endsWith("+json")) return LazyJsonString.from(value);
			return value;
		}
		if (ns.isTimestampSchema() && value != null) switch (determineTimestampFormat(ns, this.settings)) {
			case 5: return parseRfc3339DateTimeWithOffset(value);
			case 6: return parseRfc7231DateTime(value);
			case 7: return parseEpochTimestamp(value);
			default:
				console.warn("Missing timestamp format, parsing value with Date constructor:", value);
				return new Date(value);
		}
		if (ns.isBigIntegerSchema() && (typeof value === "number" || typeof value === "string")) return BigInt(value);
		if (ns.isBigDecimalSchema() && value != void 0) {
			if (value instanceof NumericValue) return value;
			const untyped = value;
			if (untyped.type === "bigDecimal" && "string" in untyped) return new NumericValue(untyped.string, untyped.type);
			return new NumericValue(String(value), "bigDecimal");
		}
		if (ns.isNumericSchema() && typeof value === "string") {
			switch (value) {
				case "Infinity": return Infinity;
				case "-Infinity": return -Infinity;
				case "NaN": return NaN;
			}
			return value;
		}
		if (ns.isDocumentSchema()) if (isObject) {
			const out = Array.isArray(value) ? [] : {};
			for (const k in value) {
				const v = value[k];
				if (v instanceof NumericValue) out[k] = v;
				else out[k] = this._read(ns, v);
			}
			return out;
		} else return structuredClone(value);
		return value;
	}
};
//#endregion
//#region ../../node_modules/.pnpm/@aws-sdk+core@3.974.11/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/jsonReplacer.js
var NUMERIC_CONTROL_CHAR = String.fromCharCode(925);
var JsonReplacer = class {
	values = /* @__PURE__ */ new Map();
	counter = 0;
	stage = 0;
	createReplacer() {
		if (this.stage === 1) throw new Error("@aws-sdk/core/protocols - JsonReplacer already created.");
		if (this.stage === 2) throw new Error("@aws-sdk/core/protocols - JsonReplacer exhausted.");
		this.stage = 1;
		return (key, value) => {
			if (value instanceof NumericValue) {
				const v = `${NUMERIC_CONTROL_CHAR + "nv" + this.counter++}_` + value.string;
				this.values.set(`"${v}"`, value.string);
				return v;
			}
			if (typeof value === "bigint") {
				const s = value.toString();
				const v = `${NUMERIC_CONTROL_CHAR + "b" + this.counter++}_` + s;
				this.values.set(`"${v}"`, s);
				return v;
			}
			return value;
		};
	}
	replaceInJson(json) {
		if (this.stage === 0) throw new Error("@aws-sdk/core/protocols - JsonReplacer not created yet.");
		if (this.stage === 2) throw new Error("@aws-sdk/core/protocols - JsonReplacer exhausted.");
		this.stage = 2;
		if (this.counter === 0) return json;
		for (const [key, value] of this.values) json = json.replace(key, value);
		return json;
	}
};
//#endregion
//#region ../../node_modules/.pnpm/@aws-sdk+core@3.974.11/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/JsonShapeSerializer.js
var JsonShapeSerializer = class extends SerdeContextConfig {
	settings;
	buffer;
	useReplacer = false;
	rootSchema;
	constructor(settings) {
		super();
		this.settings = settings;
	}
	write(schema, value) {
		this.rootSchema = NormalizedSchema.of(schema);
		this.buffer = this._write(this.rootSchema, value);
	}
	flush() {
		const { rootSchema, useReplacer } = this;
		this.rootSchema = void 0;
		this.useReplacer = false;
		if (rootSchema?.isStructSchema() || rootSchema?.isDocumentSchema()) {
			if (!useReplacer) return JSON.stringify(this.buffer);
			const replacer = new JsonReplacer();
			return replacer.replaceInJson(JSON.stringify(this.buffer, replacer.createReplacer(), 0));
		}
		return this.buffer;
	}
	writeDiscriminatedDocument(schema, value) {
		this.write(schema, value);
		if (typeof this.buffer === "object") this.buffer.__type = NormalizedSchema.of(schema).getName(true);
	}
	_write(schema, value, container) {
		const isObject = value !== null && typeof value === "object";
		const ns = NormalizedSchema.of(schema);
		if (isObject) {
			if (ns.isStructSchema()) {
				const record = value;
				const out = {};
				const { jsonName } = this.settings;
				let nameMap = void 0;
				if (jsonName) nameMap = {};
				let outCount = 0;
				for (const [memberName, memberSchema] of ns.structIterator()) {
					const serializableValue = this._write(memberSchema, record[memberName], ns);
					if (serializableValue !== void 0) {
						let targetKey = memberName;
						if (jsonName) {
							targetKey = memberSchema.getMergedTraits().jsonName ?? memberName;
							nameMap[memberName] = targetKey;
						}
						out[targetKey] = serializableValue;
						outCount++;
					}
				}
				if (ns.isUnionSchema() && outCount === 0) {
					const { $unknown } = record;
					if (Array.isArray($unknown)) {
						const [k, v] = $unknown;
						out[k] = this._write(15, v);
					}
				} else if (typeof record.__type === "string") for (const k in record) {
					const v = record[k];
					const targetKey = jsonName ? nameMap[k] ?? k : k;
					if (!(targetKey in out)) out[targetKey] = this._write(15, v);
				}
				return out;
			}
			if (Array.isArray(value) && ns.isListSchema()) {
				const listMember = ns.getValueSchema();
				const out = [];
				const sparse = !!ns.getMergedTraits().sparse;
				for (const item of value) if (sparse || item != null) out.push(this._write(listMember, item));
				return out;
			}
			if (ns.isMapSchema()) {
				const mapMember = ns.getValueSchema();
				const out = {};
				const sparse = !!ns.getMergedTraits().sparse;
				for (const _k in value) {
					const _v = value[_k];
					if (sparse || _v != null) out[_k] = this._write(mapMember, _v);
				}
				return out;
			}
			if (value instanceof Uint8Array && (ns.isBlobSchema() || ns.isDocumentSchema())) {
				if (ns === this.rootSchema) return value;
				return (this.serdeContext?.base64Encoder ?? toBase64$1)(value);
			}
			if (value instanceof Date && (ns.isTimestampSchema() || ns.isDocumentSchema())) switch (determineTimestampFormat(ns, this.settings)) {
				case 5: return value.toISOString().replace(".000Z", "Z");
				case 6: return dateToUtcString(value);
				case 7: return value.getTime() / 1e3;
				default:
					console.warn("Missing timestamp format, using epoch seconds", value);
					return value.getTime() / 1e3;
			}
			if (value instanceof NumericValue) this.useReplacer = true;
		}
		if (value === null && container?.isStructSchema()) return;
		if (ns.isStringSchema()) {
			if (typeof value === "undefined" && ns.isIdempotencyToken()) return generateIdempotencyToken();
			const mediaType = ns.getMergedTraits().mediaType;
			if (value != null && mediaType) {
				if (mediaType === "application/json" || mediaType.endsWith("+json")) return LazyJsonString.from(value);
			}
			return value;
		}
		if (typeof value === "number" && ns.isNumericSchema()) {
			if (Math.abs(value) === Infinity || isNaN(value)) return String(value);
			return value;
		}
		if (typeof value === "string" && ns.isBlobSchema()) {
			if (ns === this.rootSchema) return value;
			return (this.serdeContext?.base64Encoder ?? toBase64$1)(value);
		}
		if (typeof value === "bigint") this.useReplacer = true;
		if (ns.isDocumentSchema()) if (isObject) {
			const out = Array.isArray(value) ? [] : {};
			for (const k in value) {
				const v = value[k];
				if (v instanceof NumericValue) {
					this.useReplacer = true;
					out[k] = v;
				} else out[k] = this._write(ns, v);
			}
			return out;
		} else return structuredClone(value);
		return value;
	}
};
//#endregion
//#region ../../node_modules/.pnpm/@aws-sdk+core@3.974.11/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/JsonCodec.js
var JsonCodec = class extends SerdeContextConfig {
	settings;
	constructor(settings) {
		super();
		this.settings = settings;
	}
	createSerializer() {
		const serializer = new JsonShapeSerializer(this.settings);
		serializer.setSerdeContext(this.serdeContext);
		return serializer;
	}
	createDeserializer() {
		const deserializer = new JsonShapeDeserializer(this.settings);
		deserializer.setSerdeContext(this.serdeContext);
		return deserializer;
	}
};
//#endregion
//#region ../../node_modules/.pnpm/@aws-sdk+core@3.974.11/node_modules/@aws-sdk/core/dist-es/submodules/protocols/json/AwsRestJsonProtocol.js
var AwsRestJsonProtocol = class extends HttpBindingProtocol {
	serializer;
	deserializer;
	codec;
	mixin = new ProtocolLib();
	constructor({ defaultNamespace, errorTypeRegistries }) {
		super({
			defaultNamespace,
			errorTypeRegistries
		});
		const settings = {
			timestampFormat: {
				useTrait: true,
				default: 7
			},
			httpBindings: true,
			jsonName: true
		};
		this.codec = new JsonCodec(settings);
		this.serializer = new HttpInterceptingShapeSerializer(this.codec.createSerializer(), settings);
		this.deserializer = new HttpInterceptingShapeDeserializer(this.codec.createDeserializer(), settings);
	}
	getShapeId() {
		return "aws.protocols#restJson1";
	}
	getPayloadCodec() {
		return this.codec;
	}
	setSerdeContext(serdeContext) {
		this.codec.setSerdeContext(serdeContext);
		super.setSerdeContext(serdeContext);
	}
	async serializeRequest(operationSchema, input, context) {
		const request = await super.serializeRequest(operationSchema, input, context);
		const inputSchema = NormalizedSchema.of(operationSchema.input);
		if (!request.headers["content-type"]) {
			const contentType = this.mixin.resolveRestContentType(this.getDefaultContentType(), inputSchema);
			if (contentType) request.headers["content-type"] = contentType;
		}
		if (request.body == null && request.headers["content-type"] === this.getDefaultContentType()) request.body = "{}";
		return request;
	}
	async deserializeResponse(operationSchema, context, response) {
		const output = await super.deserializeResponse(operationSchema, context, response);
		const outputSchema = NormalizedSchema.of(operationSchema.output);
		for (const [name, member] of outputSchema.structIterator()) if (member.getMemberTraits().httpPayload && !(name in output)) output[name] = null;
		return output;
	}
	async handleError(operationSchema, context, response, dataObject, metadata) {
		const errorIdentifier = loadRestJsonErrorCode(response, dataObject) ?? "Unknown";
		this.mixin.compose(this.compositeErrorRegistry, errorIdentifier, this.options.defaultNamespace);
		const { errorSchema, errorMetadata } = await this.mixin.getErrorSchemaOrThrowBaseException(errorIdentifier, this.options.defaultNamespace, response, dataObject, metadata);
		const ns = NormalizedSchema.of(errorSchema);
		const message = dataObject.message ?? dataObject.Message ?? "UnknownError";
		const exception = new ((this.compositeErrorRegistry.getErrorCtor(errorSchema)) ?? Error)({});
		await this.deserializeHttpMessage(errorSchema, context, response, dataObject);
		const output = {};
		const errorDeserializer = this.codec.createDeserializer();
		for (const [name, member] of ns.structIterator()) {
			const target = member.getMergedTraits().jsonName ?? name;
			output[name] = errorDeserializer.readObject(member, dataObject[target]);
		}
		throw this.mixin.decorateServiceException(Object.assign(exception, errorMetadata, {
			$fault: ns.getMergedTraits().error,
			message
		}, output), dataObject);
	}
	getDefaultContentType() {
		return "application/json";
	}
};

export { AwsRestJsonProtocol as A };
//# sourceMappingURL=AwsRestJsonProtocol-BDEynihU.js.map
