"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.peel = exports.Halt = exports.End = exports.Emit = void 0;
exports.peel_ = peel_;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/tapErrorCause"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/runScoped"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/forkScoped"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatMap"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/map"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/write"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/zipRight"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/failCause"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/unit"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/_internal/Handoff"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/unwrap"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Sink/operations/fromEffect"));

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Sink/operations/fail"));

var tsplus_module_15 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Sink/operations/zipRight"));

var tsplus_module_16 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/fromEffect"));

var tsplus_module_17 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Channel/operations/readWithCause"));

var tsplus_module_18 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Sink/operations/exposeLeftover"));

var tsplus_module_19 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Sink/operations/foldSink"));

var tsplus_module_20 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Deferred/operations/make"));

var tsplus_module_21 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatten"));

var _SinkInternal = /*#__PURE__*/require("@effect/core/stream/Sink/operations/_internal/SinkInternal");

var _StreamInternal = /*#__PURE__*/require("@effect/core/stream/Stream/operations/_internal/StreamInternal");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

class Emit {
  constructor(elements) {
    this.elements = elements;
    this._tag = "Emit";
  }

}

exports.Emit = Emit;

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

}

exports.Halt = Halt;

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
 */


exports.End = End;

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.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.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
 */


const peel = (sink, __tsplusTrace) => self => peel_(self, sink, __tsplusTrace);

exports.peel = peel;
//# sourceMappingURL=peel.js.map