"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.combineChunks = void 0;
exports.combineChunks_ = combineChunks_;

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

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

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

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

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

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Take/operations/done"));

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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; }

/**
 * Combines the chunks from this stream and the specified stream by repeatedly
 * applying the function `f` to extract a chunk using both sides and
 * conceptually "offer" it to the destination stream. `f` can maintain some
 * internal state to control the combining process, with the initial state
 * being specified by `s`.
 *
 * @tsplus fluent ets/Stream combineChunks
 */
function combineChunks_(self, that, s, f, __tsplusTrace) {
  return new _StreamInternal.StreamInternal(tsplus_module_14.unwrapScoped(tsplus_module_13.map_(tsplus_module_11.bindValue_(tsplus_module_11.bindValue_(tsplus_module_12.tap_(tsplus_module_12.tap_(tsplus_module_11.bind_(tsplus_module_11.bind_(tsplus_module_11.bind_(tsplus_module_11.bind_(tsplus_module_11.Do(), "left", () => tsplus_module_5.make(__tsplusTrace), __tsplusTrace), "right", () => tsplus_module_5.make(__tsplusTrace), __tsplusTrace), "latchL", () => tsplus_module_5.make(__tsplusTrace), __tsplusTrace), "latchR", () => tsplus_module_5.make(__tsplusTrace), __tsplusTrace), ({
    latchL,
    left
  }) => {
    void 0;
    return tsplus_module_10.fork(tsplus_module_9.runScoped(tsplus_module_8.pipeTo_(self.channel, () => producer(left, latchL, __tsplusTrace))), __tsplusTrace);
  }, __tsplusTrace), ({
    latchR,
    right
  }) => {
    const that0 = that();
    void 0;
    return tsplus_module_10.fork(tsplus_module_9.runScoped(tsplus_module_8.pipeTo_(that0.channel, () => producer(right, latchR, __tsplusTrace))), __tsplusTrace);
  }, __tsplusTrace), "pullLeft", ({
    latchL,
    left
  }) => tsplus_module_7.zipRight_(tsplus_module_5.offer(latchL, undefined, __tsplusTrace), () => tsplus_module_3.flatMap_(tsplus_module_5.take(left, __tsplusTrace), take => tsplus_module_6.done(take, __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), "pullRight", ({
    latchR,
    right
  }) => tsplus_module_7.zipRight_(tsplus_module_5.offer(latchR, undefined, __tsplusTrace), () => tsplus_module_3.flatMap_(tsplus_module_5.take(right, __tsplusTrace), take => tsplus_module_6.done(take, __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), ({
    pullLeft,
    pullRight
  }) => {
    const stream = tsplus_module_4.unfoldChunkEffect(s, s => tsplus_module_3.flatMap_(f(s, pullLeft, pullRight), exit => tsplus_module_2.unsome(tsplus_module_1.done(() => exit, __tsplusTrace), __tsplusTrace), __tsplusTrace));
    void 0;
    return stream.channel;
  }, __tsplusTrace)));
}
/**
 * Combines the chunks from this stream and the specified stream by repeatedly
 * applying the function `f` to extract a chunk using both sides and
 * conceptually "offer" it to the destination stream. `f` can maintain some
 * internal state to control the combining process, with the initial state
 * being specified by `s`.
 *
 * @tsplus static ets/Stream/Aspects combineChunks
 */


const combineChunks = (that, s, f, __tsplusTrace) => self => combineChunks_(self, that, s, f, __tsplusTrace);

exports.combineChunks = combineChunks;

function producer(handoff, latch, __tsplusTrace) {
  return tsplus_module_17.zipRight_(tsplus_module_15.fromEffect(() => tsplus_module_5.take(latch, __tsplusTrace)), () => tsplus_module_20.readWithCause(chunk => tsplus_module_17.zipRight_(tsplus_module_15.fromEffect(() => tsplus_module_5.offer(handoff, tsplus_module_16.chunk(chunk), __tsplusTrace)), () => producer(handoff, latch, __tsplusTrace)), cause => tsplus_module_15.fromEffect(() => tsplus_module_5.offer(handoff, tsplus_module_18.failCause(cause), __tsplusTrace)), () => tsplus_module_17.zipRight_(tsplus_module_15.fromEffect(() => tsplus_module_5.offer(handoff, tsplus_module_19.end, __tsplusTrace)), () => producer(handoff, latch, __tsplusTrace))));
}
//# sourceMappingURL=combineChunks.js.map