const fileName_1 = "(@effect/core) _src/io/Effect/operations/zipWithPar.ts";
import * as tsplus_module_1 from "@effect/core/io/Effect/operations/descriptorWith";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/transplant";
import * as tsplus_module_3 from "@effect/core/io/Cause/definition";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/failCause";
import * as tsplus_module_5 from "@effect/core/io/Exit/operations/fold";
import * as tsplus_module_6 from "@effect/core/io/Fiber/operations/interruptAs";
import * as tsplus_module_7 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_8 from "@effect/core/io/Fiber/operations/inheritRefs";
import * as tsplus_module_9 from "@effect/core/io/Effect/operations/zipRight";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_11 from "@effect/core/io/Fiber/operations/await";
import { IRaceWith } from "@effect/core/io/Effect/definition/primitives";
/**
 * Sequentially zips this effect with the specified effect using the
 * specified combiner function.
 *
 * @tsplus fluent ets/Effect zipWithPar
 */

export function zipWithPar_(self, that, f, __tsplusTrace) {
  const g = (b, a) => f(a, b);

  return tsplus_module_2.transplant(graft => tsplus_module_1.descriptorWith(d => new IRaceWith(() => graft(() => self, __tsplusTrace), () => graft(that, __tsplusTrace), (winner, loser) => coordinate(d.id, f, true, winner, loser), (winner, loser) => coordinate(d.id, g, false, winner, loser)), __tsplusTrace), __tsplusTrace);
}
/**
 * Sequentially zips this effect with the specified effect using the
 * specified combiner function.
 *
 * @tsplus static ets/Effect/Aspects zipWithPar
 */

export const zipWithPar = (that, f, __tsplusTrace) => self => zipWithPar_(self, that, f, __tsplusTrace);

function coordinate(fiberId, f, leftWinner, winner, loser) {
  return tsplus_module_7.flatMap_(tsplus_module_11._await(winner, fileName_1 + ":43:22"), winnerExit => tsplus_module_5.fold_(winnerExit, winnerCause => tsplus_module_7.flatMap_(tsplus_module_6.interruptAsNow_(loser, fiberId, fileName_1 + ":46:26"), loserExit => tsplus_module_5.fold_(loserExit, loserCause => leftWinner ? tsplus_module_4.failCause(() => tsplus_module_3.combinePar(winnerCause, loserCause), fileName_1 + ":49:44") : tsplus_module_4.failCause(() => tsplus_module_3.combinePar(loserCause, winnerCause), fileName_1 + ":49:89"), () => tsplus_module_4.failCause(() => winnerCause, fileName_1 + ":50:35")), fileName_1 + ":46:43"), a => tsplus_module_7.flatMap_(tsplus_module_11._await(loser, fileName_1 + ":54:20"), loserExit => tsplus_module_5.fold_(loserExit, loserCause => tsplus_module_4.failCause(() => loserCause, fileName_1 + ":56:45"), b => tsplus_module_9.zipRight_(tsplus_module_9.zipRight_(tsplus_module_8.inheritRefs(winner, fileName_1 + ":57:38"), () => tsplus_module_8.inheritRefs(loser, fileName_1 + ":57:60"), fileName_1 + ":57:42"), () => tsplus_module_10.succeed(() => f(a, b), fileName_1 + ":57:79"), fileName_1 + ":57:64")), fileName_1 + ":54:30")), fileName_1 + ":43:32");
}
//# sourceMappingURL=zipWithPar.mjs.map