// tracing: off
import * as A from "../../Collections/Immutable/Chunk"
import { pipe } from "../../Function"
import * as O from "../../Option"
import * as Q from "../../Queue"
import * as T from "../_internal/effect"
import * as M from "../_internal/managed"
import * as Ref from "../_internal/ref"
import * as BP from "../BufferedPull"
import * as Pull from "../Pull"
import { Stream } from "./definitions"
import { empty } from "./empty"
/**
 * Takes the last specified number of elements from this stream.
 */

export function takeRight_(self, n) {
  if (n <= 0) {
    return empty
  } else {
    return new Stream(
      M.map_(
        M.bind_(
          M.bind_(
            M.bind_(M.do, "pull", () => M.mapM_(self.proc, BP.make)),
            "queue",
            () => T.toManaged(Q.makeSliding(n))
          ),
          "done",
          () => Ref.makeManagedRef(false)
        ),
        ({ done, pull, queue }) =>
          T.chain_(done.get, (_) => {
            if (_) {
              return Pull.end
            } else {
              return T.catchSome_(
                T.as_(
                  T.tap_(BP.pullElement(pull), (x) => Q.offer_(queue, x)),
                  A.empty()
                ),
                O.fold(
                  () => O.some(T.zipRight_(done.set(true), Q.takeAll(queue))),
                  () => O.none
                )
              )
            }
          })
      )
    )
  }
}
/**
 * Takes the last specified number of elements from this stream.
 */

export function takeRight(n) {
  return (self) => takeRight_(self, n)
}
//# sourceMappingURL=takeRight.js.map
