import * as tsplus_module_1 from "@tsplus/stdlib/collections/Chunk/collect";
import * as tsplus_module_2 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_3 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/unit";
import * as tsplus_module_6 from "@effect/core/stream/Channel/operations/readWith";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_8 from "@effect/core/stream/Stream/operations/succeed";
import * as tsplus_module_9 from "@effect/core/stream/Stream/operations/flatMap";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
import { RingBufferNew } from "@effect/core/support/RingBufferNew";
/**
 * Drops the last specified number of elements from this stream.
 *
 * Note: this combinator keeps `n` elements in memory. Be careful with big
 * numbers.
 *
 * @tsplus fluent ets/Stream dropRight
 */

export function dropRight_(self, n, __tsplusTrace) {
  if (n <= 0) {
    return self;
  }

  return tsplus_module_9.flatMap_(tsplus_module_8.succeed(() => new RingBufferNew(n), __tsplusTrace), queue => {
    const reader = tsplus_module_6.readWith(chunk => {
      const outs = tsplus_module_1.collect_(chunk, elem => {
        const head = queue.head();
        queue.put(elem);
        return head;
      });
      return tsplus_module_3.zipRight_(tsplus_module_2.write(() => outs), () => reader);
    }, err => tsplus_module_4.fail(() => err), () => tsplus_module_5.unit);
    void 0;
    return new StreamInternal(tsplus_module_7.pipeTo_(self.channel, () => reader));
  }, __tsplusTrace);
}
/**
 * Drops the last specified number of elements from this stream.
 *
 * Note: this combinator keeps `n` elements in memory. Be careful with big
 * numbers.
 *
 * @tsplus static ets/Stream/Aspects dropRight
 */

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