"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.raceWith = exports.raceWith_ = void 0;
const T = __importStar(require("../../../Effect"));
const Function_1 = require("../../../Function");
const H = __importStar(require("../../../Hub"));
const M = __importStar(require("../../../Managed"));
const CH = __importStar(require("../Channel"));
const MH = __importStar(require("../Channel/_internal/mergeHelpers"));
const C = __importStar(require("./core"));
const UnwrapManaged = __importStar(require("./unwrapManaged"));
/**
 * Runs both sinks in parallel on the input, using the specified merge
 * function as soon as one result or the other has been computed.
 */
function raceWith_(self, that, leftDone, rightDone, 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), leftDone, rightDone)), "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);
}
exports.raceWith_ = raceWith_;
/**
 * Runs both sinks in parallel on the input, using the specified merge
 * function as soon as one result or the other has been computed.
 *
 * @ets_data_first raceWith_
 */
function raceWith(that, leftDone, rightDone, capacity = 16) {
    return (self) => raceWith_(self, that, leftDone, rightDone, capacity);
}
exports.raceWith = raceWith;
//# sourceMappingURL=raceWith.js.map