import * as tracing_1 from "../../../../Tracing"
const fileName_1 =
  "(@effect-ts/system): _src/Collections/Immutable/Chunk/api/takeWhileM.ts"
import * as core from "../../../../Effect/core"
import * as coreMap from "../../../../Effect/map"
import * as Chunk from "../core"
import { concreteId } from "../definition"
/**
 * Takes all elements so long as the effectual predicate returns true.
 */

export function takeWhileM_(self, f) {
  return core.suspend(() => {
    const iterator = self.arrayLikeIterator()
    let next
    let taking = core.succeed(true, fileName_1 + ":17:53")
    let builder = Chunk.empty()

    while ((next = iterator.next()) && !next.done) {
      const array = next.value
      const len = array.length
      let i = 0

      while (i < len) {
        const a = array[i]
        taking = core.chain_(
          taking,
          (d) =>
            coreMap.map_(
              d ? f(a) : core.succeed(false, fileName_1 + ":27:47"),
              (b) => {
                if (b) {
                  builder = Chunk.append_(builder, a)
                }

                return b
              },
              fileName_1 + ":27:23"
            ),
          fileName_1 + ":26:29"
        )
        i++
      }

      next = iterator.next()
    }

    return coreMap.map_(taking, () => builder, fileName_1 + ":38:24")
  }, fileName_1 + ":14:22")
}
/**
 * Takes all elements so long as the effectual predicate returns true.
 *
 * @dataFirst takeWhileM_
 */

export function takeWhileM(f) {
  return (self) => takeWhileM_(self, f)
}
//# sourceMappingURL=takeWhileM.js.map
