import * as tsplus_module_1 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/suspendSucceed";
import { concreteChunkId } from "@tsplus/stdlib/collections/Chunk/definition";
/**
 * Folds over the elements in this chunk from the left.
 * Stops the fold early when the condition is not fulfilled.
 *
 * @tsplus fluent Chunk reduceWhileEffect
 */

export function reduceWhileEffect_(self, s, pred, f, __tsplusTrace) {
  const iterator = self._arrayLikeIterator();

  const next = iterator.next();

  if (next.done) {
    return tsplus_module_1.succeedNow(s, __tsplusTrace);
  } else {
    const array = next.value;
    const length = array.length;
    return loop(s, iterator, array, 0, length, pred, f, __tsplusTrace);
  }
}
/**
 * Folds over the elements in this chunk from the left.
 * Stops the fold early when the condition is not fulfilled.
 *
 * @tsplus static Chunk/Aspects reduceWhileEffect
 */

export const reduceWhileEffect = (s, pred, f, __tsplusTrace) => self => reduceWhileEffect_(self, s, pred, f, __tsplusTrace);

function loop(s, iterator, array, i, length, pred, f, __tsplusTrace) {
  if (i < length) {
    if (pred(s)) {
      return tsplus_module_2.flatMap_(f(s, array[i]), s1 => loop(s1, iterator, array, i + 1, length, pred, f, __tsplusTrace), __tsplusTrace);
    } else {
      return tsplus_module_1.succeedNow(s, __tsplusTrace);
    }
  } else {
    const next = iterator.next();

    if (next.done) {
      return tsplus_module_1.succeedNow(s, __tsplusTrace);
    } else {
      const arr = next.value;
      return tsplus_module_3.suspendSucceed(() => loop(s, iterator, arr, 0, arr.length, pred, f, __tsplusTrace), __tsplusTrace);
    }
  }
}
//# sourceMappingURL=reduceWhileEffect.mjs.map