import * as tsplus_module_1 from "@tsplus/stdlib/collections/Chunk/definition";
import * as tsplus_module_2 from "@effect/core/io/Cause/errors";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/die";
import * as tsplus_module_4 from "@tsplus/stdlib/collections/Chunk/unsafeHead";
import * as tsplus_module_5 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_6 from "@tsplus/stdlib/collections/Chunk/unsafeTail";
import * as tsplus_module_7 from "@effect/core/io/Effect/operations/orElse";
import * as tsplus_module_8 from "@tsplus/stdlib/collections/Chunk/reduce";
import * as tsplus_module_9 from "@effect/core/io/Effect/operations/suspendSucceed";
/**
 * Returns an effect that runs this effect and in case of failure, runs each
 * of the specified effects in order until one of them succeeds.
 *
 * @tsplus static ets/Effect/Ops firstSuccessOf
 */

export function firstSuccessOf(effects, __tsplusTrace) {
  return tsplus_module_9.suspendSucceed(() => {
    const chunk = tsplus_module_1.from(effects);

    if (chunk.length <= 0) {
      return tsplus_module_3.die(() => new tsplus_module_2.IllegalArgumentException(`received empty collection of effects`), __tsplusTrace);
    }

    const head = tsplus_module_4.unsafeHead(chunk);
    const tail = chunk.length === 1 ? tsplus_module_5.empty() : tsplus_module_6.unsafeTail(chunk);
    return tsplus_module_8.reduce_(tail, head, (b, a) => tsplus_module_7.orElse_(b, () => a, __tsplusTrace));
  }, __tsplusTrace);
}
//# sourceMappingURL=firstSuccessOf.mjs.map