import * as tsplus_module_1 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/yieldNow";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/zipRight";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_5 from "@effect/core/io/Effect/operations/suspendSucceed";
/**
 * Returns a new effect that repeats this effect the specified number of times
 * or until the first failure. Repeats are in addition to the first execution,
 * so that `io.repeatN(1)` yields an effect that executes `io`, and then if
 * that succeeds, executes `io` an additional time.
 *
 * @tsplus fluent ets/Effect repeatN
 */

export function repeatN_(self, n, __tsplusTrace) {
  return tsplus_module_5.suspendSucceed(() => {
    function loop(n) {
      return tsplus_module_4.flatMap_(self, a => n <= 0 ? tsplus_module_1.succeedNow(a, __tsplusTrace) : tsplus_module_3.zipRight_(tsplus_module_2.yieldNow, () => loop(n - 1), __tsplusTrace), __tsplusTrace);
    }

    return loop(n);
  }, __tsplusTrace);
}
/**
 * Returns a new effect that repeats this effect the specified number of times
 * or until the first failure. Repeats are in addition to the first execution,
 * so that `io.repeatN(1)` yields an effect that executes `io`, and then if
 * that succeeds, executes `io` an additional time.
 *
 * @tsplus static ets/Effect/Aspects repeatN
 */

export const repeatN = (n, __tsplusTrace) => self => repeatN_(self, n, __tsplusTrace);
//# sourceMappingURL=repeatN.mjs.map