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