import * as T from "../../../Effect";
import * as E from "../../../Either";
import { pipe } from "../../../Function";
import * as H from "../../../Hub";
import * as M from "../../../Managed";
import * as CH from "../Channel";
import * as MH from "../Channel/_internal/mergeHelpers";
import * as C from "./core";
import * as UnwrapManaged from "./unwrapManaged";
/**
 * Runs both sinks in parallel on the input, returning the result or the error from the
 * one that finishes first.
 */

export function raceBoth_(self, that, capacity = 16) {
  const managed = M.map_(M.let_(M.let_(M.let_(M.bind_(M.bind_(M.bind_(M.do, "hub", () => T.toManaged(H.makeBounded(capacity))), "c1", ({
    hub
  }) => CH.fromHubManaged(hub)), "c2", ({
    hub
  }) => CH.fromHubManaged(hub)), "reader", ({
    hub
  }) => CH.toHub(hub)), "writer", ({
    c1,
    c2
  }) => CH.mergeWith_(c1[">>>"](self.channel), c2[">>>"](that.channel), selfDone => MH.done(T.map_(T.done(selfDone), E.left)), thatDone => MH.done(T.map_(T.done(thatDone), E.right)))), "channel", ({
    reader,
    writer
  }) => CH.mergeWith_(reader, writer, _ => MH.await_(_ => T.done(_)), done => MH.done(T.done(done)))), ({
    channel
  }) => new C.Sink(channel));
  return UnwrapManaged.unwrapManaged(managed);
}
/**
 * Runs both sinks in parallel on the input, returning the result or the error from the
 * one that finishes first.
 *
 * @ets_data_first orElse_
 */

export function raceBoth(that, capacity = 16) {
  return self => raceBoth_(self, that, capacity);
}
//# sourceMappingURL=raceBoth.js.map