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

//# debugId=90B0BB64DC0BF97D64756E2164756E21
