import * as tsplus_module_1 from "@tsplus/stdlib/data/AtomicReference";
import * as tsplus_module_2 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_3 from "@tsplus/stdlib/data/AtomicBoolean";
import * as tsplus_module_4 from "@effect/core/stream/Channel/operations/write";
import * as tsplus_module_5 from "@effect/core/stream/Channel/operations/zipRight";
import * as tsplus_module_6 from "@effect/core/stream/Channel/operations/fail";
import * as tsplus_module_7 from "@effect/core/stream/Channel/operations/succeed";
import * as tsplus_module_8 from "@effect/core/stream/Channel/operations/succeedNow";
import * as tsplus_module_9 from "@effect/core/stream/Channel/operations/readWith";
import * as tsplus_module_10 from "@tsplus/stdlib/collections/Chunk/isEmpty";
import * as tsplus_module_11 from "@effect/core/stream/Channel/operations/writeChunk";
import * as tsplus_module_12 from "@effect/core/stream/Channel/operations/suspend";
import * as tsplus_module_13 from "@effect/core/stream/Channel/operations/unit";
import * as tsplus_module_14 from "@tsplus/stdlib/collections/Chunk/single";
import * as tsplus_module_15 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_16 from "@effect/core/stream/Channel/operations/flatMap";
import * as tsplus_module_17 from "@effect/core/stream/Channel/operations/doneCollect";
import * as tsplus_module_18 from "@effect/core/stream/Channel/operations/pipeTo";
import * as tsplus_module_19 from "@effect/core/stream/Channel/operations/pipeToOrFail";
import * as tsplus_module_20 from "@tsplus/stdlib/collections/Chunk/isNonEmpty";
import * as tsplus_module_21 from "@tsplus/stdlib/collections/Chunk/filter";
import * as tsplus_module_22 from "@tsplus/stdlib/collections/Chunk/concat";
import { concreteSink } from "@effect/core/stream/Sink/operations/_internal/SinkInternal";
import { concreteStream, StreamInternal } from "@effect/core/stream/Stream/operations/_internal/StreamInternal";
/**
 * Applies the transducer to the stream and emits its outputs.
 *
 * @tsplus fluent ets/Stream transduce
 */

export function transduce_(self, sink, __tsplusTrace) {
  void 0;
  return new 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
 */

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

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.mjs.map