import{C as e,T as t}from"./CzgNarYu.js";var n=class{marshaller;serializer;deserializer;serdeContext;defaultContentType;constructor({marshaller:e,serializer:t,deserializer:n,serdeContext:r,defaultContentType:i}){this.marshaller=e,this.serializer=t,this.deserializer=n,this.serdeContext=r,this.defaultContentType=i}async serializeEventStream({eventStream:e,requestSchema:t,initialRequest:n}){let r=this.marshaller,i=t.getEventStreamMember(),a=t.getMemberSchema(i),o=this.serializer,s=this.defaultContentType,c=Symbol(`initialRequestMarker`),l={async*[Symbol.asyncIterator](){if(n){let e={":event-type":{type:`string`,value:`initial-request`},":message-type":{type:`string`,value:`event`},":content-type":{type:`string`,value:s}};o.write(t,n);let r=o.flush();yield{[c]:!0,headers:e,body:r}}for await(let t of e)yield t}};return r.serialize(l,e=>{if(e[c])return{headers:e.headers,body:e.body};let t=``;for(let n in e)if(n!==`__type`){t=n;break}let{additionalHeaders:n,body:r,eventType:i,explicitPayloadContentType:o}=this.writeEventBody(t,a,e);return{headers:{":event-type":{type:`string`,value:i},":message-type":{type:`string`,value:`event`},":content-type":{type:`string`,value:o??s},...n},body:r}})}async deserializeEventStream({response:t,responseSchema:n,initialResponseContainer:r}){let i=this.marshaller,a=n.getEventStreamMember(),o=n.getMemberSchema(a).getMemberSchemas(),s=Symbol(`initialResponseMarker`),c=i.deserialize(t.body,async t=>{let r=``;for(let e in t)if(e!==`__type`){r=e;break}let i=t[r].body;if(r===`initial-response`){let e=await this.deserializer.read(n,i);return delete e[a],{[s]:!0,...e}}else if(r in o){let n=o[r];if(n.isStructSchema()){let a={},o=!1;for(let[s,c]of n.structIterator()){let{eventHeader:n,eventPayload:l}=c.getMergedTraits();if(o||=!!(n||l),l)c.isBlobSchema()?a[s]=i:c.isStringSchema()?a[s]=(this.serdeContext?.utf8Encoder??e)(i):c.isStructSchema()&&(a[s]=await this.deserializer.read(c,i));else if(n){let e=t[r].headers[s]?.value;e!=null&&(c.isNumericSchema()?e&&typeof e==`object`&&`bytes`in e?a[s]=BigInt(e.toString()):a[s]=Number(e):a[s]=e)}}if(o)return{[r]:a};if(i.byteLength===0)return{[r]:{}}}return{[r]:await this.deserializer.read(n,i)}}else return{$unknown:t}}),l=c[Symbol.asyncIterator](),u=await l.next();if(u.done)return c;if(u.value?.[s]){if(!n)throw Error(`@smithy::core/protocols - initial-response event encountered in event stream but no response schema given.`);for(let e in u.value)r[e]=u.value[e]}return{async*[Symbol.asyncIterator](){for(u?.value?.[s]||(yield u.value);;){let{done:e,value:t}=await l.next();if(e)break;yield t}}}}writeEventBody(e,n,r){let i=this.serializer,a=e,o=null,s,c=n.getSchema()[4].includes(e),l={};if(c){let t=n.getMemberSchema(e);if(t.isStructSchema()){for(let[n,i]of t.structIterator()){let{eventHeader:t,eventPayload:a}=i.getMergedTraits();if(a)o=n;else if(t){let t=r[e][n],a=`binary`;i.isNumericSchema()?a=(-2)**31<=t&&t<=2**31-1?`integer`:`long`:i.isTimestampSchema()?a=`timestamp`:i.isStringSchema()?a=`string`:i.isBooleanSchema()&&(a=`boolean`),t!=null&&(l[n]={type:a,value:t},delete r[e][n])}}if(o!==null){let n=t.getMemberSchema(o);n.isBlobSchema()?s=`application/octet-stream`:n.isStringSchema()&&(s=`text/plain`),i.write(n,r[e][o])}else i.write(t,r[e])}else if(t.isUnitSchema())i.write(t,{});else throw Error(`@smithy/core/event-streams - non-struct member not supported in event stream union.`)}else{let[t,n]=r[e];a=t,i.write(15,n)}let u=i.flush()??new Uint8Array;return{body:typeof u==`string`?(this.serdeContext?.utf8Decoder??t)(u):u,eventType:a,explicitPayloadContentType:s,additionalHeaders:l}}};export{n as EventStreamSerde};