import * as tsplus_module_1 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_2 from "@effect/core/stream/Stream/operations/tapErrorCause";
import * as tsplus_module_3 from "@effect/core/stream/Stream/operations/runScoped";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/forkScoped";
import * as tsplus_module_5 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/failCause";
import * as tsplus_module_10 from "@effect/core/stream/Channel/operations/unit";
import * as tsplus_module_11 from "@effect/core/stream/Stream/operations/_internal/Handoff";
import * as tsplus_module_12 from "@effect/core/stream/Channel/operations/unwrap";
import * as tsplus_module_13 from "@effect/core/stream/Sink/operations/fromEffect";
import * as tsplus_module_14 from "@effect/core/stream/Sink/operations/fail";
import * as tsplus_module_15 from "@effect/core/stream/Sink/operations/zipRight";
import * as tsplus_module_16 from "@effect/core/stream/Channel/operations/fromEffect";
import * as tsplus_module_17 from "@effect/core/stream/Channel/operations/readWithCause";
import * as tsplus_module_18 from "@effect/core/stream/Sink/operations/exposeLeftover";
import * as tsplus_module_19 from "@effect/core/stream/Sink/operations/foldSink";
import * as tsplus_module_20 from "@effect/core/io/Deferred/operations/make";
import * as tsplus_module_21 from "@effect/core/io/Effect/operations/flatten";
import { SinkInternal } from "@effect/core/stream/Sink/operations/_internal/SinkInternal";
import { StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
export class Emit {
  constructor(elements) {
    this.elements = elements;
    this._tag = "Emit";
  }

}
export class Halt {
  constructor(error) {
    this.error = error;
    this._tag = "Halt";
  }

}
export class End {
  constructor() {
    this._tag = "End";
  }

}
/**
 * Peels off enough material from the stream to construct a `Z` using the
 * provided `Sink` and then returns both the `Z` and the rest of the
 * `Stream` in a scope. Like all scoped values, the provided stream is valid
 * only within the scope.
 *
 * @tsplus fluent ets/Stream peel
 */

export function peel_(self, sink, __tsplusTrace) {
  return tsplus_module_21.flattenNow(tsplus_module_5.flatMap_(tsplus_module_20.make(__tsplusTrace), deferred => tsplus_module_6.map_(tsplus_module_11.make(__tsplusTrace), handoff => {
    const consumer = tsplus_module_19.foldSink_(tsplus_module_18.exposeLeftover(sink(), __tsplusTrace), e => tsplus_module_15.zipRight_(tsplus_module_13.fromEffect(() => deferred.fail(() => e, __tsplusTrace), __tsplusTrace), () => tsplus_module_14.fail(() => e, __tsplusTrace), __tsplusTrace), ({
      tuple: [z1, leftovers]
    }) => {
      const loop = tsplus_module_17.readWithCause(chunk => tsplus_module_8.zipRight_(tsplus_module_16.fromEffect(() => tsplus_module_11.offer(handoff, new Emit(chunk), __tsplusTrace)), () => loop), cause => tsplus_module_8.zipRight_(tsplus_module_16.fromEffect(() => tsplus_module_11.offer(handoff, new Halt(cause), __tsplusTrace)), () => tsplus_module_9.failCause(() => cause)), () => tsplus_module_8.zipRight_(tsplus_module_16.fromEffect(() => tsplus_module_11.offer(handoff, new End(), __tsplusTrace)), () => tsplus_module_10.unit));
      return new SinkInternal(tsplus_module_8.zipRight_(tsplus_module_8.zipRight_(tsplus_module_16.fromEffect(() => deferred.succeed(() => z1, __tsplusTrace)), () => tsplus_module_16.fromEffect(() => tsplus_module_11.offer(handoff, new Emit(leftovers), __tsplusTrace))), () => loop));
    }, __tsplusTrace);
    const producer = tsplus_module_12.unwrap(() => tsplus_module_6.map_(tsplus_module_11.take(handoff, __tsplusTrace), signal => {
      switch (signal._tag) {
        case "Emit":
          {
            return tsplus_module_8.zipRight_(tsplus_module_7.write(() => signal.elements), () => producer);
          }

        case "Halt":
          {
            return tsplus_module_9.failCause(() => signal.error);
          }

        case "End":
          {
            return tsplus_module_10.unit;
          }
      }
    }, __tsplusTrace));
    return tsplus_module_6.map_(tsplus_module_5.flatMap_(tsplus_module_4.forkScoped(tsplus_module_3.runScoped_(tsplus_module_2.tapErrorCause_(self, cause => deferred.failCause(() => cause, __tsplusTrace)), () => consumer, __tsplusTrace), __tsplusTrace), () => deferred.await(__tsplusTrace), __tsplusTrace), z => tsplus_module_1.make(z, new StreamInternal(producer)), __tsplusTrace);
  }, __tsplusTrace), __tsplusTrace), __tsplusTrace);
}
/**
 * Peels off enough material from the stream to construct a `Z` using the
 * provided `Sink` and then returns both the `Z` and the rest of the
 * `Stream` in a scope. Like all scoped values, the provided stream is valid
 * only within the scope.
 *
 * @tsplus static ets/Stream/Aspects peel
 */

export const peel = (sink, __tsplusTrace) => self => peel_(self, sink, __tsplusTrace);
//# sourceMappingURL=peel.mjs.map