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