import * as tsplus_module_1 from "@effect/core/io/Fiber/operations/inheritRefs";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_3 from "@effect/core/io/Fiber/operations/await";
import { IRaceWith } from "@effect/core/io/Effect/definition/primitives";
/**
 * Returns an effect that races this effect with the specified effect, calling
 * the specified finisher as soon as one result or the other has been computed.
 *
 * @tsplus fluent ets/Effect raceWith
 */

export function raceWith_(self, that, leftDone, rightDone, __tsplusTrace) {
  return new IRaceWith(() => self, that, (winner, loser) => tsplus_module_2.flatMap_(tsplus_module_3._await(winner, __tsplusTrace), exit => {
    switch (exit._tag) {
      case "Success":
        {
          return tsplus_module_2.flatMap_(tsplus_module_1.inheritRefs(winner, __tsplusTrace), () => leftDone(exit, loser), __tsplusTrace);
        }

      case "Failure":
        {
          return leftDone(exit, loser);
        }
    }
  }, __tsplusTrace), (winner, loser) => tsplus_module_2.flatMap_(tsplus_module_3._await(winner, __tsplusTrace), exit => {
    switch (exit._tag) {
      case "Success":
        {
          return tsplus_module_2.flatMap_(tsplus_module_1.inheritRefs(winner, __tsplusTrace), () => rightDone(exit, loser), __tsplusTrace);
        }

      case "Failure":
        {
          return rightDone(exit, loser);
        }
    }
  }, __tsplusTrace), __tsplusTrace);
}
/**
 * Returns an effect that races this effect with the specified effect, calling
 * the specified finisher as soon as one result or the other has been computed.
 *
 * @tsplus static ets/Effect/Aspects raceWith
 */

export const raceWith = (that, leftDone, rightDone, __tsplusTrace) => self => raceWith_(self, that, leftDone, rightDone, __tsplusTrace);
//# sourceMappingURL=raceWith.mjs.map