import { t as z, f as x } from "./parquet-Bqjmp2vo.js";
class T {
  marshaller;
  serializer;
  deserializer;
  serdeContext;
  defaultContentType;
  constructor({ marshaller: a, serializer: n, deserializer: o, serdeContext: m, defaultContentType: y }) {
    this.marshaller = a, this.serializer = n, this.deserializer = o, this.serdeContext = m, this.defaultContentType = y;
  }
  async serializeEventStream({ eventStream: a, requestSchema: n, initialRequest: o }) {
    const m = this.marshaller, y = n.getEventStreamMember(), p = n.getMemberSchema(y), d = this.serializer, f = this.defaultContentType, h = /* @__PURE__ */ 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 }
          };
          d.write(n, o);
          const t = d.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: i, 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: i
      };
    });
  }
  async deserializeEventStream({ response: a, responseSchema: n, initialResponseContainer: o }) {
    const m = this.marshaller, y = n.getEventStreamMember(), d = n.getMemberSchema(y).getMemberSchemas(), f = /* @__PURE__ */ Symbol("initialResponseMarker"), h = m.deserialize(a.body, async (t) => {
      const e = Object.keys(t).find((l) => l !== "__type") ?? "", i = t[e].body;
      if (e === "initial-response") {
        const l = await this.deserializer.read(n, i);
        return delete l[y], {
          [f]: !0,
          ...l
        };
      } else if (e in d) {
        const l = d[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] = i : g.isStringSchema() ? c[s] = (this.serdeContext?.utf8Encoder ?? z)(i) : g.isStructSchema() && (c[s] = await this.deserializer.read(g, i));
            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
            };
          if (i.byteLength === 0)
            return {
              [e]: {}
            };
        }
        return {
          [e]: await this.deserializer.read(l, i)
        };
      } else
        return {
          $unknown: t
        };
    }), S = h[Symbol.asyncIterator](), r = await S.next();
    if (r.done)
      return h;
    if (r.value?.[f]) {
      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(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, n, o) {
    const m = this.serializer;
    let y = a, p = null, d;
    const f = n.getSchema()[4].includes(a), h = {};
    if (f) {
      const t = n.getMemberSchema(a);
      if (t.isStructSchema()) {
        for (const [e, i] of t.structIterator()) {
          const { eventHeader: l, eventPayload: c } = i.getMergedTraits();
          if (c)
            p = e;
          else if (l) {
            const u = o[a][e];
            let s = "binary";
            i.isNumericSchema() ? (-2) ** 31 <= u && u <= 2 ** 31 - 1 ? s = "integer" : s = "long" : i.isTimestampSchema() ? s = "timestamp" : i.isStringSchema() ? s = "string" : i.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() ? d = "application/octet-stream" : e.isStringSchema() && (d = "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];
      y = t, m.write(15, e);
    }
    const S = m.flush();
    return {
      body: typeof S == "string" ? (this.serdeContext?.utf8Decoder ?? x)(S) : S,
      eventType: y,
      explicitPayloadContentType: d,
      additionalHeaders: h
    };
  }
}
export {
  T as EventStreamSerde
};
