import { t as toUtf8$1, m as fromUtf8$1 } from './role-executor-C4-3bmgC.js';
import './paths-2M_UxD_H.js';
import 'node:fs';
import 'node:path';
import 'node:os';
import 'node:events';
import 'node:diagnostics_channel';
import 'fs';
import 'events';
import 'util';
import 'path';
import 'assert';
import 'worker_threads';
import 'module';
import 'node:url';
import 'url';
import 'buffer';
import 'node:module';
import 'node:child_process';
import 'node:crypto';
import 'node:process';
import 'node:async_hooks';
import './credential-pool-B04qO8at.js';
import './index-IVoIz4D5.js';
import 'constants';
import 'stream';
import './config-B0nEjgSh.js';
import './json-DXl7iDYg.js';
import './lock-BoHF25mx.js';
import 'node:fs/promises';
import './index-Ca4C2M45.js';
import './role-resolver-DF9BQ_U6.js';
import 'node:stream';
import 'crypto';
import 'os';
import 'node:http2';
import 'fs/promises';
import 'node:net';

//#region ../../node_modules/.pnpm/@smithy+core@3.24.3/node_modules/@smithy/core/dist-es/submodules/event-streams/EventStreamSerde.js
var EventStreamSerde = class {
	marshaller;
	serializer;
	deserializer;
	serdeContext;
	defaultContentType;
	constructor({ marshaller, serializer, deserializer, serdeContext, defaultContentType }) {
		this.marshaller = marshaller;
		this.serializer = serializer;
		this.deserializer = deserializer;
		this.serdeContext = serdeContext;
		this.defaultContentType = defaultContentType;
	}
	async serializeEventStream({ eventStream, requestSchema, initialRequest }) {
		const marshaller = this.marshaller;
		const eventStreamMember = requestSchema.getEventStreamMember();
		const unionSchema = requestSchema.getMemberSchema(eventStreamMember);
		const serializer = this.serializer;
		const defaultContentType = this.defaultContentType;
		const initialRequestMarker = Symbol("initialRequestMarker");
		const eventStreamIterable = { async *[Symbol.asyncIterator]() {
			if (initialRequest) {
				const headers = {
					":event-type": {
						type: "string",
						value: "initial-request"
					},
					":message-type": {
						type: "string",
						value: "event"
					},
					":content-type": {
						type: "string",
						value: defaultContentType
					}
				};
				serializer.write(requestSchema, initialRequest);
				const body = serializer.flush();
				yield {
					[initialRequestMarker]: true,
					headers,
					body
				};
			}
			for await (const page of eventStream) yield page;
		} };
		return marshaller.serialize(eventStreamIterable, (event) => {
			if (event[initialRequestMarker]) return {
				headers: event.headers,
				body: event.body
			};
			let unionMember = "";
			for (const key in event) if (key !== "__type") {
				unionMember = key;
				break;
			}
			const { additionalHeaders, body, eventType, explicitPayloadContentType } = this.writeEventBody(unionMember, unionSchema, event);
			return {
				headers: {
					":event-type": {
						type: "string",
						value: eventType
					},
					":message-type": {
						type: "string",
						value: "event"
					},
					":content-type": {
						type: "string",
						value: explicitPayloadContentType ?? defaultContentType
					},
					...additionalHeaders
				},
				body
			};
		});
	}
	async deserializeEventStream({ response, responseSchema, initialResponseContainer }) {
		const marshaller = this.marshaller;
		const eventStreamMember = responseSchema.getEventStreamMember();
		const memberSchemas = responseSchema.getMemberSchema(eventStreamMember).getMemberSchemas();
		const initialResponseMarker = Symbol("initialResponseMarker");
		const asyncIterable = marshaller.deserialize(response.body, async (event) => {
			let unionMember = "";
			for (const key in event) if (key !== "__type") {
				unionMember = key;
				break;
			}
			const body = event[unionMember].body;
			if (unionMember === "initial-response") {
				const dataObject = await this.deserializer.read(responseSchema, body);
				delete dataObject[eventStreamMember];
				return {
					[initialResponseMarker]: true,
					...dataObject
				};
			} else if (unionMember in memberSchemas) {
				const eventStreamSchema = memberSchemas[unionMember];
				if (eventStreamSchema.isStructSchema()) {
					const out = {};
					let hasBindings = false;
					for (const [name, member] of eventStreamSchema.structIterator()) {
						const { eventHeader, eventPayload } = member.getMergedTraits();
						hasBindings = hasBindings || Boolean(eventHeader || eventPayload);
						if (eventPayload) {
							if (member.isBlobSchema()) out[name] = body;
							else if (member.isStringSchema()) out[name] = (this.serdeContext?.utf8Encoder ?? toUtf8$1)(body);
							else if (member.isStructSchema()) out[name] = await this.deserializer.read(member, body);
						} else if (eventHeader) {
							const value = event[unionMember].headers[name]?.value;
							if (value != null) if (member.isNumericSchema()) if (value && typeof value === "object" && "bytes" in value) out[name] = BigInt(value.toString());
							else out[name] = Number(value);
							else out[name] = value;
						}
					}
					if (hasBindings) return { [unionMember]: out };
					if (body.byteLength === 0) return { [unionMember]: {} };
				}
				return { [unionMember]: await this.deserializer.read(eventStreamSchema, body) };
			} else return { $unknown: event };
		});
		const asyncIterator = asyncIterable[Symbol.asyncIterator]();
		const firstEvent = await asyncIterator.next();
		if (firstEvent.done) return asyncIterable;
		if (firstEvent.value?.[initialResponseMarker]) {
			if (!responseSchema) throw new Error("@smithy::core/protocols - initial-response event encountered in event stream but no response schema given.");
			for (const key in firstEvent.value) initialResponseContainer[key] = firstEvent.value[key];
		}
		return { async *[Symbol.asyncIterator]() {
			if (!firstEvent?.value?.[initialResponseMarker]) yield firstEvent.value;
			while (true) {
				const { done, value } = await asyncIterator.next();
				if (done) break;
				yield value;
			}
		} };
	}
	writeEventBody(unionMember, unionSchema, event) {
		const serializer = this.serializer;
		let eventType = unionMember;
		let explicitPayloadMember = null;
		let explicitPayloadContentType;
		const isKnownSchema = unionSchema.getSchema()[4].includes(unionMember);
		const additionalHeaders = {};
		if (!isKnownSchema) {
			const [type, value] = event[unionMember];
			eventType = type;
			serializer.write(15, value);
		} else {
			const eventSchema = unionSchema.getMemberSchema(unionMember);
			if (eventSchema.isStructSchema()) {
				for (const [memberName, memberSchema] of eventSchema.structIterator()) {
					const { eventHeader, eventPayload } = memberSchema.getMergedTraits();
					if (eventPayload) explicitPayloadMember = memberName;
					else if (eventHeader) {
						const value = event[unionMember][memberName];
						let type = "binary";
						if (memberSchema.isNumericSchema()) if ((-2) ** 31 <= value && value <= 2 ** 31 - 1) type = "integer";
						else type = "long";
						else if (memberSchema.isTimestampSchema()) type = "timestamp";
						else if (memberSchema.isStringSchema()) type = "string";
						else if (memberSchema.isBooleanSchema()) type = "boolean";
						if (value != null) {
							additionalHeaders[memberName] = {
								type,
								value
							};
							delete event[unionMember][memberName];
						}
					}
				}
				if (explicitPayloadMember !== null) {
					const payloadSchema = eventSchema.getMemberSchema(explicitPayloadMember);
					if (payloadSchema.isBlobSchema()) explicitPayloadContentType = "application/octet-stream";
					else if (payloadSchema.isStringSchema()) explicitPayloadContentType = "text/plain";
					serializer.write(payloadSchema, event[unionMember][explicitPayloadMember]);
				} else serializer.write(eventSchema, event[unionMember]);
			} else if (eventSchema.isUnitSchema()) serializer.write(eventSchema, {});
			else throw new Error("@smithy/core/event-streams - non-struct member not supported in event stream union.");
		}
		const messageSerialization = serializer.flush() ?? new Uint8Array();
		return {
			body: typeof messageSerialization === "string" ? (this.serdeContext?.utf8Decoder ?? fromUtf8$1)(messageSerialization) : messageSerialization,
			eventType,
			explicitPayloadContentType,
			additionalHeaders
		};
	}
};

export { EventStreamSerde };
//# sourceMappingURL=event-streams-rFr-AmFE.js.map
