"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.zipWithPar = void 0;
exports.zipWithPar_ = zipWithPar_;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/descriptorWith"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/transplant"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Cause/definition"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/failCause"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Exit/operations/fold"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Fiber/operations/interruptAs"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatMap"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Fiber/operations/inheritRefs"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/zipRight"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/succeed"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Fiber/operations/await"));

var _primitives = /*#__PURE__*/require("@effect/core/io/Effect/definition/primitives");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const fileName_1 = "(@effect/core) _src/io/Effect/operations/zipWithPar.ts";

/**
 * Sequentially zips this effect with the specified effect using the
 * specified combiner function.
 *
 * @tsplus fluent ets/Effect zipWithPar
 */
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 _primitives.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
 */


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

exports.zipWithPar = zipWithPar;

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.js.map