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