var M = Object.defineProperty;
var d = (r, n) => M(r, "name", { value: n, configurable: !0 });
import { makeAnyClient as P } from "./any-client.js";
import { ConnectError as b } from "./connect-error.js";
import { Code as x } from "./code.js";
import { createAsyncIterable as V } from "./protocol/async-iterable.js";
import { MethodKind as S } from "../../../../../../@bufbuild_protobuf@1.10.0/node_modules/@bufbuild/protobuf/dist/esm/service-type.js";
var I = function(r) {
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
  var n = r[Symbol.asyncIterator], l;
  return n ? n.call(r) : (r = typeof __values == "function" ? __values(r) : r[Symbol.iterator](), l = {}, u("next"), u("throw"), u("return"), l[Symbol.asyncIterator] = function() {
    return this;
  }, l);
  function u(a) {
    l[a] = r[a] && function(i) {
      return new Promise(function(c, f) {
        i = r[a](i), e(c, f, i.done, i.value);
      });
    };
  }
  function e(a, i, c, f) {
    Promise.resolve(f).then(function(o) {
      a({ value: o, done: c });
    }, i);
  }
}, h = function(r) {
  return this instanceof h ? (this.v = r, this) : new h(r);
}, T = function(r) {
  var n, l;
  return n = {}, u("next"), u("throw", function(e) {
    throw e;
  }), u("return"), n[Symbol.iterator] = function() {
    return this;
  }, n;
  function u(e, a) {
    n[e] = r[e] ? function(i) {
      return (l = !l) ? { value: h(r[e](i)), done: !1 } : a ? a(i) : i;
    } : a;
  }
}, D = function(r, n, l) {
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
  var u = l.apply(r, n || []), e, a = [];
  return e = {}, c("next"), c("throw"), c("return", i), e[Symbol.asyncIterator] = function() {
    return this;
  }, e;
  function i(t) {
    return function(v) {
      return Promise.resolve(v).then(t, y);
    };
  }
  function c(t, v) {
    u[t] && (e[t] = function(m) {
      return new Promise(function(w, g) {
        a.push([t, m, w, g]) > 1 || f(t, m);
      });
    }, v && (e[t] = v(e[t])));
  }
  function f(t, v) {
    try {
      o(u[t](v));
    } catch (m) {
      s(a[0][3], m);
    }
  }
  function o(t) {
    t.value instanceof h ? Promise.resolve(t.value.v).then(_, y) : s(a[0][2], t);
  }
  function _(t) {
    f("next", t);
  }
  function y(t) {
    f("throw", t);
  }
  function s(t, v) {
    t(v), a.shift(), a.length && f(a[0][0], a[0][1]);
  }
};
function F(r, n) {
  return P(r, (l) => {
    switch (l.kind) {
      case S.Unary:
        return U(n, r, l);
      case S.ServerStreaming:
        return E(n, r, l);
      case S.ClientStreaming:
        return H(n, r, l);
      case S.BiDiStreaming:
        return k(n, r, l);
      default:
        return null;
    }
  });
}
d(F, "createClient");
function K(r, n) {
  return F(r, n);
}
d(K, "createPromiseClient");
function U(r, n, l) {
  return async function(u, e) {
    var a, i;
    const c = await r.unary(n, l, e?.signal, e?.timeoutMs, e?.headers, u, e?.contextValues);
    return (a = e?.onHeader) === null || a === void 0 || a.call(e, c.header), (i = e?.onTrailer) === null || i === void 0 || i.call(e, c.trailer), c.message;
  };
}
d(U, "createUnaryFn");
function E(r, n, l) {
  return function(u, e) {
    return C(r.stream(n, l, e?.signal, e?.timeoutMs, e?.headers, V([u]), e?.contextValues), e);
  };
}
d(E, "createServerStreamingFn");
function H(r, n, l) {
  return async function(u, e) {
    var a, i, c, f, o, _;
    const y = await r.stream(n, l, e?.signal, e?.timeoutMs, e?.headers, u, e?.contextValues);
    (o = e?.onHeader) === null || o === void 0 || o.call(e, y.header);
    let s, t = 0;
    try {
      for (var v = !0, m = I(y.message), w; w = await m.next(), a = w.done, !a; v = !0)
        f = w.value, v = !1, s = f, t++;
    } catch (g) {
      i = { error: g };
    } finally {
      try {
        !v && !a && (c = m.return) && await c.call(m);
      } finally {
        if (i) throw i.error;
      }
    }
    if (!s)
      throw new b("protocol error: missing response message", x.Unimplemented);
    if (t > 1)
      throw new b("protocol error: received extra messages for client streaming method", x.Unimplemented);
    return (_ = e?.onTrailer) === null || _ === void 0 || _.call(e, y.trailer), s;
  };
}
d(H, "createClientStreamingFn");
function k(r, n, l) {
  return function(u, e) {
    return C(r.stream(n, l, e?.signal, e?.timeoutMs, e?.headers, u, e?.contextValues), e);
  };
}
d(k, "createBiDiStreamingFn");
function C(r, n) {
  const l = function() {
    return D(this, arguments, function* () {
      var u, e;
      const a = yield h(r);
      (u = n?.onHeader) === null || u === void 0 || u.call(n, a.header), yield h(yield* T(I(a.message))), (e = n?.onTrailer) === null || e === void 0 || e.call(n, a.trailer);
    });
  }()[Symbol.asyncIterator]();
  return {
    [Symbol.asyncIterator]: () => ({
      next: /* @__PURE__ */ d(() => l.next(), "next")
    })
  };
}
d(C, "handleStreamResponse");
export {
  k as createBiDiStreamingFn,
  F as createClient,
  H as createClientStreamingFn,
  K as createPromiseClient,
  E as createServerStreamingFn,
  U as createUnaryFn
};
