var J = Object.defineProperty;
var i = (r, p) => J(r, "name", { value: p, configurable: !0 });
import { assertFetchApi as z } from "./assert-fetch-api.js";
import { createClientMethodSerializers as C, getJsonOptions as D } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol/serialization.js";
import { runStreamingCall as K, runUnaryCall as N } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol/run-call.js";
import { requestHeader as F } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol-connect/request-header.js";
import { createMethodUrl as P } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol/create-method-url.js";
import { createContextValues as k } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/context-values.js";
import { validateResponse as B } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol-connect/validate-response.js";
import { MethodIdempotency as L, MethodKind as Q } from "../../../../../../@bufbuild_protobuf@1.10.0/node_modules/@bufbuild/protobuf/dist/esm/service-type.js";
import { transformConnectPostToGetRequest as W } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol-connect/get-request.js";
import { errorFromJson as X } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol-connect/error-json.js";
import { appendHeaders as Y } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/http-headers.js";
import { trailerDemux as U } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol-connect/trailer-mux.js";
import { encodeEnvelope as Z, createEnvelopeReadableStream as $ } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol/envelope.js";
import { compressedFlag as A } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol/compression.js";
import { ConnectError as V } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/connect-error.js";
import { Code as M } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/code.js";
import { endStreamFlag as G, endStreamFromJson as q } from "../../../../../../@connectrpc_connect@1.6.1_@bufbuild_protobuf@1.10.0/node_modules/@connectrpc/connect/dist/esm/protocol-connect/end-stream.js";
var S = function(r) {
  return this instanceof S ? (this.v = r, this) : new S(r);
}, ee = function(r, p, f) {
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
  var l = f.apply(r, p || []), n, c = [];
  return n = {}, y("next"), y("throw"), y("return", s), n[Symbol.asyncIterator] = function() {
    return this;
  }, n;
  function s(e) {
    return function(o) {
      return Promise.resolve(o).then(e, g);
    };
  }
  function y(e, o) {
    l[e] && (n[e] = function(t) {
      return new Promise(function(d, b) {
        c.push([e, t, d, b]) > 1 || h(e, t);
      });
    }, o && (n[e] = o(n[e])));
  }
  function h(e, o) {
    try {
      u(l[e](o));
    } catch (t) {
      w(c[0][3], t);
    }
  }
  function u(e) {
    e.value instanceof S ? Promise.resolve(e.value.v).then(m, g) : w(c[0][2], e);
  }
  function m(e) {
    h("next", e);
  }
  function g(e) {
    h("throw", e);
  }
  function w(e, o) {
    e(o), c.shift(), c.length && h(c[0][0], c[0][1]);
  }
};
function be(r) {
  var p;
  z();
  const f = (p = r.useBinaryFormat) !== null && p !== void 0 ? p : !1;
  return {
    async unary(l, n, c, s, y, h, u) {
      var m;
      const { serialize: g, parse: w } = C(n, f, r.jsonOptions, r.binaryOptions);
      return s = s === void 0 ? r.defaultTimeoutMs : s <= 0 ? void 0 : s, await N({
        interceptors: r.interceptors,
        signal: c,
        timeoutMs: s,
        req: {
          stream: !1,
          service: l,
          method: n,
          url: P(r.baseUrl, l, n),
          init: {
            method: "POST",
            credentials: (m = r.credentials) !== null && m !== void 0 ? m : "same-origin",
            redirect: "error",
            mode: "cors"
          },
          header: F(n.kind, f, s, y),
          contextValues: u ?? k(),
          message: h
        },
        next: /* @__PURE__ */ i(async (e) => {
          var o;
          const t = r.useHttpGet === !0 && n.idempotency === L.NoSideEffects;
          let d = null;
          t ? e = W(e, g(e.message), f) : d = g(e.message);
          const a = await ((o = r.fetch) !== null && o !== void 0 ? o : globalThis.fetch)(e.url, Object.assign(Object.assign({}, e.init), { headers: e.header, signal: e.signal, body: d })), { isUnaryError: v, unaryError: O } = B(n.kind, f, a.status, a.headers);
          if (v)
            throw X(await a.json(), Y(...U(a.headers)), O);
          const [_, j] = U(a.headers);
          return {
            stream: !1,
            service: l,
            method: n,
            header: _,
            message: f ? w(new Uint8Array(await a.arrayBuffer())) : n.O.fromJson(await a.json(), D(r.jsonOptions)),
            trailer: j
          };
        }, "next")
      });
    },
    async stream(l, n, c, s, y, h, u) {
      var m;
      const { serialize: g, parse: w } = C(n, f, r.jsonOptions, r.binaryOptions);
      function e(t, d, b, a) {
        return ee(this, arguments, /* @__PURE__ */ i(function* () {
          const O = $(t).getReader();
          let _ = !1;
          for (; ; ) {
            const j = yield S(O.read());
            if (j.done)
              break;
            const { flags: I, data: x } = j.value;
            if ((I & A) === A)
              throw new V("protocol error: received unsupported compressed output", M.Internal);
            if ((I & G) === G) {
              _ = !0;
              const T = q(x);
              if (T.error) {
                const R = T.error;
                throw b.forEach((E, H) => {
                  R.metadata.append(H, E);
                }), R;
              }
              T.metadata.forEach((R, E) => d.set(E, R));
              continue;
            }
            yield yield S(w(x));
          }
          if ("throwIfAborted" in a && a.throwIfAborted(), !_)
            throw "missing EndStreamResponse";
        }, "parseResponseBody_1"));
      }
      i(e, "parseResponseBody");
      async function o(t) {
        if (n.kind != Q.ServerStreaming)
          throw "The fetch API does not support streaming request bodies";
        const d = await t[Symbol.asyncIterator]().next();
        if (d.done == !0)
          throw "missing request message";
        return Z(0, g(d.value));
      }
      return i(o, "createRequestBody"), s = s === void 0 ? r.defaultTimeoutMs : s <= 0 ? void 0 : s, await K({
        interceptors: r.interceptors,
        timeoutMs: s,
        signal: c,
        req: {
          stream: !0,
          service: l,
          method: n,
          url: P(r.baseUrl, l, n),
          init: {
            method: "POST",
            credentials: (m = r.credentials) !== null && m !== void 0 ? m : "same-origin",
            redirect: "error",
            mode: "cors"
          },
          header: F(n.kind, f, s, y),
          contextValues: u ?? k(),
          message: h
        },
        next: /* @__PURE__ */ i(async (t) => {
          var d;
          const a = await ((d = r.fetch) !== null && d !== void 0 ? d : globalThis.fetch)(t.url, Object.assign(Object.assign({}, t.init), { headers: t.header, signal: t.signal, body: await o(t.message) }));
          if (B(n.kind, f, a.status, a.headers), a.body === null)
            throw "missing response body";
          const v = new Headers();
          return Object.assign(Object.assign({}, t), { header: a.headers, trailer: v, message: e(a.body, v, a.headers, t.signal) });
        }, "next")
      });
    }
  };
}
i(be, "createConnectTransport");
export {
  be as createConnectTransport
};
