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