import * as tracing_1 from "../../../../Tracing"
import * as tracing_2 from "../../../../Tracing"
const fileName_1 =
  "(@effect-ts/system): _src/Collections/Immutable/Chunk/api/reduceWhileM.ts"
import * as core from "../../../../Effect/core"
import { concreteId } from "../definition"

function loop(s, iterator, array, i, length, pred, f) {
  if (i < length) {
    if (pred(s)) {
      return core.chain_(
        f(s, array[i]),
        (s1) => loop(s1, iterator, array, i + 1, length, pred, f),
        fileName_1 + ":17:25"
      )
    } else {
      return core.succeed(s, fileName_1 + ":21:26")
    }
  } else {
    const next = iterator.next()

    if (next.done) {
      return core.succeed(s, fileName_1 + ":27:26")
    } else {
      const arr = next.value
      return core.suspend(
        () => loop(s, iterator, arr, 0, arr.length, pred, f),
        fileName_1 + ":30:26"
      )
    }
  }
}
/**
 * Folds over the elements in this chunk from the left.
 * Stops the fold early when the condition is not fulfilled.
 */

export function reduceWhileM_(self, s, pred, f) {
  const iterator = self.arrayLikeIterator()
  const next = iterator.next()

  if (next.done) {
    return core.succeed(s, fileName_1 + ":49:24")
  } else {
    const array = next.value
    const length = array.length
    return loop(s, iterator, array, 0, length, pred, f)
  }
}
/**
 * Folds over the elements in this chunk from the left.
 * Stops the fold early when the condition is not fulfilled.
 *
 * @dataFirst reduceWhileM_
 */

export function reduceWhileM(s, pred, f) {
  return (self) => reduceWhileM_(self, s, pred, f)
}
//# sourceMappingURL=reduceWhileM.js.map
