const fileName_1 = "(@effect/core) _src/io/Effect/operations/raceAll.ts";
import * as tsplus_module_1 from "@effect/core/io/Effect/operations/unit";
import * as tsplus_module_2 from "@effect/core/io/Fiber/operations/interrupt";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/zipLeft";
import * as tsplus_module_4 from "@tsplus/stdlib/collections/Chunk/reduce";
import * as tsplus_module_5 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/interruption";
import * as tsplus_module_7 from "@effect/core/io/Fiber/operations/inheritRefs";
import * as tsplus_module_8 from "@effect/core/io/Effect/operations/as";
import * as tsplus_module_9 from "@effect/core/io/Fiber/operations/await";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/fork";
import * as tsplus_module_11 from "@effect/core/io/Effect/operations/zipRight";
import * as tsplus_module_12 from "@tsplus/stdlib/collections/Chunk/prepend";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/excl-forEach";
import * as tsplus_module_14 from "@tsplus/stdlib/collections/Chunk/size";
import * as tsplus_module_15 from "@effect/core/io/Ref/operations/make";
import * as tsplus_module_16 from "@effect/core/io/Deferred/operations/make";
import * as tsplus_module_17 from "@tsplus/stdlib/collections/Chunk/definition";
import * as tsplus_module_18 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_19 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_20 from "@effect/core/io/Effect/operations/flatten";
import * as tsplus_module_21 from "@effect/core/io/Exit/operations/foldEffect";
/**
 * 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
 */

export 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
 */

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

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