// ets_tracing: off
import * as A from "../../Collections/Immutable/Chunk";
import { pipe } from "../../Function";
import * as O from "../../Option";
import * as BP from "../../Stream/BufferedPull";
import * as Pull from "../../Stream/Pull";
import * as T from "../_internal/effect";
import * as M from "../_internal/managed";
import * as Ref from "../_internal/ref";
import { Stream } from "./definitions";
/**
 * Effectfully transforms all elements of the stream for as long as the specified partial function is defined.
 */

export function collectWhileM_(self, pf) {
  return new Stream(M.map_(M.let_(M.bind_(M.bind_(M.do, "as", () => M.mapM_(self.proc, BP.make)), "done", () => T.toManaged(Ref.makeRef(false))), "pull", ({
    as,
    done
  }) => T.chain_(done.get, _ => {
    if (_) {
      return Pull.end;
    } else {
      return T.chain_(BP.pullElement(as), a => O.fold_(pf(a), () => T.zipRight_(done.set(true), Pull.end), v => T.bimap_(v, O.some, A.single)));
    }
  })), ({
    pull
  }) => pull));
}
/**
 * Effectfully transforms all elements of the stream for as long as the specified partial function is defined.
 */

export function collectWhileM(pf) {
  return self => collectWhileM_(self, pf);
}
//# sourceMappingURL=collectWhileM.js.map