"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.zipWithPar = exports.zipWithPar_ = void 0;
const T = __importStar(require("../../../Effect"));
const Ex = __importStar(require("../../../Exit"));
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"));
/**
 * Runs both sinks in parallel on the input and combines the results
 * using the provided function.
 */
function zipWithPar_(self, that, f, capacity = 16) {
    const channel = CH.unwrapManaged((M.map_(M.let_(M.let_(M.let_(M.let_(M.bind_(M.bind_(M.bind_(M.do, "hub", () => T.toManaged(H.makeBounded(capacity))), "left", ({ hub }) => CH.fromHubManaged(hub)), "right", ({ hub }) => CH.fromHubManaged(hub)), "reader", ({ hub }) => CH.toHub(hub)), "c1", ({ left }) => left[">>>"](self.channel)), "c2", ({ right }) => right[">>>"](that.channel)), "writer", ({ c1, c2 }) => CH.mergeWith_(c1, c2, Ex.fold((err) => MH.done(T.halt(err)), (lz) => MH.await_(Ex.fold((cause) => T.halt(cause), (rz) => T.succeed(f(lz, rz))))), Ex.fold((err) => MH.done(T.halt(err)), (rz) => MH.await_(Ex.fold((cause) => T.halt(cause), (lz) => T.succeed(f(lz, rz))))))), ({ reader, writer }) => CH.mergeWith_(reader, writer, (_) => MH.await_(Ex.fold((cause) => T.halt(cause), (z) => T.succeed(z))), Ex.fold((cause) => MH.done(T.halt(cause)), (z) => MH.done(T.succeed(z)))))));
    return new C.Sink(channel);
}
exports.zipWithPar_ = zipWithPar_;
/**
 * Runs both sinks in parallel on the input and combines the results
 * using the provided function.
 *
 * @ets_data_first zipWithPar_
 */
function zipWithPar(that, f, capacity = 16) {
    return (self) => zipWithPar_(self, that, f, capacity);
}
exports.zipWithPar = zipWithPar;
//# sourceMappingURL=zipWithPar.js.map