import * as tsplus_module_1 from "@effect/core/stream/Stream/operations/empty";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/Chunk/forEach";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/unit";
import * as tsplus_module_6 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/readWith";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_9 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_11 from "@effect/core/stream/Channel/operations/unwrap";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
import { RingBufferNew } from "@effect/core/support/RingBufferNew";
/**
 * Takes the last specified number of elements from this stream.
 *
 * @tsplus fluent ets/Stream takeRight
 */

export function takeRight_(self, n, __tsplusTrace) {
  if (n <= 0) {
    return tsplus_module_1.empty;
  }

  void 0;
  return new StreamInternal(tsplus_module_11.unwrap(() => tsplus_module_10.map_(tsplus_module_9.succeed(() => new RingBufferNew(n), __tsplusTrace), queue => {
    const reader = tsplus_module_7.readWith(input => {
      tsplus_module_2.forEach_(input, a => queue.put(a));
      return reader;
    }, err => tsplus_module_3.fail(() => err), () => tsplus_module_6.zipRight_(tsplus_module_4.write(() => queue.toChunk()), () => tsplus_module_5.unit));
    return tsplus_module_8.pipeTo_(self.channel, () => reader);
  }, __tsplusTrace)));
}
/**
 * Takes the last specified number of elements from this stream.
 *
 * @tsplus static ets/Stream/Aspects takeRight
 */

export const takeRight = (n, __tsplusTrace) => self => takeRight_(self, n, __tsplusTrace);
//# sourceMappingURL=takeRight.mjs.map