var b = Object.defineProperty;
var i = (e, n) => b(e, "name", { value: n, configurable: !0 });
import { applyInterceptors as u } from "../interceptor.js";
import { ConnectError as d } from "../connect-error.js";
import { createDeadlineSignal as f, createLinkedAbortController as h, getAbortSignalReason as q } from "./signals.js";
import { normalizeIterable as y, normalize as j } from "./normalize.js";
function C(e) {
  const n = u(e.next, e.interceptors), [t, r, c] = g(e), l = Object.assign(Object.assign({}, e.req), { message: j(e.req.method.I, e.req.message), signal: t });
  return n(l).then((a) => (c(), a), r);
}
i(C, "runUnaryCall");
function z(e) {
  const n = u(e.next, e.interceptors), [t, r, c] = g(e), l = Object.assign(Object.assign({}, e.req), { message: y(e.req.method.I, e.req.message), signal: t });
  let a = !1;
  return t.addEventListener("abort", function() {
    var s, m;
    const o = e.req.message[Symbol.asyncIterator]();
    a || (s = o.throw) === null || s === void 0 || s.call(o, this.reason).catch(() => {
    }), (m = o.return) === null || m === void 0 || m.call(o).catch(() => {
    });
  }), n(l).then((s) => Object.assign(Object.assign({}, s), { message: {
    [Symbol.asyncIterator]() {
      const m = s.message[Symbol.asyncIterator]();
      return {
        next() {
          return m.next().then((o) => (o.done == !0 && (a = !0, c()), o), r);
        }
        // We deliberately omit throw/return.
      };
    }
  } }), r);
}
i(z, "runStreamingCall");
function g(e) {
  const { signal: n, cleanup: t } = f(e.timeoutMs), r = h(e.signal, n);
  return [
    r.signal,
    /* @__PURE__ */ i(function(l) {
      const a = d.from(n.aborted ? q(n) : l);
      return r.abort(a), t(), Promise.reject(a);
    }, "abort"),
    /* @__PURE__ */ i(function() {
      t(), r.abort();
    }, "done")
  ];
}
i(g, "setupSignal");
export {
  z as runStreamingCall,
  C as runUnaryCall
};
