"use strict";

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

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

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/empty"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/AtomicBoolean"));

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

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

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

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

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

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

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/isEmpty"));

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

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

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

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/single"));

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

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

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

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

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

var tsplus_module_20 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/isNonEmpty"));

var tsplus_module_21 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/filter"));

var tsplus_module_22 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/concat"));

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

/**
 * Applies the transducer to the stream and emits its outputs.
 *
 * @tsplus fluent ets/Stream transduce
 */
function transduce_(self, sink, __tsplusTrace) {
  void 0;
  return new _StreamInternal.StreamInternal(tsplus_module_12.suspend(() => {
    const sink0 = sink();
    void 0;
    const leftovers = new tsplus_module_1.AtomicReference(tsplus_module_2.empty());
    const upstreamDone = new tsplus_module_3.AtomicBoolean(false);
    const upstreamMarker = tsplus_module_9.readWith(chunk => tsplus_module_5.zipRight_(tsplus_module_4.write(() => chunk), () => upstreamMarker), err => tsplus_module_6.fail(() => err), done => tsplus_module_5.zipRight_(tsplus_module_7.succeed(() => upstreamDone.set(true)), () => tsplus_module_8.succeedNow(done)));
    const buffer = tsplus_module_12.suspend(() => {
      const leftover = leftovers.get;

      if (tsplus_module_10.isEmpty(leftover)) {
        return tsplus_module_9.readWith(chunk => tsplus_module_5.zipRight_(tsplus_module_4.write(() => chunk), () => buffer), err => tsplus_module_6.fail(() => err), done => tsplus_module_8.succeedNow(done));
      }

      leftovers.set(tsplus_module_2.empty());
      return tsplus_module_5.zipRight_(tsplus_module_11.writeChunk(leftover), () => buffer);
    });
    const transducer = tsplus_module_16.flatMap_(tsplus_module_17.doneCollect(sink0.channel), ({
      tuple: [leftover, z]
    }) => tsplus_module_16.flatMap_(tsplus_module_7.succeed(() => tsplus_module_15.make(upstreamDone.get, concatAndGet(leftovers, leftover, __tsplusTrace))), ({
      tuple: [done, newLeftovers]
    }) => {
      const nextChannel = done && tsplus_module_10.isEmpty(newLeftovers) ? tsplus_module_13.unit : transducer;
      return tsplus_module_5.zipRight_(tsplus_module_4.write(() => tsplus_module_14.single(z)), () => nextChannel);
    }));
    return tsplus_module_18.pipeTo_(tsplus_module_18.pipeTo_(self.channel, () => upstreamMarker), () => tsplus_module_19.pipeToOrFail_(buffer, () => transducer));
  }));
}
/**
 * Creates a pipeline that repeatedly sends all elements through the given
 * sink.
 *
 * @tsplus static ets/Stream/Aspects fromSink
 */


const transduce = (sink, __tsplusTrace) => self => transduce_(self, sink, __tsplusTrace);

exports.transduce = transduce;

function concatAndGet(leftovers, chunk, __tsplusTrace) {
  const ls = leftovers.get;
  const concat = tsplus_module_22.concat_(ls, tsplus_module_21.filter_(chunk, c => tsplus_module_20.isNonEmpty(c)));
  leftovers.set(concat);
  return concat;
}
//# sourceMappingURL=transduce.js.map