import * as tsplus_module_1 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_3 from "@tsplus/stdlib/collections/Chunk/append";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_5 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/suspendSucceed";
import { concreteChunkId } from "@tsplus/stdlib/collections/Chunk/definition";
/**
 * Takes all elements so long as the effectual predicate returns true.
 *
 * @tsplus fluent Chunk takeWhileEffect
 */

export function takeWhileEffect_(self, f, __tsplusTrace) {
  return tsplus_module_6.suspendSucceed(() => {
    const iterator = self._arrayLikeIterator();

    let next;
    let taking = tsplus_module_1.succeedNow(true, __tsplusTrace);
    let builder = tsplus_module_2.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 = tsplus_module_5.flatMap_(taking, d => tsplus_module_4.map_(d ? f(a) : tsplus_module_1.succeedNow(false, __tsplusTrace), b => {
          if (b) {
            builder = tsplus_module_3.append_(builder, a);
          }

          return b;
        }, __tsplusTrace), __tsplusTrace);
        i++;
      }
    }

    return tsplus_module_4.map_(taking, () => builder, __tsplusTrace);
  }, __tsplusTrace);
}
/**
 * Takes all elements so long as the effectual predicate returns true.
 *
 * @tsplus static Chunk/Aspects takeWhileEffect
 */

export const takeWhileEffect = (f, __tsplusTrace) => self => takeWhileEffect_(self, f, __tsplusTrace);
//# sourceMappingURL=takeWhileEffect.mjs.map