"use strict";

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

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

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

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

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/reduce"));

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

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

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

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

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

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

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

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/prepend"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/excl-forEach"));

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/size"));

var tsplus_module_15 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Ref/operations/make"));

var tsplus_module_16 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Deferred/operations/make"));

var tsplus_module_17 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/definition"));

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

var tsplus_module_19 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

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

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

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/raceAll.ts";

/**
 * Returns an effect that races this effect with all the specified effects,
 * yielding the value of the first effect to succeed with a value. Losers of
 * the race will be interrupted immediately
 *
 * @tsplus fluent ets/Effect raceAll
 */
function raceAll_(self, effects, __tsplusTrace) {
  return tsplus_module_5.flatMap_(tsplus_module_18.succeed(() => tsplus_module_17.from(effects()), __tsplusTrace), ios => tsplus_module_5.flatMap_(tsplus_module_16.make(__tsplusTrace), done => tsplus_module_5.flatMap_(tsplus_module_15.makeRef(() => tsplus_module_14.size(ios), __tsplusTrace), fails => tsplus_module_6.uninterruptibleMask(({
    restore
  }) => tsplus_module_5.flatMap_(tsplus_module_10.fork(tsplus_module_6.interruptible(self, __tsplusTrace), __tsplusTrace), head => tsplus_module_5.flatMap_(tsplus_module_13.forEach(() => ios, io => tsplus_module_10.fork(tsplus_module_6.interruptible(io, __tsplusTrace), __tsplusTrace), __tsplusTrace), tail => {
    const fs = tsplus_module_12.prepend_(tail, head);
    return tsplus_module_5.flatMap_(tsplus_module_4.reduce_(fs, tsplus_module_1.unit, (io, fiber) => tsplus_module_11.zipRight_(io, () => tsplus_module_10.fork(tsplus_module_5.flatMap_(tsplus_module_9._await(fiber, __tsplusTrace), arbiter(fs, fiber, done, fails), __tsplusTrace), __tsplusTrace), __tsplusTrace)), () => {
      const inheritRefs = res => tsplus_module_8.as_(tsplus_module_7.inheritRefs(res.get(1), __tsplusTrace), () => res.get(0), __tsplusTrace);

      return tsplus_module_6.onInterrupt_(restore(() => tsplus_module_5.flatMap_(done.await(__tsplusTrace), inheritRefs, __tsplusTrace), __tsplusTrace), () => tsplus_module_4.reduce_(fs, tsplus_module_1.unit, (io, fiber) => tsplus_module_3.zipLeft_(io, () => tsplus_module_2.interrupt(fiber, __tsplusTrace), __tsplusTrace)), __tsplusTrace);
    }, __tsplusTrace);
  }, __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace);
}
/**
 * Returns an effect that races this effect with all the specified effects,
 * yielding the value of the first effect to succeed with a value. Losers of
 * the race will be interrupted immediately
 *
 * @tsplus static ets/Effect/Aspects raceAll
 */


const raceAll = (effects, __tsplusTrace) => self => raceAll_(self, effects, __tsplusTrace);

exports.raceAll = raceAll;

function arbiter(fibers, winner, promise, fails) {
  return exit => {
    return tsplus_module_21.foldEffect_(exit, e => tsplus_module_20.flattenNow(fails.modify(c => tsplus_module_19.make(c === 0 ? tsplus_module_1.unit_(promise.failCause(() => e, fileName_1 + ":56:59"), fileName_1 + ":56:67") : tsplus_module_1.unit, c - 1), fileName_1 + ":56:18"), fileName_1 + ":57:19"), a => tsplus_module_5.flatMap_(promise.succeed(() => tsplus_module_19.make(a, winner), fileName_1 + ":60:19"), set => set ? tsplus_module_4.reduce_(fibers, tsplus_module_1.unit, (io, fiber) => fiber === winner ? io : tsplus_module_3.zipLeft_(io, () => tsplus_module_2.interrupt(fiber, fileName_1 + ":63:109"), fileName_1 + ":63:93")) : tsplus_module_1.unit, fileName_1 + ":61:19"), fileName_1 + ":53:27");
  };
}
//# sourceMappingURL=raceAll.js.map