// tracing: off
import * as A from "../../Collections/Immutable/Chunk"
import * as O from "../../Option"
import * as BP from "../../Stream/BufferedPull"
import * as T from "../_internal/effect"
import * as M from "../_internal/managed"
import { Stream } from "./definitions"
/**
 * Effectfully filters the elements emitted by this stream.
 */

export function filterM_(self, f) {
  return new Stream(
    M.map_(M.mapM_(self.proc, BP.make), (os) => {
      const pull = T.chain_(BP.pullElement(os), (o) =>
        T.chain_(
          T.mapError_(f(o), (v) => O.some(v)),
          (_) => {
            if (_) {
              return T.succeed(A.single(o))
            } else {
              return pull
            }
          }
        )
      )
      return pull
    })
  )
}
/**
 * Effectfully filters the elements emitted by this stream.
 */

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