import { pipe } from "../../Function"
import * as O from "../../Option"
import * as BP from "../../Stream/BufferedPull"
import * as P 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"
export function flattenExitOption(self) {
  return new Stream(
    M.map_(
      M.bind_(
        M.bind_(M.do, "upstream", () => M.mapM_(self.proc, BP.make)),
        "done",
        () => T.toManaged(Ref.makeRef(false))
      ),
      ({ done, upstream }) =>
        T.chain_(done.get, (_) => {
          if (_) {
            return P.end
          } else {
            return T.foldM_(
              BP.pullElement(upstream),
              O.fold(
                () => T.zipRight_(done.set(true), P.end),
                (e) => P.fail(e)
              ),
              (os) =>
                T.foldM_(
                  T.done(os),
                  O.fold(
                    () => T.zipRight_(done.set(true), P.end),
                    (e) => P.fail(e)
                  ),
                  (_) => P.emit(_)
                )
            )
          }
        })
    )
  )
}
//# sourceMappingURL=flattenExitOption.js.map
